1- import { cn } from "@/lib/utils"
2- import { Button } from "@/components/ui/button"
1+ 'use client'
2+
3+ import { cn } from '@/lib/utils'
4+ import { Button } from '@/components/ui/button'
35import {
46 Card ,
57 CardContent ,
68 CardDescription ,
79 CardHeader ,
810 CardTitle ,
9- } from "@/components/ui/card"
10- import { Input } from "@/components/ui/input"
11- import { Label } from "@/components/ui/label"
11+ } from '@/components/ui/card'
12+ import { Input } from '@/components/ui/input'
13+ import { Label } from '@/components/ui/label'
14+ import { useActionState } from 'react'
15+ import { login } from '@/lib/actions/login'
16+ import Link from 'next/link'
1217
1318export function LoginForm ( {
1419 className,
1520 ...props
16- } : React . ComponentPropsWithoutRef < "div" > ) {
21+ } : React . ComponentPropsWithoutRef < 'div' > & { email ?: string } ) {
22+ const initialState = {
23+ error : '' ,
24+ email : props . email ?? '' ,
25+ password : '' ,
26+ }
27+ const [ state , action , isPending ] = useActionState ( login , initialState , '/' )
1728 return (
18- < div className = { cn ( " flex flex-col gap-6" , className ) } { ...props } >
29+ < div className = { cn ( ' flex flex-col gap-6' , className ) } { ...props } >
1930 < Card >
2031 < CardHeader >
2132 < CardTitle className = "text-2xl" > Login</ CardTitle >
2233 < CardDescription >
2334 Enter your email below to login to your account
2435 </ CardDescription >
36+ { state . error && (
37+ < div className = "text-sm text-red-400" > { state . error } </ div >
38+ ) }
2539 </ CardHeader >
2640 < CardContent >
27- < form >
41+ < form action = { action } >
2842 < div className = "flex flex-col gap-6" >
2943 < div className = "grid gap-2" >
3044 < Label htmlFor = "email" > Email</ Label >
3145 < Input
3246 id = "email"
3347 type = "email"
34- placeholder = "m@example.com"
48+ name = "email"
49+ // placeholder="m@example.com"
50+ defaultValue = { state . email }
3551 required
3652 />
3753 </ div >
@@ -45,20 +61,26 @@ export function LoginForm({
4561 Forgot your password?
4662 </ a >
4763 </ div >
48- < Input id = "password" type = "password" required />
64+ < Input
65+ id = "password"
66+ name = "password"
67+ type = "password"
68+ // defaultValue={state.password}
69+ required
70+ />
4971 </ div >
50- < Button type = "submit" className = "w-full" >
72+ < Button type = "submit" className = "w-full" disabled = { isPending } >
5173 Login
5274 </ Button >
53- < Button variant = "outline" className = "w-full" >
75+ { /* <Button variant="outline" className="w-full">
5476 Login with Google
55- </ Button >
77+ </Button> */ }
5678 </ div >
5779 < div className = "mt-4 text-center text-sm" >
58- Don't have an account?{ " " }
59- < a href = "# " className = "underline underline-offset-4" >
80+ Don't have an account?{ ' ' }
81+ < Link href = "/signup " className = "underline underline-offset-4" >
6082 Sign up
61- </ a >
83+ </ Link >
6284 </ div >
6385 </ form >
6486 </ CardContent >
0 commit comments