1+ 'use client'
2+
3+ import { useState } from 'react'
4+ import { useRouter } from 'next/navigation'
5+ import { Button } from '@/components/ui/button'
6+ import { Alert , AlertDescription } from '@/components/ui/alert'
7+ import { Card , CardContent , CardHeader , CardTitle } from '@/components/ui/card'
8+ import { supabase } from '@/lib/supabaseClient'
9+
10+ export default function ResetPasswordPage ( ) {
11+ const router = useRouter ( )
12+ const [ newPassword , setNewPassword ] = useState ( '' )
13+ const [ confirmPassword , setConfirmPassword ] = useState ( '' )
14+ const [ status , setStatus ] = useState < 'idle' | 'updating' | 'updated' | 'error' > ( 'idle' )
15+ const [ errorMessage , setErrorMessage ] = useState ( '' )
16+
17+ const handleSubmit = async ( e : React . FormEvent ) => {
18+ e . preventDefault ( )
19+ setErrorMessage ( '' )
20+
21+ if ( newPassword !== confirmPassword ) {
22+ setErrorMessage ( 'Passwords do not match.' )
23+ return
24+ }
25+
26+ setStatus ( 'updating' )
27+
28+ // Use the access token to update the user's password
29+ const { data, error } = await supabase . auth . updateUser ( { password : newPassword } )
30+
31+ if ( error ) {
32+ setStatus ( 'error' )
33+ console . error ( 'Error updating password:' , error . message )
34+ } else {
35+ setStatus ( 'updated' )
36+ // Redirect to login or another page after successful password update
37+ setTimeout ( ( ) => {
38+ router . push ( '/auth/signin' )
39+ } , 2000 )
40+ }
41+ }
42+
43+ return (
44+ < main className = "flex min-h-screen items-center justify-center px-4" >
45+ < Card className = "w-full max-w-md rounded-2xl" >
46+ < CardHeader >
47+ < CardTitle className = "text-center text-2xl" > Set New Password</ CardTitle >
48+ </ CardHeader >
49+ < CardContent >
50+ < form onSubmit = { handleSubmit } className = "space-y-4" >
51+ < input
52+ type = "password"
53+ placeholder = "New Password"
54+ value = { newPassword }
55+ onChange = { ( e ) => setNewPassword ( e . target . value ) }
56+ required
57+ className = "w-full p-2 border rounded"
58+ />
59+ < input
60+ type = "password"
61+ placeholder = "Confirm Password"
62+ value = { confirmPassword }
63+ onChange = { ( e ) => setConfirmPassword ( e . target . value ) }
64+ required
65+ className = "w-full p-2 border rounded"
66+ />
67+ { errorMessage && (
68+ < Alert variant = "destructive" className = "mt-2" >
69+ < AlertDescription > { errorMessage } </ AlertDescription >
70+ </ Alert >
71+ ) }
72+ < Button type = "submit" disabled = { status === 'updating' } >
73+ { status === 'updating' ? 'Updating...' : 'Set New Password' }
74+ </ Button >
75+ </ form >
76+ { status === 'updated' && (
77+ < Alert variant = "default" className = "mt-4" >
78+ < AlertDescription > Password updated successfully! Redirecting to sign in...</ AlertDescription >
79+ </ Alert >
80+ ) }
81+ { status === 'error' && (
82+ < Alert variant = "destructive" className = "mt-4" >
83+ < AlertDescription > Failed to update password. Please try again.</ AlertDescription >
84+ </ Alert >
85+ ) }
86+ </ CardContent >
87+ </ Card >
88+ </ main >
89+ )
90+ }
0 commit comments