|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useState } from 'react' |
| 4 | +import { useRouter } from 'next/navigation' |
| 5 | +import { zodResolver } from "@hookform/resolvers/zod" |
| 6 | +import { useForm } from "react-hook-form" |
| 7 | +import * as z from "zod" |
| 8 | +import { Button } from "@/components/ui/button" |
| 9 | +import { |
| 10 | + Form, |
| 11 | + FormControl, |
| 12 | + FormField, |
| 13 | + FormItem, |
| 14 | + FormLabel, |
| 15 | + FormMessage, |
| 16 | +} from "@/components/ui/form" |
| 17 | +import { Input } from "@/components/ui/input" |
| 18 | +import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" |
| 19 | +import { Loader2 } from 'lucide-react' |
| 20 | +import {createClient} from "@/utils/supabase/client"; |
| 21 | + |
| 22 | +const formSchema = z.object({ |
| 23 | + email: z.string().email({ |
| 24 | + message: "Please enter a valid email address.", |
| 25 | + }), |
| 26 | + password: z.string().min(6, { |
| 27 | + message: "Password must be at least 6 characters.", |
| 28 | + }), |
| 29 | +}) |
| 30 | + |
| 31 | +export function LoginForm() { |
| 32 | + const supabase = createClient() |
| 33 | + |
| 34 | + const [isLoading, setIsLoading] = useState(false) |
| 35 | + const [error, setError] = useState<string | null>(null) |
| 36 | + const router = useRouter() |
| 37 | + |
| 38 | + const form = useForm<z.infer<typeof formSchema>>({ |
| 39 | + resolver: zodResolver(formSchema), |
| 40 | + defaultValues: { |
| 41 | + email: "", |
| 42 | + password: "", |
| 43 | + }, |
| 44 | + }) |
| 45 | + |
| 46 | + async function onSubmit(values: z.infer<typeof formSchema>) { |
| 47 | + setIsLoading(true) |
| 48 | + setError(null) |
| 49 | + |
| 50 | + const { error } = await supabase.auth.signInWithPassword({ |
| 51 | + email: values.email, |
| 52 | + password: values.password, |
| 53 | + }) |
| 54 | + |
| 55 | + if (error) { |
| 56 | + setError(error.message) |
| 57 | + setIsLoading(false) |
| 58 | + } else { |
| 59 | + router.push('/') // Redirect to dashboard on successful login |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="flex items-center justify-center min-h-screen bg-gray-100"> |
| 65 | + <Card className="w-[350px]"> |
| 66 | + <CardHeader> |
| 67 | + <CardTitle>Login</CardTitle> |
| 68 | + </CardHeader> |
| 69 | + <CardContent> |
| 70 | + <Form {...form}> |
| 71 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8"> |
| 72 | + <FormField |
| 73 | + control={form.control} |
| 74 | + name="email" |
| 75 | + render={({ field }) => ( |
| 76 | + <FormItem> |
| 77 | + <FormLabel>Email</FormLabel> |
| 78 | + <FormControl> |
| 79 | + <Input placeholder="Enter your email" {...field} /> |
| 80 | + </FormControl> |
| 81 | + <FormMessage /> |
| 82 | + </FormItem> |
| 83 | + )} |
| 84 | + /> |
| 85 | + <FormField |
| 86 | + control={form.control} |
| 87 | + name="password" |
| 88 | + render={({ field }) => ( |
| 89 | + <FormItem> |
| 90 | + <FormLabel>Password</FormLabel> |
| 91 | + <FormControl> |
| 92 | + <Input type="password" placeholder="Enter your password" {...field} /> |
| 93 | + </FormControl> |
| 94 | + <FormMessage /> |
| 95 | + </FormItem> |
| 96 | + )} |
| 97 | + /> |
| 98 | + {error && <p className="text-red-500 text-sm">{error}</p>} |
| 99 | + <Button type="submit" className="w-full" disabled={isLoading}> |
| 100 | + {isLoading ? ( |
| 101 | + <> |
| 102 | + <Loader2 className="mr-2 h-4 w-4 animate-spin" /> |
| 103 | + Logging in... |
| 104 | + </> |
| 105 | + ) : ( |
| 106 | + 'Login' |
| 107 | + )} |
| 108 | + </Button> |
| 109 | + </form> |
| 110 | + </Form> |
| 111 | + </CardContent> |
| 112 | + <CardFooter> |
| 113 | + <p className="text-sm text-gray-500"> |
| 114 | + Demo Credentials:<br /> |
| 115 | + Email: data@table.org <br /> |
| 116 | + Password: serverside |
| 117 | + </p> |
| 118 | + </CardFooter> |
| 119 | + </Card> |
| 120 | + </div> |
| 121 | + ) |
| 122 | +} |
| 123 | + |
0 commit comments