'use client'; import React, { useState } from 'react'; import Link from 'next/link'; import Image from 'next/image'; import { useRouter } from 'next/navigation'; import { Eye, EyeOff, Mail, Lock, AlertCircle, CheckCircle } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { cn } from '@/utils/cn'; import { useAuth } from '@/contexts/AuthContext'; interface LoginForm { email: string; password: string; } interface FormErrors { email?: string; password?: string; general?: string; } export default function LoginPage() { const router = useRouter(); const { login, isLoading: authLoading } = useAuth(); const [form, setForm] = useState({ email: '', password: '' }); const [errors, setErrors] = useState({}); const [showPassword, setShowPassword] = useState(false); const [isLoading, setIsLoading] = useState(false); const [rememberMe, setRememberMe] = useState(false); const validateForm = (): boolean => { const newErrors: FormErrors = {}; // Email validation if (!form.email) { newErrors.email = 'Email wajib diisi'; } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email)) { newErrors.email = 'Format email tidak valid'; } // Password validation if (!form.password) { newErrors.password = 'Password wajib diisi'; } else if (form.password.length < 6) { newErrors.password = 'Password minimal 6 karakter'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleInputChange = (field: keyof LoginForm, value: string) => { setForm(prev => ({ ...prev, [field]: value })); // Clear error when user starts typing if (errors[field]) { setErrors(prev => ({ ...prev, [field]: undefined })); } }; const handleQuickLogin = (email: string, password: string) => { setForm({ email, password }); setErrors({}); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validateForm()) return; setIsLoading(true); setErrors({}); try { const success = await login(form.email, form.password); if (!success) { setErrors({ general: 'Email atau password salah' }); } // If successful, AuthContext will handle the redirect } catch (error) { setErrors({ general: 'Terjadi kesalahan. Silakan coba lagi.' }); } finally { setIsLoading(false); } }; return (
{/* Logo and Title */}
Logo Badan Gizi Nasional

Selamat Datang

Masuk ke akun Learning Management System Anda

Masuk Masukkan email dan password untuk mengakses dashboard
{/* General Error */} {errors.general && ( {errors.general} )} {/* Email Field */}
handleInputChange('email', e.target.value)} className={cn( "pl-10", errors.email && "border-red-500 focus:border-red-500" )} disabled={isLoading} />
{errors.email && (

{errors.email}

)}
{/* Password Field */}
handleInputChange('password', e.target.value)} className={cn( "pl-10 pr-10", errors.password && "border-red-500 focus:border-red-500" )} disabled={isLoading} />
{errors.password && (

{errors.password}

)}
{/* Remember Me & Forgot Password */}
setRememberMe(e.target.checked)} className="w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500" disabled={isLoading} />
Lupa password?
{/* Submit Button */}
{/* Demo Credentials */}

Demo Credentials:

{/* Admin Credentials */}

👨‍💼 Admin:

Email: admin@lms.com

Password: admin123

{/* Student Credentials */}

👨‍🎓 Student:

Email: student@lms.com

Password: student123

{/* Register Link */}

Belum punya akun?{' '} Daftar sekarang

{/* Footer */}

© 2024 Learning Management System. All rights reserved.

); }