feat: add forgot password flow and reusable password field

This commit is contained in:
2026-06-17 16:55:10 +05:30
parent 4c2241ff72
commit b325975282
15 changed files with 541 additions and 44 deletions

View File

@@ -0,0 +1,94 @@
'use client';
import Link from 'next/link';
import { MailCheck, Send } from 'lucide-react';
import { Suspense } from 'react';
import { FormField } from '@/components/form';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { GuestGuard } from '@/guards';
import { useForgotPasswordForm } from '@/hooks/useForgotPasswordForm';
import { ROUTES } from '@/utils/routes';
import { Loader2 } from 'lucide-react';
function ForgotPasswordContent() {
const {
register,
handleSubmit,
errors,
isLoading,
isSubmitted,
isFormSubmitted,
canSubmit,
displayInfo,
} = useForgotPasswordForm();
return (
<GuestGuard>
<div className="flex min-h-screen w-full">
<div className="hidden w-[40%] md:flex" />
<div className="flex h-screen flex-1 items-center justify-center border-l border-border p-6 md:w-[60%]">
<section className="w-full max-w-sm space-y-7">
<header className="space-y-2 text-center md:text-left">
<p className="text-muted-foreground">VisionRoad</p>
<h1>{displayInfo.title}</h1>
<p className="text-muted-foreground">{displayInfo.description}</p>
</header>
{!isSubmitted ? (
<form className="space-y-5" onSubmit={handleSubmit}>
<FormField
id="email"
label="Email"
required
error={isFormSubmitted ? errors.email?.message : undefined}
>
<Input
id="email"
type="email"
placeholder="name@example.com"
autoComplete="email"
disabled={isLoading}
aria-invalid={!!errors.email}
{...register('email')}
/>
</FormField>
<Button
type="submit"
className="w-full"
disabled={isLoading || !canSubmit}
>
{isLoading ? <Loader2 className="size-4 animate-spin" /> : <Send />}
{displayInfo.buttonLabel}
</Button>
<div className="text-center">
<Link className="text-primary" href={ROUTES.LOGIN}>
Back to Login
</Link>
</div>
</form>
) : (
<Button asChild className="w-full">
<Link href={ROUTES.LOGIN}>
<MailCheck />
{displayInfo.buttonLabel}
</Link>
</Button>
)}
</section>
</div>
</div>
</GuestGuard>
);
}
export default function ForgotPasswordPage() {
return (
<Suspense fallback={null}>
<ForgotPasswordContent />
</Suspense>
);
}

View File

@@ -1,10 +1,14 @@
'use client';
import Link from 'next/link';
import { PasswordField } from '@/components/form';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { GuestGuard } from '@/guards';
import { useLoginForm } from '@/hooks/useLoginForm';
import { ROUTES } from '@/utils/routes';
import { Loader2 } from 'lucide-react';
export default function LoginPage() {
@@ -47,25 +51,24 @@ export default function LoginPage() {
)}
</div>
<div className="space-y-2">
<Label htmlFor="password">Password</Label>
<Input
id="password"
type="password"
placeholder="Enter password"
autoComplete="current-password"
disabled={isLoading}
aria-invalid={!!errors.password}
{...register('password', {
required: 'Password is required',
})}
/>
{errors.password && (
<p className="text-sm text-destructive">
{errors.password.message}
</p>
)}
</div>
<PasswordField
id="password"
label="Password"
required
labelEnd={
<Link href={ROUTES.FORGOT_PASSWORD} className="text-primary">
Forgot password?
</Link>
}
placeholder="Enter password"
autoComplete="current-password"
disabled={isLoading}
aria-invalid={!!errors.password}
error={errors.password?.message}
{...register('password', {
required: 'Password is required',
})}
/>
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? (

View File

@@ -0,0 +1,90 @@
'use client';
import { Loader2 } from 'lucide-react';
import { Suspense } from 'react';
import { PasswordField } from '@/components/form';
import { Button } from '@/components/ui/button';
import { GuestGuard } from '@/guards';
import { useSetPasswordForm } from '@/hooks/useSetPasswordForm';
function ResetPasswordContent() {
const {
register,
handleSubmit,
errors,
isLoading,
isSubmitted,
canSubmit,
displayInfo,
hasToken,
} = useSetPasswordForm();
return (
<GuestGuard>
<div className="flex min-h-screen w-full">
<div className="hidden w-[40%] md:flex" />
<div className="flex h-screen flex-1 items-center justify-center border-l border-border p-6 md:w-[60%]">
<section className="w-full max-w-sm space-y-7">
<header className="space-y-2 text-center md:text-left">
<p className="text-muted-foreground">VisionRoad</p>
<h1>{displayInfo.title}</h1>
<p className="text-muted-foreground">{displayInfo.description}</p>
</header>
{!hasToken ? (
<div className="rounded-md border border-destructive/30 bg-destructive/5 p-4 text-destructive">
Password reset token is missing.
</div>
) : (
<form className="space-y-5" onSubmit={handleSubmit}>
<PasswordField
id="password"
label="New Password"
required
error={isSubmitted ? errors.password?.message : undefined}
placeholder="Enter new password"
autoComplete="new-password"
disabled={isLoading}
aria-invalid={!!errors.password}
{...register('password')}
/>
<PasswordField
id="confirm-password"
label="Confirm Password"
required
error={
isSubmitted ? errors.confirmPassword?.message : undefined
}
placeholder="Confirm new password"
autoComplete="new-password"
disabled={isLoading}
aria-invalid={!!errors.confirmPassword}
{...register('confirmPassword')}
/>
<Button
type="submit"
className="w-full"
disabled={isLoading || !canSubmit}
>
{isLoading ? <Loader2 className="size-4 animate-spin" /> : null}
{displayInfo.buttonLabel}
</Button>
</form>
)}
</section>
</div>
</div>
</GuestGuard>
);
}
export default function ResetPasswordPage() {
return (
<Suspense fallback={null}>
<ResetPasswordContent />
</Suspense>
);
}

View File

@@ -0,0 +1 @@
export { default } from '../reset-password/page';

View File

@@ -98,32 +98,34 @@ export function UserSheet({
</FormField>
</div>
<FormField
id="email"
label="Email"
required
error={emailErrorMessage}
>
<Input
<div className="grid gap-4 md:grid-cols-2">
<FormField
id="email"
placeholder="name@example.com"
aria-invalid={!!emailErrorMessage}
{...register('email')}
/>
</FormField>
label="Email"
required
error={emailErrorMessage}
>
<Input
id="email"
placeholder="name@example.com"
aria-invalid={!!emailErrorMessage}
{...register('email')}
/>
</FormField>
<FormField
id="phone-number"
label="Phone Number"
error={phoneErrorMessage}
>
<Input
<FormField
id="phone-number"
placeholder="+919876543210"
aria-invalid={!!phoneErrorMessage}
{...register('phone_number')}
/>
</FormField>
label="Phone Number"
error={phoneErrorMessage}
>
<Input
id="phone-number"
placeholder="+919876543210"
aria-invalid={!!phoneErrorMessage}
{...register('phone_number')}
/>
</FormField>
</div>
<FormField label="Role" required error={roleErrorMessage}>
<RoleCombobox