Spaces:
Sleeping
Sleeping
| import { useState } from 'react'; | |
| import { useNavigate, Link } from 'react-router-dom'; | |
| import { useAuth } from '../../lib/authContext'; | |
| import { Button } from '../ui/button'; | |
| import { Input } from '../ui/input'; | |
| import { Label } from '../ui/label'; | |
| import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '../ui/card'; | |
| import { BookOpen, Mail } from 'lucide-react'; | |
| import { useLanguage } from '../../lib/languageContext'; | |
| import { translations } from '../../lib/translations'; | |
| import { LanguageSwitcher } from '../LanguageSwitcher'; | |
| export function SignIn() { | |
| const [email, setEmail] = useState(''); | |
| const [password, setPassword] = useState(''); | |
| const [loading, setLoading] = useState(false); | |
| const [error, setError] = useState<string | null>(null); | |
| const { signIn } = useAuth(); | |
| const { language } = useLanguage(); | |
| const t = translations[language].auth; | |
| const navigate = useNavigate(); | |
| const handleSubmit = async (e: React.FormEvent) => { | |
| e.preventDefault(); | |
| setLoading(true); | |
| setError(null); | |
| try { | |
| const user = await signIn(email, password); | |
| const role = user?.role?.toLowerCase(); | |
| const approval = user?.sheikhApprovalStatus?.toLowerCase(); | |
| console.log(approval); | |
| if (role === 'student') { | |
| navigate('/student/dashboard'); | |
| } else if (role === 'admin') { | |
| navigate('/admin/dashboard'); | |
| } else if (role === 'sheikh') { | |
| if(approval ==='rejected'){ | |
| navigate('/sheikh/interview'); | |
| } | |
| else if (!user.profileCompleted) { | |
| navigate('/complete-profile/sheikh'); | |
| } else if (approval === 'approved') { | |
| navigate('/sheikh/dashboard'); | |
| } else { | |
| navigate('/sheikh/interview'); | |
| } | |
| } else { | |
| navigate('/'); | |
| } | |
| } catch (err) { | |
| console.error('Login error:', err); | |
| setError(err instanceof Error ? err.message : 'Login failed'); | |
| setLoading(false); | |
| } | |
| }; | |
| const handleGoogleSignIn = async () => { | |
| navigate('/auth/google'); | |
| }; | |
| const handleForgotPassword = () => { | |
| navigate('/forgot-password'); | |
| }; | |
| return ( | |
| <div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-emerald-50 to-teal-50 p-4"> | |
| <div className="absolute top-4 right-4"> | |
| <LanguageSwitcher /> | |
| </div> | |
| <Card className="w-full max-w-md"> | |
| <CardHeader className="text-center"> | |
| <div className="flex justify-center mb-4"> | |
| <div className="w-16 h-16 bg-emerald-600 rounded-full flex items-center justify-center"> | |
| <BookOpen className="h-8 w-8 text-white" /> | |
| </div> | |
| </div> | |
| <CardTitle>{t.welcomeTitle}</CardTitle> | |
| <CardDescription>{t.welcomeDescription}</CardDescription> | |
| </CardHeader> | |
| <CardContent className="space-y-4"> | |
| {/* Google Login */} | |
| <Button | |
| variant="outline" | |
| className="w-full gap-2" | |
| onClick={handleGoogleSignIn} | |
| disabled={loading} | |
| > | |
| {t.signInWithGoogle} | |
| </Button> | |
| <div className="relative"> | |
| <div className="absolute inset-0 flex items-center"> | |
| <span className="w-full border-t" /> | |
| </div> | |
| <div className="relative flex justify-center"> | |
| <span className="bg-white px-2 text-muted-foreground">{t.orContinueWith}</span> | |
| </div> | |
| </div> | |
| {/* Email / Password */} | |
| <form onSubmit={handleSubmit} className="space-y-4"> | |
| <div className="space-y-2"> | |
| <Label htmlFor="email">{t.emailLabel}</Label> | |
| <div className="relative"> | |
| <Mail className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" /> | |
| <Input | |
| id="email" | |
| type="email" | |
| value={email} | |
| placeholder={language === 'ar' ? 'ادخل الايميل' : 'Enter your email'} | |
| onChange={(e) => setEmail(e.target.value)} | |
| className="pl-10" | |
| required | |
| /> | |
| </div> | |
| </div> | |
| <div className="space-y-2"> | |
| <div className="flex justify-between items-center"> | |
| <Label htmlFor="password">{t.passwordLabel}</Label> | |
| <button | |
| type="button" | |
| onClick={handleForgotPassword} | |
| className="text-sm text-emerald-600 hover:text-emerald-700 hover:underline" | |
| > | |
| {t.forgotPassword || 'Forgot password?'} | |
| </button> | |
| </div> | |
| <Input | |
| id="password" | |
| type="password" | |
| placeholder={language === 'ar' ? 'ادخل الباسورد' : 'Enter your password'} | |
| value={password} | |
| onChange={(e) => setPassword(e.target.value)} | |
| required | |
| /> | |
| </div> | |
| {error && ( | |
| <p className="text-red-600 text-sm text-center">{error}</p> | |
| )} | |
| <Button | |
| type="submit" | |
| className="w-full bg-emerald-600" | |
| disabled={loading} | |
| > | |
| {loading ? `${t.signInButton}...` : t.signInButton} | |
| </Button> | |
| </form> | |
| <div className="text-center"> | |
| <Link to="/register" className="text-emerald-600 hover:underline"> | |
| {t.noAccount} {t.signUpLink} | |
| </Link> | |
| </div> | |
| </CardContent> | |
| </Card> | |
| </div> | |
| ); | |
| } |