'use client'; import { Suspense, useState } from 'react'; import Link from 'next/link'; import { useRouter, useSearchParams } from 'next/navigation'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { toast } from 'sonner'; import { cn } from '@/lib/utils'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { BrandedAuthShell } from '@/components/shared/branded-auth-shell'; const MIN_LENGTH = 9; const passwordSchema = z .object({ password: z.string().min(MIN_LENGTH, `Must be at least ${MIN_LENGTH} characters`), confirmPassword: z.string().min(1, 'Please confirm your password'), }) .refine((data) => data.password === data.confirmPassword, { message: 'Passwords do not match', path: ['confirmPassword'], }); type SetPasswordFormData = z.infer; function SetPasswordInner() { const router = useRouter(); const searchParams = useSearchParams(); const token = searchParams.get('token'); const [isLoading, setIsLoading] = useState(false); const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(passwordSchema), }); async function onSubmit(data: SetPasswordFormData) { if (!token) { toast.error('Invalid or missing reset token. Please request a new link.'); return; } setIsLoading(true); try { const response = await fetch('/api/auth/set-password', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ token, password: data.password }), }); if (!response.ok) { const body = await response.json().catch(() => ({})); toast.error(body.message ?? body.error ?? 'Failed to set password. Please try again.'); return; } toast.success('Password set successfully. You can now sign in.'); router.push('/login'); } catch { toast.error('Something went wrong. Please try again.'); } finally { setIsLoading(false); } } if (!token) { return (

Link is missing or invalid

Please use the link from the email we sent you. If the link is broken, ask your administrator for a new one.

Back to sign in
); } return (

Set your password

Choose a password for your CRM account

At least {MIN_LENGTH} characters.

{errors.password &&

{errors.password.message}

}
{errors.confirmPassword && (

{errors.confirmPassword.message}

)}
); } export default function SetPasswordPage() { return ( {null}}> ); }