'use client' import { useState } from 'react' import { createClient } from '@/lib/supabase/client' export default function ChangePasswordForm() { const [current, setCurrent] = useState('') const [newPass, setNewPass] = useState('') const [confirm, setConfirm] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [success, setSuccess] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError(null) setSuccess(false) // Client-side validation if (!current || !newPass || !confirm) { setError('All fields are required.') return } if (newPass.length < 8) { setError('Password must be at least 8 characters.') return } if (newPass !== confirm) { setError('Passwords do not match.') return } if (newPass === current) { setError('New password must differ from current password.') return } setLoading(true) const supabase = createClient() // Get current user email for re-auth const { data: { user } } = await supabase.auth.getUser() if (!user?.email) { setError('Session expired, please log in again.') setLoading(false) return } // Re-authenticate with current password const { error: reauthError } = await supabase.auth.signInWithPassword({ email: user.email, password: current, }) if (reauthError) { setError('Current password is incorrect.') setLoading(false) return } // Update to new password const { error: updateError } = await supabase.auth.updateUser({ password: newPass }) if (updateError) { setError(updateError.message) setLoading(false) return } setCurrent('') setNewPass('') setConfirm('') setSuccess(true) setLoading(false) } return (

Change Password

setCurrent(e.target.value)} disabled={loading} autoComplete="current-password" className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent disabled:opacity-50" />
setNewPass(e.target.value)} disabled={loading} autoComplete="new-password" className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent disabled:opacity-50" />
setConfirm(e.target.value)} disabled={loading} autoComplete="new-password" className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent disabled:opacity-50" />
{error &&

{error}

} {success &&

Password changed successfully.

}
) }