1- "use client"
2- import { cn } from "@/lib/utils"
3- import { Button } from "@/components/ui/button"
1+ "use client" ;
2+ import { login } from "@/api/auth" ;
3+ import { Button } from "@/components/ui/button" ;
44import {
55 Card ,
66 CardContent ,
77 CardDescription ,
88 CardHeader ,
99 CardTitle ,
10- } from "@/components/ui/card"
11- import { Input } from "@/components/ui/input"
12- import { Label } from "@/components/ui/label"
13- import { useState } from "react"
14- import { login } from "@/api/auth"
15- import { useRouter } from "next/navigation"
10+ } from "@/components/ui/card" ;
11+ import { Input } from "@/components/ui/input" ;
12+ import { Label } from "@/components/ui/label" ;
13+ import { cn } from "@/lib/utils" ;
14+ import { Loader2 } from "lucide-react" ; // Import spinner icon
15+ import { useRouter } from "next/navigation" ;
16+ import { useState } from "react" ;
1617
1718export function LoginForm ( {
1819 className,
1920 ...props
2021} : React . ComponentPropsWithoutRef < "div" > ) {
21- const [ email , setEmail ] = useState < string > ( "" )
22+ const [ email , setEmail ] = useState < string > ( "" ) ;
2223 const [ password , setPassword ] = useState < string > ( "" ) ;
23- const router = useRouter ( )
24-
24+ const [ loading , setLoading ] = useState < boolean > ( false ) ; // Loading state
25+ const router = useRouter ( ) ;
2526
2627 const handleLogin = async ( e : { preventDefault : ( ) => void } ) => {
2728 e . preventDefault ( ) ;
29+ setLoading ( true ) ; // Set loading to true when login starts
2830 try {
29- const response = await login ( email , password ) ;
30- if ( response . status === "success" ) {
31- router . push ( "/users" )
32- }
31+ const response = await login ( email , password ) ;
32+ if ( response . status === "success" ) {
33+ router . push ( "/users" ) ;
34+ }
3335 } catch ( err ) {
34- throw err
36+ console . error ( "Login failed" , err ) ;
37+ } finally {
38+ setLoading ( false ) ; // Reset loading state after login attempt
3539 }
36- } ;
40+ } ;
41+
3742 return (
3843 < div className = { cn ( "flex flex-col gap-6" , className ) } { ...props } >
3944 < Card >
@@ -49,27 +54,40 @@ export function LoginForm({
4954 < div className = "grid gap-2" >
5055 < Label htmlFor = "email" > Email</ Label >
5156 < Input
52- value = { email }
57+ value = { email }
5358 id = "email"
5459 type = "email"
5560 placeholder = "m@example.com"
5661 required
57- onChange = { ( e ) => { setEmail ( e . target . value ) } }
62+ onChange = { ( e ) => setEmail ( e . target . value ) }
5863 />
5964 </ div >
6065 < div className = "grid gap-2" >
6166 < div className = "flex items-center" >
6267 < Label htmlFor = "password" > Password</ Label >
6368 </ div >
64- < Input value = { password } onChange = { ( e ) => { setPassword ( e . target . value ) } } id = "password" type = "password" required />
69+ < Input
70+ value = { password }
71+ onChange = { ( e ) => setPassword ( e . target . value ) }
72+ id = "password"
73+ type = "password"
74+ required
75+ />
6576 </ div >
66- < Button type = "submit" className = "w-full" >
67- Login
77+ < Button type = "submit" className = "w-full" disabled = { loading } >
78+ { loading ? (
79+ < >
80+ < Loader2 className = "mr-2 h-4 w-4 animate-spin" />
81+ Logging in...
82+ </ >
83+ ) : (
84+ "Login"
85+ ) }
6886 </ Button >
6987 </ div >
7088 </ form >
7189 </ CardContent >
7290 </ Card >
7391 </ div >
74- )
92+ ) ;
7593}
0 commit comments