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-18 09:56:08 +00:00
|
|
|
import { useSearchParams } from "next/navigation";
|
|
|
|
import revalidate from "./revalidate";
|
|
|
|
import { useState } from "react";
|
|
|
|
import Link from "next/link";
|
2024-09-13 15:54:20 +00:00
|
|
|
|
|
|
|
export default function LoginForm() {
|
2024-09-16 10:53:11 +00:00
|
|
|
const router = useRouter()
|
2024-09-18 09:56:08 +00:00
|
|
|
const searchParams = useSearchParams()
|
2024-09-19 09:37:01 +00:00
|
|
|
const redirect = searchParams.get("from") ?? "submission"
|
2024-09-16 10:53:11 +00:00
|
|
|
const form = useForm<z.infer<typeof loginSchema>>({
|
|
|
|
resolver: zodResolver(loginSchema),
|
2024-09-13 15:54:20 +00:00
|
|
|
})
|
2024-09-18 09:56:08 +00:00
|
|
|
const [submitted, setSubmitted] = useState(false)
|
2024-09-13 15:54:20 +00:00
|
|
|
|
2024-09-18 09:56:08 +00:00
|
|
|
const onSubmit = form.handleSubmit(async (data, event) => {
|
|
|
|
event.preventDefault()
|
2024-09-16 10:53:11 +00:00
|
|
|
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!" })
|
2024-09-18 09:56:08 +00:00
|
|
|
setSubmitted(true)
|
|
|
|
await revalidate(redirect)
|
|
|
|
router.refresh()
|
|
|
|
router.push(redirect)
|
2024-09-16 10:53:11 +00:00
|
|
|
} else {
|
|
|
|
toast({ title: "login failed!" })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-09-13 15:54:20 +00:00
|
|
|
|
|
|
|
return (
|
2024-09-18 09:56:08 +00:00
|
|
|
<>
|
|
|
|
{submitted ? <p>Logging in...</p> :
|
|
|
|
<Form {...form}>
|
|
|
|
<form onSubmit={onSubmit}>
|
|
|
|
<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>
|
|
|
|
}
|
|
|
|
</>
|
2024-09-13 15:54:20 +00:00
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|