441 lines
19 KiB
TypeScript
441 lines
19 KiB
TypeScript
import React, { useState } from 'react';
|
|
import {
|
|
XCircle, Sparkles, AlertCircle, CheckCircle, RefreshCw,
|
|
Phone, Globe, Eye, EyeOff, LogIn, UserPlus
|
|
} from 'lucide-react';
|
|
import { COUNTRY_CODES, getCountryRuleByDialCode, getCountryRuleByName } from '../../constants/countries';
|
|
import { cleanPhoneInput } from '../../utils/validation';
|
|
|
|
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);
|
|
const activeRule = getCountryRuleByDialCode(authDialCode);
|
|
|
|
if (!isOpen) return null;
|
|
|
|
return (
|
|
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4 bg-slate-950/70 backdrop-blur-md transition-all duration-300">
|
|
<div className="relative w-full max-w-md bg-slate-900 border border-slate-800 rounded-3xl p-6 md:p-8 space-y-6 shadow-2xl text-slate-100">
|
|
{/* 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 Clinical 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'
|
|
? 'Onboard as a clinical practitioner to 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>
|
|
<div className="flex gap-2">
|
|
<div className="relative">
|
|
<select
|
|
value={authDialCode}
|
|
onChange={(e) => {
|
|
const newCode = e.target.value;
|
|
setAuthDialCode(newCode);
|
|
const rule = getCountryRuleByDialCode(newCode);
|
|
if (rule && rule.country !== 'Other') {
|
|
setAuthCountry(rule.country);
|
|
}
|
|
}}
|
|
className="w-[90px] bg-slate-950 border border-slate-800 rounded-xl px-3 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 appearance-none"
|
|
>
|
|
{COUNTRY_CODES.map((cc) => (
|
|
<option key={cc.code} value={cc.code}>
|
|
{cc.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
<input
|
|
type="tel"
|
|
value={authPhone}
|
|
onChange={(e) => setAuthPhone(cleanPhoneInput(e.target.value, activeRule.maxLength))}
|
|
placeholder={activeRule.placeholder}
|
|
maxLength={activeRule.maxLength}
|
|
className="flex-1 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>
|
|
<p className="text-[10px] text-slate-500 font-medium">
|
|
{activeRule.minLength === activeRule.maxLength
|
|
? `${activeRule.label || activeRule.country}: Requires exactly ${activeRule.maxLength} digits`
|
|
: `${activeRule.label || activeRule.country}: ${activeRule.minLength} to ${activeRule.maxLength} digits`}
|
|
</p>
|
|
</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>
|
|
<div className="relative">
|
|
<select
|
|
value={authCountry}
|
|
onChange={(e) => {
|
|
const newCountry = e.target.value;
|
|
setAuthCountry(newCountry);
|
|
const rule = getCountryRuleByName(newCountry);
|
|
if (rule) {
|
|
setAuthDialCode(rule.code);
|
|
}
|
|
}}
|
|
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 appearance-none"
|
|
>
|
|
{COUNTRY_CODES.map((cc) => (
|
|
<option key={cc.code} value={cc.country}>
|
|
{cc.country} (Calculates in {cc.currency} {cc.currency === 'INR' ? '₹' : '$'})
|
|
</option>
|
|
))}
|
|
</select>
|
|
<div className="absolute right-4 top-1/2 -translate-y-1/2 pointer-events-none text-slate-500 text-xs">
|
|
▼
|
|
</div>
|
|
</div>
|
|
</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 Session' : 'Register Clinical Profile'}
|
|
</>
|
|
)}
|
|
</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 clinical account' : 'Sign in here'}
|
|
</button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|