"use client"; import { changePasswordSchema, type ChangePasswordFormData } from "@nextsparkjs/core/lib/validation"; import { useAuth } from "@nextsparkjs/core/hooks/useAuth"; import { useUserProfile } from "@nextsparkjs/core/hooks/useUserProfile"; import { zodResolver } from "@hookform/resolvers/zod"; import { AlertCircle, CheckCircle, Loader2 } from "lucide-react"; import { useState, useCallback } from "react"; import { useForm } from "react-hook-form"; import { useRouter } from "next/navigation"; import { Button } from "@nextsparkjs/core/components/ui/button"; import { Label } from "@nextsparkjs/core/components/ui/label"; import { PasswordInput } from "@nextsparkjs/core/components/ui/password-input"; import { Alert, AlertDescription } from "@nextsparkjs/core/components/ui/alert"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@nextsparkjs/core/components/ui/card"; import { Checkbox } from "@nextsparkjs/core/components/ui/checkbox"; import { sel } from '@nextsparkjs/core/selectors'; import { useTranslations } from 'next-intl'; import { getTemplateOrDefaultClient } from '@nextsparkjs/registries/template-registry.client' function UpdatePasswordPage() { const { changePassword, user } = useAuth(); const { hasPassword, isLoading: profileLoading } = useUserProfile(); const router = useRouter(); const t = useTranslations('settings'); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const [statusMessage, setStatusMessage] = useState(''); const form = useForm({ resolver: zodResolver(changePasswordSchema), defaultValues: { currentPassword: "", newPassword: "", confirmPassword: "", revokeOtherSessions: false, }, }); const { register, handleSubmit, watch, formState: { errors }, } = form; const newPassword = watch("newPassword"); const onSubmit = useCallback(async (data: ChangePasswordFormData) => { setLoading(true); setError(null); setStatusMessage(t('password.messages.updating')); try { const result = await changePassword( data.currentPassword, data.newPassword, data.revokeOtherSessions ); if (!result.success) { const errorMsg = result.error || t('password.messages.updateError'); setError(errorMsg); setStatusMessage(`${t('common.status.error')}: ${errorMsg}`); } else { setSuccess(true); setStatusMessage(t('password.messages.updateSuccess')); } } catch { const errorMsg = t('password.messages.unexpectedError'); setError(errorMsg); setStatusMessage(`${t('common.status.error')}: ${errorMsg}`); } finally { setLoading(false); } }, [changePassword, t]); if (!user) { router.push('/login'); return null; } // Redirect Google users - they can't change password if (!profileLoading && !hasPassword) { router.push('/dashboard/settings/profile'); return null; } if (profileLoading) { return (
{t('password.loading.verifying')}
); } if (success) { return ( <> {/* MANDATORY: Screen reader announcements */}
{statusMessage}

{t('password.success.title')}

{t('password.success.description')}

); } return ( <> {/* MANDATORY: Screen reader announcements */}
{statusMessage}
{/* Header */}

{t('password.title')}

{t('password.description')}

{t('password.form.title')} {t('password.form.description')} {error && ( )}
{errors.currentPassword && (

{errors.currentPassword.message}

)}
{errors.newPassword && (

{errors.newPassword.message}

)}
{errors.confirmPassword && (

{errors.confirmPassword.message}

)}
{/* Action Section - Checkbox and Button */}
{loading ? t('password.form.submitHelpUpdating') : t('password.form.submitHelp')}
); } // Opt out of static generation due to client-side state export default getTemplateOrDefaultClient('app/dashboard/settings/password/page.tsx', UpdatePasswordPage)