2024-09-13 15:54:20 +00:00
|
|
|
"use client"
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import { toast } from "@/components/ui/use-toast";
|
|
|
|
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
import { Button } from "@/components/ui/button";
|
2024-09-16 10:53:11 +00:00
|
|
|
import { redirect } from "next/navigation";
|
|
|
|
import { loginSchema } from "./schema";
|
|
|
|
import { useRouter } from "next/navigation";
|
2024-09-13 15:54:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
export default function LoginForm() {
|
2024-09-16 10:53:11 +00:00
|
|
|
const router = useRouter()
|
|
|
|
const form = useForm<z.infer<typeof loginSchema>>({
|
|
|
|
resolver: zodResolver(loginSchema),
|
2024-09-13 15:54:20 +00:00
|
|
|
})
|
|
|
|
|
2024-09-16 10:53:11 +00:00
|
|
|
const onSubmit = form.handleSubmit(async (data) => {
|
|
|
|
// const res = await login(data)
|
|
|
|
// if (res?.error) {
|
|
|
|
// toast({ title: "Whoops!", description: res.error })
|
|
|
|
// form.reset()
|
|
|
|
// } else {
|
|
|
|
// toast({ title: "login successful" })
|
|
|
|
// }
|
|
|
|
const res = await fetch('/api/auth/login', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(data),
|
2024-09-13 15:54:20 +00:00
|
|
|
})
|
2024-09-16 10:53:11 +00:00
|
|
|
if (res.status === 200) {
|
|
|
|
toast({ title: "login successful!" })
|
|
|
|
router.push('/submission')
|
|
|
|
} else {
|
|
|
|
toast({ title: "login failed!" })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-09-13 15:54:20 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Form {...form}>
|
2024-09-16 10:53:11 +00:00
|
|
|
<form onSubmit={onSubmit}>
|
2024-09-13 15:54:20 +00:00
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="email"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem>
|
|
|
|
<FormLabel>Email Address</FormLabel>
|
|
|
|
<FormControl>
|
|
|
|
<Input placeholder="email goes here" {...field} />
|
|
|
|
</FormControl>
|
|
|
|
<FormMessage />
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
></FormField>
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="password"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem>
|
|
|
|
<FormLabel>Password</FormLabel>
|
|
|
|
<FormControl>
|
|
|
|
<Input placeholder="password goes here" type="password"{...field} />
|
|
|
|
</FormControl>
|
|
|
|
<FormMessage />
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
></FormField>
|
|
|
|
<Button type="submit">SUBMIT</Button>
|
|
|
|
</form>
|
|
|
|
</Form>
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|