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 [ status , setStatus ] = useState < 'idle' | 'updating' | 'updated' | 'error' > ( 'idle' )
14
+
15
+ const handleSubmit = async ( e : React . FormEvent ) => {
16
+ e . preventDefault ( )
17
+ setStatus ( 'updating' )
18
+
19
+ // Use the access token to update the user's password
20
+ const { data, error } = await supabase . auth . updateUser ( { password : newPassword } )
21
+
22
+ if ( error ) {
23
+ setStatus ( 'error' )
24
+ console . error ( 'Error updating password:' , error . message )
25
+ } else {
26
+ setStatus ( 'updated' )
27
+ // Redirect to login or another page after successful password update
28
+ setTimeout ( ( ) => {
29
+ router . push ( '/auth/signin' )
30
+ } , 2000 )
31
+ }
32
+ }
33
+
34
+ return (
35
+ < main className = "flex min-h-screen items-center justify-center px-4" >
36
+ < Card className = "w-full max-w-md rounded-2xl" >
37
+ < CardHeader >
38
+ < CardTitle className = "text-center text-2xl" > Set New Password</ CardTitle >
39
+ </ CardHeader >
40
+ < CardContent >
41
+ < form onSubmit = { handleSubmit } className = "space-y-4" >
42
+ < input
43
+ type = "password"
44
+ placeholder = "New Password"
45
+ value = { newPassword }
46
+ onChange = { ( e ) => setNewPassword ( e . target . value ) }
47
+ required
48
+ className = "w-full p-2 border rounded"
49
+ />
50
+ < Button type = "submit" disabled = { status === 'updating' } >
51
+ { status === 'updating' ? 'Updating...' : 'Set New Password' }
52
+ </ Button >
53
+ </ form >
54
+ { status === 'updated' && (
55
+ < Alert variant = "default" className = "mt-4" >
56
+ < AlertDescription > Password updated successfully! Redirecting to sign in...</ AlertDescription >
57
+ </ Alert >
58
+ ) }
59
+ { status === 'error' && (
60
+ < Alert variant = "destructive" className = "mt-4" >
61
+ < AlertDescription > Failed to update password. Please try again.</ AlertDescription >
62
+ </ Alert >
63
+ ) }
64
+ </ CardContent >
65
+ </ Card >
66
+ </ main >
67
+ )
68
+ }
0 commit comments