|
|
|
|
|
"use client"; |
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod"; |
|
|
import { useForm } from "react-hook-form"; |
|
|
import { Button } from "@/components/ui/button"; |
|
|
import { |
|
|
Form, |
|
|
FormControl, |
|
|
FormField, |
|
|
FormItem, |
|
|
FormLabel, |
|
|
FormMessage, |
|
|
} from "@/components/ui/form"; |
|
|
import { Input } from "@/components/ui/input"; |
|
|
import { AdminLoginSchema, type AdminLoginInput } from "@/lib/schemas"; |
|
|
import { adminLoginUser } from "@/lib/actions/auth"; |
|
|
import { useToast } from "@/hooks/use-toast"; |
|
|
import { useState } from "react"; |
|
|
import { Loader2, ShieldCheck } from "lucide-react"; |
|
|
|
|
|
|
|
|
export function AdminLoginForm() { |
|
|
const { toast } = useToast(); |
|
|
const [isLoading, setIsLoading] = useState(false); |
|
|
|
|
|
const form = useForm<AdminLoginInput>({ |
|
|
resolver: zodResolver(AdminLoginSchema), |
|
|
defaultValues: { |
|
|
email: "", |
|
|
password: "", |
|
|
}, |
|
|
}); |
|
|
|
|
|
async function onSubmit(values: AdminLoginInput) { |
|
|
setIsLoading(true); |
|
|
try { |
|
|
const result = await adminLoginUser(values); |
|
|
|
|
|
|
|
|
if (result && result.success === false) { |
|
|
toast({ |
|
|
title: "Admin Login Failed", |
|
|
description: result.message, |
|
|
variant: "destructive", |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} catch (error: any) { |
|
|
|
|
|
if (error.message?.includes('NEXT_REDIRECT')) { |
|
|
|
|
|
} else { |
|
|
toast({ |
|
|
title: "Error", |
|
|
description: error.message || "An unexpected error occurred. Please try again.", |
|
|
variant: "destructive", |
|
|
}); |
|
|
} |
|
|
} finally { |
|
|
setIsLoading(false); |
|
|
} |
|
|
} |
|
|
|
|
|
return ( |
|
|
<Form {...form}> |
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6"> |
|
|
<FormField |
|
|
control={form.control} |
|
|
name="email" |
|
|
render={({ field }) => ( |
|
|
<FormItem> |
|
|
<FormLabel>Admin Email</FormLabel> |
|
|
<FormControl> |
|
|
<Input placeholder="admin@example.com" {...field} type="email" /> |
|
|
</FormControl> |
|
|
<FormMessage /> |
|
|
</FormItem> |
|
|
)} |
|
|
/> |
|
|
<FormField |
|
|
control={form.control} |
|
|
name="password" |
|
|
render={({ field }) => ( |
|
|
<FormItem> |
|
|
<FormLabel>Admin Password</FormLabel> |
|
|
<FormControl> |
|
|
<Input type="password" placeholder="••••••••" {...field} /> |
|
|
</FormControl> |
|
|
<FormMessage /> |
|
|
</FormItem> |
|
|
)} |
|
|
/> |
|
|
<Button type="submit" className="w-full" disabled={isLoading}> |
|
|
{isLoading ? ( |
|
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" /> |
|
|
) : ( |
|
|
<ShieldCheck className="mr-2 h-4 w-4" /> |
|
|
)} |
|
|
Login as Admin |
|
|
</Button> |
|
|
</form> |
|
|
</Form> |
|
|
); |
|
|
} |
|
|
|