import { useState } from 'react'; import { Alert, Button, PasswordInput, Stack, Text, ThemeIcon, Title } from '@mantine/core'; import { IconArrowRight, IconLock, IconLockOpen } from '@tabler/icons-react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { useNavigate } from 'react-router-dom'; import { useApiMutation } from '@ema-platform/api'; import { notify } from '@ema-platform/ui'; import { AuthShell } from '../components/AuthShell'; const schema = z .object({ newPassword: z.string().min(8, { message: 'Password must be at least 8 characters' }), confirmPassword: z.string().min(8, { message: 'Confirm your password' }), }) .refine((data) => data.newPassword === data.confirmPassword, { message: 'Passwords do not match', path: ['confirmPassword'], }); type FormValues = z.infer; export function SetPasswordPage() { const navigate = useNavigate(); const [serverError, setServerError] = useState(null); const [setPasswordTrigger, { isLoading }] = useApiMutation(); const userId = sessionStorage.getItem('fayda_userId'); const userInfoRaw = sessionStorage.getItem('fayda_userInfo'); let userName = 'your account'; if (userInfoRaw) { try { const info = JSON.parse(userInfoRaw); userName = info.name || 'your account'; } catch { // ignore } } const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(schema), }); const onSubmit = async (values: FormValues) => { if (!userId) { notify.error('Session expired. Please sign up again.'); navigate('/signup', { replace: true }); return; } try { await setPasswordTrigger({ url: '/v1/auth/set-fayda-password', method: 'PATCH', body: { userId, newPassword: values.newPassword, confirmPassword: values.confirmPassword, }, }).unwrap(); sessionStorage.removeItem('fayda_userId'); sessionStorage.removeItem('fayda_userInfo'); notify.success('Password set successfully'); navigate('/login', { replace: true }); } catch (err: unknown) { const msg = (err as { data?: { message?: string } })?.data?.message ?? (err instanceof Error ? err.message : 'Something went wrong'); setServerError(msg); notify.error(msg); } }; return (
Set your password Welcome, {userName}. Your National ID has been verified successfully. Choose a password to complete your account setup.
{serverError && ( setServerError(null)}> {serverError} )}
} error={errors.newPassword?.message} {...register('newPassword')} /> } error={errors.confirmPassword?.message} {...register('confirmPassword')} />
); }