419 lines
17 KiB
TypeScript
419 lines
17 KiB
TypeScript
import React, { useState } from 'react';
|
|
import {
|
|
XCircle, Sparkles, AlertCircle, CheckCircle, RefreshCw,
|
|
Phone, Globe, Eye, EyeOff, LogIn, UserPlus
|
|
} from 'lucide-react';
|
|
import { getCountryRuleByName } from '../../constants/countries';
|
|
import { CountrySelect } from '../common/CountrySelect';
|
|
import { PhoneInput } from '../common/PhoneInput';
|
|
|
|
interface AuthModalProps {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
authMode: 'login' | 'register' | 'forgot';
|
|
setAuthMode: (mode: 'login' | 'register' | 'forgot') => void;
|
|
authEmail: string;
|
|
setAuthEmail: (email: string) => void;
|
|
authPassword: string;
|
|
setAuthPassword: (pwd: string) => void;
|
|
authDisplayName: string;
|
|
setAuthDisplayName: (name: string) => void;
|
|
authPhone: string;
|
|
setAuthPhone: (phone: string) => void;
|
|
authDialCode: string;
|
|
setAuthDialCode: (code: string) => void;
|
|
authCountry: string;
|
|
setAuthCountry: (country: string) => void;
|
|
authLoading: boolean;
|
|
authError: string | null;
|
|
setAuthError: (err: string | null) => void;
|
|
onAuthSubmit: (e: React.FormEvent) => Promise<void>;
|
|
// Forgot password flow
|
|
resetStep: 'request' | 'verify';
|
|
setResetStep: (step: 'request' | 'verify') => void;
|
|
resetOtp: string;
|
|
setResetOtp: (otp: string) => void;
|
|
resetNewPassword: string;
|
|
setResetNewPassword: (pwd: string) => void;
|
|
resetConfirmPassword: string;
|
|
setResetConfirmPassword: (pwd: string) => void;
|
|
resetSuccessMessage: string | null;
|
|
setResetSuccessMessage: (msg: string | null) => void;
|
|
onForgotPasswordRequest: (e: React.FormEvent) => Promise<void>;
|
|
onResetPasswordSubmit: (e: React.FormEvent) => Promise<void>;
|
|
}
|
|
|
|
export const AuthModal: React.FC<AuthModalProps> = ({
|
|
isOpen,
|
|
onClose,
|
|
authMode,
|
|
setAuthMode,
|
|
authEmail,
|
|
setAuthEmail,
|
|
authPassword,
|
|
setAuthPassword,
|
|
authDisplayName,
|
|
setAuthDisplayName,
|
|
authPhone,
|
|
setAuthPhone,
|
|
authDialCode,
|
|
setAuthDialCode,
|
|
authCountry,
|
|
setAuthCountry,
|
|
authLoading,
|
|
authError,
|
|
setAuthError,
|
|
onAuthSubmit,
|
|
resetStep,
|
|
setResetStep,
|
|
resetOtp,
|
|
setResetOtp,
|
|
resetNewPassword,
|
|
setResetNewPassword,
|
|
resetConfirmPassword,
|
|
setResetConfirmPassword,
|
|
resetSuccessMessage,
|
|
setResetSuccessMessage,
|
|
onForgotPasswordRequest,
|
|
onResetPasswordSubmit,
|
|
}) => {
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
|
|
if (!isOpen) return null;
|
|
|
|
return (
|
|
<div className="fixed inset-0 z-[100] flex items-center justify-center p-3 sm:p-4 bg-slate-950/70 backdrop-blur-md transition-all duration-300 overflow-y-auto">
|
|
<div className="relative w-full max-w-md bg-slate-900 border border-slate-800 rounded-3xl p-5 sm:p-6 md:p-8 space-y-4 sm:space-y-6 shadow-2xl text-slate-100 max-h-[92vh] overflow-y-auto my-auto">
|
|
{/* Close button */}
|
|
<button
|
|
onClick={onClose}
|
|
className="absolute top-4 right-4 h-8 w-8 rounded-full bg-slate-800 hover:bg-slate-700/80 flex items-center justify-center text-slate-400 hover:text-slate-200 transition-colors"
|
|
>
|
|
<XCircle className="h-5 w-5" />
|
|
</button>
|
|
|
|
{/* Title / Headers */}
|
|
<div className="text-center space-y-1.5">
|
|
<div className="h-10 w-10 rounded-xl bg-indigo-600 flex items-center justify-center mx-auto shadow-md">
|
|
<Sparkles className="h-5 w-5 text-white animate-spin" style={{ animationDuration: '6s' }} />
|
|
</div>
|
|
<h3 className="text-xl font-black tracking-tight text-white">
|
|
{authMode === 'login'
|
|
? 'Welcome Back!'
|
|
: authMode === 'register'
|
|
? 'Create Account'
|
|
: 'Reset Password'}
|
|
</h3>
|
|
<p className="text-xs text-slate-400 leading-relaxed font-semibold">
|
|
{authMode === 'login'
|
|
? 'Sign in to access your saved TCM modules and invoices.'
|
|
: authMode === 'register'
|
|
? 'Sign up to get started and access TCM modules.'
|
|
: resetStep === 'request'
|
|
? 'Enter your registered email to receive a 6-digit verification code.'
|
|
: 'Enter the 6-digit code sent to your email and your new password.'}
|
|
</p>
|
|
</div>
|
|
|
|
{/* Error badge */}
|
|
{authError && (
|
|
<div className="p-3.5 bg-rose-500/10 border border-rose-500/30 text-rose-400 text-xs rounded-xl font-bold flex gap-2 items-center">
|
|
<AlertCircle className="h-4.5 w-4.5 flex-shrink-0" />
|
|
<span>{authError}</span>
|
|
</div>
|
|
)}
|
|
|
|
{/* Success message for OTP */}
|
|
{resetSuccessMessage && authMode === 'forgot' && (
|
|
<div className="p-3.5 bg-emerald-500/10 border border-emerald-500/30 text-emerald-400 text-xs rounded-xl font-bold flex gap-2 items-center">
|
|
<CheckCircle className="h-4.5 w-4.5 flex-shrink-0" />
|
|
<span>{resetSuccessMessage}</span>
|
|
</div>
|
|
)}
|
|
|
|
{/* Forgot Password Flow */}
|
|
{authMode === 'forgot' ? (
|
|
resetStep === 'request' ? (
|
|
<form onSubmit={onForgotPasswordRequest} noValidate className="space-y-4 text-left">
|
|
<div className="space-y-1.5">
|
|
<label className="text-[10px] uppercase font-black tracking-wider text-slate-500">
|
|
Registered Email Address
|
|
</label>
|
|
<input
|
|
type="email"
|
|
required
|
|
value={authEmail}
|
|
onChange={(e) => setAuthEmail(e.target.value)}
|
|
placeholder="doctor@example.com"
|
|
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 placeholder:text-slate-700"
|
|
/>
|
|
</div>
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={authLoading}
|
|
className="w-full h-11 bg-indigo-600 hover:bg-indigo-500 text-white font-bold text-sm rounded-xl transition-all duration-300 flex items-center justify-center gap-2 mt-4 shadow-lg shadow-indigo-600/10"
|
|
>
|
|
{authLoading ? <RefreshCw className="h-4 w-4 animate-spin" /> : 'Send Verification Code'}
|
|
</button>
|
|
|
|
<div className="text-center pt-2 text-xs">
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setAuthMode('login');
|
|
setAuthError(null);
|
|
}}
|
|
className="text-indigo-400 hover:text-indigo-300 font-bold underline"
|
|
>
|
|
← Back to sign in
|
|
</button>
|
|
</div>
|
|
</form>
|
|
) : (
|
|
<form onSubmit={onResetPasswordSubmit} noValidate className="space-y-4 text-left">
|
|
<div className="space-y-1.5">
|
|
<label className="text-[10px] uppercase font-black tracking-wider text-slate-500">
|
|
6-Digit Verification Code (OTP)
|
|
</label>
|
|
<input
|
|
type="text"
|
|
required
|
|
maxLength={6}
|
|
value={resetOtp}
|
|
onChange={(e) => setResetOtp(e.target.value.replace(/\D/g, ''))}
|
|
placeholder="123456"
|
|
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-base font-mono font-bold tracking-widest text-center text-indigo-400 focus:outline-none focus:border-indigo-500"
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<label className="text-[10px] uppercase font-black tracking-wider text-slate-500">
|
|
New Password
|
|
</label>
|
|
<input
|
|
type="password"
|
|
required
|
|
value={resetNewPassword}
|
|
onChange={(e) => setResetNewPassword(e.target.value)}
|
|
placeholder="Min 8 chars (1 uppercase, 1 number, 1 special)"
|
|
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500"
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<label className="text-[10px] uppercase font-black tracking-wider text-slate-500">
|
|
Confirm New Password
|
|
</label>
|
|
<input
|
|
type="password"
|
|
required
|
|
value={resetConfirmPassword}
|
|
onChange={(e) => setResetConfirmPassword(e.target.value)}
|
|
placeholder="Re-enter new password"
|
|
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500"
|
|
/>
|
|
</div>
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={authLoading}
|
|
className="w-full h-11 bg-emerald-600 hover:bg-emerald-500 text-white font-bold text-sm rounded-xl transition-all duration-300 flex items-center justify-center gap-2 mt-4 shadow-lg shadow-emerald-600/10"
|
|
>
|
|
{authLoading ? <RefreshCw className="h-4 w-4 animate-spin" /> : 'Set New Password'}
|
|
</button>
|
|
|
|
<div className="flex justify-between items-center pt-2 text-xs">
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setResetStep('request');
|
|
setAuthError(null);
|
|
}}
|
|
className="text-slate-400 hover:text-white font-semibold"
|
|
>
|
|
Resend code
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setAuthMode('login');
|
|
setAuthError(null);
|
|
}}
|
|
className="text-indigo-400 hover:text-indigo-300 font-bold underline"
|
|
>
|
|
Back to sign in
|
|
</button>
|
|
</div>
|
|
</form>
|
|
)
|
|
) : (
|
|
/* Standard Login & Register Form */
|
|
<form onSubmit={onAuthSubmit} noValidate className="space-y-4 text-left">
|
|
{/* Common: Email */}
|
|
<div className="space-y-1.5">
|
|
<label className="text-[10px] uppercase font-black tracking-wider text-slate-500">
|
|
Email Address
|
|
</label>
|
|
<input
|
|
type="email"
|
|
required
|
|
value={authEmail}
|
|
onChange={(e) => setAuthEmail(e.target.value)}
|
|
placeholder="doctor@example.com"
|
|
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 placeholder:text-slate-700"
|
|
/>
|
|
</div>
|
|
|
|
{/* Register exclusive: Display Name */}
|
|
{authMode === 'register' && (
|
|
<div className="space-y-1.5">
|
|
<label className="text-[10px] uppercase font-black tracking-wider text-slate-500">
|
|
Display Name
|
|
</label>
|
|
<input
|
|
type="text"
|
|
required
|
|
value={authDisplayName}
|
|
onChange={(e) => setAuthDisplayName(e.target.value)}
|
|
placeholder="Dr. John Doe"
|
|
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 placeholder:text-slate-700"
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
{/* Register exclusive: Phone Number with Country Code */}
|
|
{authMode === 'register' && (
|
|
<div className="space-y-1.5">
|
|
<label className="text-[10px] uppercase font-black tracking-wider text-slate-500 flex items-center gap-1">
|
|
<Phone className="h-3 w-3 text-indigo-400" /> Phone Number
|
|
</label>
|
|
<PhoneInput
|
|
dialCode={authDialCode}
|
|
phone={authPhone}
|
|
country={authCountry}
|
|
clearOnDialCodeChange={true}
|
|
onDialCodeChange={(newCode, newCountry) => {
|
|
setAuthDialCode(newCode);
|
|
setAuthCountry(newCountry);
|
|
setAuthPhone('');
|
|
}}
|
|
onPhoneChange={(newPhone) => {
|
|
setAuthPhone(newPhone);
|
|
if (authError && authError.toLowerCase().includes('phone')) {
|
|
setAuthError(null);
|
|
}
|
|
}}
|
|
theme="dark"
|
|
error={authError && authError.toLowerCase().includes('phone') ? authError : null}
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
{/* Register exclusive: Country */}
|
|
{authMode === 'register' && (
|
|
<div className="space-y-1.5">
|
|
<label className="text-[10px] uppercase font-black tracking-wider text-slate-500 flex items-center gap-1">
|
|
<Globe className="h-3 w-3 text-indigo-400" /> Country (Defaults currency)
|
|
</label>
|
|
<CountrySelect
|
|
value={authCountry}
|
|
onChange={(newCountry) => {
|
|
if (newCountry !== authCountry) {
|
|
setAuthCountry(newCountry);
|
|
const rule = getCountryRuleByName(newCountry);
|
|
if (rule) {
|
|
setAuthDialCode(rule.code);
|
|
}
|
|
setAuthPhone('');
|
|
}
|
|
}}
|
|
theme="dark"
|
|
size="md"
|
|
showCurrency={true}
|
|
showDialCode={true}
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
{/* Common: Password */}
|
|
<div className="space-y-1.5">
|
|
<div className="flex justify-between items-center">
|
|
<label className="text-[10px] uppercase font-black tracking-wider text-slate-500">
|
|
Password
|
|
</label>
|
|
{authMode === 'login' && (
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setAuthMode('forgot');
|
|
setResetStep('request');
|
|
setAuthError(null);
|
|
setResetSuccessMessage(null);
|
|
}}
|
|
className="text-[11px] text-indigo-400 hover:text-indigo-300 font-semibold"
|
|
>
|
|
Forgot password?
|
|
</button>
|
|
)}
|
|
</div>
|
|
<div className="relative">
|
|
<input
|
|
type={showPassword ? 'text' : 'password'}
|
|
required
|
|
value={authPassword}
|
|
onChange={(e) => setAuthPassword(e.target.value)}
|
|
placeholder="••••••••"
|
|
className="w-full bg-slate-950 border border-slate-800 rounded-xl pl-4 pr-12 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 placeholder:text-slate-700"
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowPassword(!showPassword)}
|
|
className="absolute right-4 top-1/2 -translate-y-1/2 text-slate-500 hover:text-slate-300 transition-colors p-1"
|
|
>
|
|
{showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Submit trigger button */}
|
|
<button
|
|
type="submit"
|
|
disabled={authLoading}
|
|
className="w-full h-11 bg-indigo-600 hover:bg-indigo-500 text-white font-bold text-sm rounded-xl transition-all duration-300 flex items-center justify-center gap-2 mt-4 shadow-lg shadow-indigo-600/10"
|
|
>
|
|
{authLoading ? (
|
|
<RefreshCw className="h-4 w-4 animate-spin" />
|
|
) : (
|
|
<>
|
|
{authMode === 'login' ? <LogIn className="h-4 w-4" /> : <UserPlus className="h-4 w-4" />}
|
|
{authMode === 'login' ? 'Sign In' : 'Create Account'}
|
|
</>
|
|
)}
|
|
</button>
|
|
</form>
|
|
)}
|
|
|
|
{/* Toggle auth mode links */}
|
|
{authMode !== 'forgot' && (
|
|
<div className="text-center pt-2 text-xs">
|
|
<span className="text-slate-400 font-semibold">
|
|
{authMode === 'login' ? "Don't have an account? " : 'Already registered? '}
|
|
</span>
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setAuthMode(authMode === 'login' ? 'register' : 'login');
|
|
setAuthError(null);
|
|
}}
|
|
className="text-indigo-400 hover:text-indigo-300 font-bold underline"
|
|
>
|
|
{authMode === 'login' ? 'Create an account' : 'Sign in here'}
|
|
</button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|