|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React, { useState } from 'react'; |
| 4 | +import { Button } from '@/components/ui/button'; |
| 5 | +import { Input } from '@/components/ui/input'; |
| 6 | +import { Label } from '@/components/ui/label'; |
| 7 | +import { useRouter } from 'next/navigation'; |
| 8 | +import { supabase } from '@/lib/supabaseClient'; |
| 9 | +import Link from 'next/link'; |
| 10 | + |
| 11 | +const LoginForm: React.FC = () => { |
| 12 | + const router = useRouter(); |
| 13 | + const [formData, setFormData] = useState<{ email: string; password: string }>({ |
| 14 | + email: '', |
| 15 | + password: '', |
| 16 | + }); |
| 17 | + |
| 18 | + const [loading, setLoading] = useState(false); |
| 19 | + const [error, setError] = useState<string | null>(null); |
| 20 | + |
| 21 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 22 | + const { name, value } = e.target; |
| 23 | + setFormData((prev) => ({ ...prev, [name]: value })); |
| 24 | + }; |
| 25 | + |
| 26 | + const handleSubmit = async (e: React.FormEvent) => { |
| 27 | + e.preventDefault(); |
| 28 | + setLoading(true); |
| 29 | + setError(null); |
| 30 | + |
| 31 | + const { email, password } = formData; |
| 32 | + |
| 33 | + try { |
| 34 | + // Attempt user login |
| 35 | + const { data, error: loginError } = await supabase.auth.signInWithPassword({ email, password }); |
| 36 | + |
| 37 | + if (loginError) { |
| 38 | + throw new Error(loginError.message); |
| 39 | + } |
| 40 | + |
| 41 | + // Redirect to dashboard if login succeeds |
| 42 | + if (data.session) { |
| 43 | + router.push('/dashboard'); |
| 44 | + } else { |
| 45 | + throw new Error('Unable to retrieve session after login.'); |
| 46 | + } |
| 47 | + } catch (err: any) { |
| 48 | + console.error('Login Error:', err); |
| 49 | + setError(err.message || 'Something went wrong.'); |
| 50 | + } finally { |
| 51 | + setLoading(false); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + return ( |
| 56 | + <main className="w-full h-screen flex flex-col items-center justify-center px-4"> |
| 57 | + <div className="max-w-sm w-full text-gray-600"> |
| 58 | + <div className="p-6 rounded-lg shadow-lg "> |
| 59 | + <h2 className="text-2xl font-bold mb-4">Log In</h2> |
| 60 | + <form onSubmit={handleSubmit}> |
| 61 | + {/* Email Field */} |
| 62 | + <div className="mb-4"> |
| 63 | + <Label htmlFor="email">Email</Label> |
| 64 | + <Input |
| 65 | + id="email" |
| 66 | + name="email" |
| 67 | + type="email" |
| 68 | + placeholder="you@example.com" |
| 69 | + value={formData.email} |
| 70 | + onChange={handleChange} |
| 71 | + required |
| 72 | + /> |
| 73 | + </div> |
| 74 | + |
| 75 | + {/* Password Field */} |
| 76 | + <div className="mb-4"> |
| 77 | + <Label htmlFor="password">Password</Label> |
| 78 | + <Input |
| 79 | + id="password" |
| 80 | + name="password" |
| 81 | + type="password" |
| 82 | + placeholder="********" |
| 83 | + value={formData.password} |
| 84 | + onChange={handleChange} |
| 85 | + required |
| 86 | + /> |
| 87 | + </div> |
| 88 | + |
| 89 | + {/* Error Message */} |
| 90 | + {error && <p className="text-red-500 mb-4">{error}</p>} |
| 91 | + |
| 92 | + {/* Submit Button */} |
| 93 | + <Button type="submit" disabled={loading} className="w-full"> |
| 94 | + {loading ? 'Logging in...' : 'Log In'} |
| 95 | + </Button> |
| 96 | + |
| 97 | + <div className="flex justify-center mt-4"> |
| 98 | + <Link href="/signup" className="text-sm text-gray-500"> |
| 99 | + Don't have an account? |
| 100 | + <span className='hover:underline ms-1'>Sign up</span> |
| 101 | + </Link> |
| 102 | + </div> |
| 103 | + </form> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + </main> |
| 107 | + ); |
| 108 | +}; |
| 109 | + |
| 110 | +export default LoginForm; |
0 commit comments