Files
ims/app/(auth)/reset-password/page.tsx
T

122 lines
3.8 KiB
TypeScript

'use client'
import { useState, useEffect } from 'react'
import { useRouter } from 'next/navigation'
import Link from 'next/link'
import { createClient } from '@/lib/supabase/client'
export default function ResetPasswordPage() {
const router = useRouter()
const [password, setPassword] = useState('')
const [confirm, setConfirm] = useState('')
const [loading, setLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const [sessionReady, setSessionReady] = useState<boolean | null>(null)
useEffect(() => {
const supabase = createClient()
supabase.auth.getSession().then(({ data: { session } }) => {
setSessionReady(!!session)
})
}, [])
async function handleSubmit(e: React.FormEvent) {
e.preventDefault()
setError(null)
if (password.length < 8) {
setError('Password must be at least 8 characters.')
return
}
if (password !== confirm) {
setError('Passwords do not match.')
return
}
setLoading(true)
const supabase = createClient()
const { error: updateError } = await supabase.auth.updateUser({ password })
setLoading(false)
if (updateError) {
setError('Reset link has expired. Request a new one.')
return
}
router.push('/login?message=password_reset')
}
if (sessionReady === null) {
return (
<div className="flex min-h-screen items-center justify-center bg-gray-50 px-4">
<p className="text-sm text-gray-500">Loading</p>
</div>
)
}
if (!sessionReady) {
return (
<div className="flex min-h-screen items-center justify-center bg-gray-50 px-4">
<div className="w-full max-w-sm text-center space-y-3">
<p className="bg-red-50 border border-red-200 text-red-700 text-sm rounded px-3 py-2">
Reset link has expired or is invalid.
</p>
<Link href="/forgot-password" className="text-sm text-blue-600 hover:underline">
Request a new reset link
</Link>
</div>
</div>
)
}
return (
<div className="flex min-h-screen items-center justify-center bg-gray-50 px-4">
<form onSubmit={handleSubmit} className="flex flex-col gap-4 w-full max-w-sm">
<div className="text-center">
<h1 className="text-2xl font-bold">Set new password</h1>
<p className="text-sm text-gray-500 mt-1">Choose a strong password.</p>
</div>
{error && (
<p className="bg-red-50 border border-red-200 text-red-700 text-sm rounded px-3 py-2">
{error}{' '}
{error.includes('expired') && (
<Link href="/forgot-password" className="underline">
Request new link
</Link>
)}
</p>
)}
<input
type="password"
placeholder="New password"
value={password}
onChange={e => setPassword(e.target.value)}
required
minLength={8}
autoComplete="new-password"
className="border border-gray-300 rounded-md px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
<input
type="password"
placeholder="Confirm new password"
value={confirm}
onChange={e => setConfirm(e.target.value)}
required
autoComplete="new-password"
className="border border-gray-300 rounded-md px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
<button
type="submit"
disabled={loading}
className="bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 rounded-md disabled:opacity-50 transition-colors"
>
{loading ? 'Saving…' : 'Set password'}
</button>
</form>
</div>
)
}