import { useState } from 'react' import { z } from 'zod' import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { Link, useNavigate } from '@tanstack/react-router' import { Loader2, LogIn } from 'lucide-react' import { toast } from 'sonner' import { IconFacebook, IconGithub } from '@/assets/brand-icons' import { useAuthStore } from '@/stores/auth-store' import { sleep, cn } from '@/lib/utils' import { Button } from '@/components/ui/button' import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form' import { Input } from '@/components/ui/input' import { PasswordInput } from '@/components/password-input' const formSchema = z.object({ email: z.email({ error: (iss) => (iss.input === '' ? 'Please enter your email' : undefined), }), password: z .string() .min(1, 'Please enter your password') .min(7, 'Password must be at least 7 characters long'), }) interface UserAuthFormProps extends React.HTMLAttributes { redirectTo?: string } export function UserAuthForm({ className, redirectTo, ...props }: UserAuthFormProps) { const [isLoading, setIsLoading] = useState(false) const navigate = useNavigate() const { auth } = useAuthStore() const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { email: '', password: '', }, }) function onSubmit(data: z.infer) { setIsLoading(true) toast.promise(sleep(2000), { loading: 'Signing in...', success: () => { setIsLoading(false) // Mock successful authentication with expiry computed at success time const mockUser = { accountNo: 'ACC001', email: data.email, role: ['user'], exp: Date.now() + 24 * 60 * 60 * 1000, // 24 hours from now } // Set user and access token auth.setUser(mockUser) auth.setAccessToken('mock-access-token') // Redirect to the stored location or default to dashboard const targetPath = redirectTo || '/' navigate({ to: targetPath, replace: true }) return `Welcome back, ${data.email}!` }, error: 'Error', }) } return (
( Email )} /> ( Password Forgot password? )} />
Or continue with
) }