Files
goodgo-platform/apps/web/app/[locale]/(auth)/register/page.tsx
Ho Ngoc Hai 7195064f12 feat(web): add i18n locale routes and language switcher component
Add locale-prefixed routes for admin, auth, dashboard, and public pages.
Add error, loading, and not-found pages for locale context. Add language
switcher UI component for Vietnamese/English toggle.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-09 09:44:18 +07:00

181 lines
6.7 KiB
TypeScript

'use client';
import { zodResolver } from '@hookform/resolvers/zod';
import { Loader2 } from 'lucide-react';
import { useRouter } from 'next/navigation';
import { useTranslations } from 'next-intl';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { OAuthButtons } from '@/components/auth/oauth-buttons';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Link } from '@/i18n/navigation';
import { useAuthStore } from '@/lib/auth-store';
import { registerSchema, type RegisterFormData } from '@/lib/validations/auth';
export default function RegisterPage() {
const router = useRouter();
const { register: registerUser, isLoading, error, clearError } = useAuthStore();
const [showPassword, setShowPassword] = useState(false);
const t = useTranslations('auth');
const {
register,
handleSubmit,
formState: { errors },
} = useForm<RegisterFormData>({
resolver: zodResolver(registerSchema),
});
const onSubmit = async (data: RegisterFormData) => {
try {
await registerUser({
phone: data.phone,
password: data.password,
fullName: data.fullName,
email: data.email || undefined,
});
router.push('/');
} catch {
// Error is handled by the store
}
};
return (
<Card>
<CardHeader className="space-y-1 text-center">
<CardTitle className="text-2xl font-bold">{t('registerTitle')}</CardTitle>
<CardDescription>{t('registerDescription')}</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4" noValidate>
{error && (
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive" role="alert">
{error}
<button type="button" onClick={clearError} className="ml-2 font-medium underline">
{t('dismiss')}
</button>
</div>
)}
<div className="space-y-2">
<Label htmlFor="fullName">{t('fullName')}</Label>
<Input
id="fullName"
type="text"
placeholder={t('fullNamePlaceholder')}
autoComplete="name"
aria-describedby={errors.fullName ? 'fullName-error' : undefined}
aria-invalid={!!errors.fullName}
{...register('fullName')}
/>
{errors.fullName && (
<p id="fullName-error" className="text-sm text-destructive" role="alert">{errors.fullName.message}</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="phone">{t('phone')}</Label>
<Input
id="phone"
type="tel"
placeholder={t('phonePlaceholder')}
autoComplete="tel"
aria-describedby={errors.phone ? 'phone-error' : undefined}
aria-invalid={!!errors.phone}
{...register('phone')}
/>
{errors.phone && (
<p id="phone-error" className="text-sm text-destructive" role="alert">{errors.phone.message}</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="email">{t('email')}</Label>
<Input
id="email"
type="email"
placeholder={t('emailPlaceholder')}
autoComplete="email"
aria-describedby={errors.email ? 'email-error' : undefined}
aria-invalid={!!errors.email}
{...register('email')}
/>
{errors.email && (
<p id="email-error" className="text-sm text-destructive" role="alert">{errors.email.message}</p>
)}
</div>
<div className="space-y-2">
<div className="flex items-center justify-between">
<Label htmlFor="password">{t('password')}</Label>
<button
type="button"
className="text-xs text-muted-foreground hover:text-primary"
onClick={() => setShowPassword(!showPassword)}
aria-label={showPassword ? t('hidePassword') : t('showPassword')}
>
{showPassword ? t('hidePassword') : t('showPassword')}
</button>
</div>
<Input
id="password"
type={showPassword ? 'text' : 'password'}
placeholder={t('passwordPlaceholder')}
autoComplete="new-password"
aria-describedby={errors.password ? 'password-error' : undefined}
aria-invalid={!!errors.password}
{...register('password')}
/>
{errors.password && (
<p id="password-error" className="text-sm text-destructive" role="alert">{errors.password.message}</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="confirmPassword">{t('confirmPassword')}</Label>
<Input
id="confirmPassword"
type={showPassword ? 'text' : 'password'}
placeholder={t('confirmPasswordPlaceholder')}
autoComplete="new-password"
aria-describedby={errors.confirmPassword ? 'confirmPassword-error' : undefined}
aria-invalid={!!errors.confirmPassword}
{...register('confirmPassword')}
/>
{errors.confirmPassword && (
<p id="confirmPassword-error" className="text-sm text-destructive" role="alert">{errors.confirmPassword.message}</p>
)}
</div>
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" aria-hidden="true" />}
{t('registerButton')}
</Button>
</form>
<div className="relative my-4">
<div className="absolute inset-0 flex items-center">
<span className="w-full border-t" />
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-card px-2 text-muted-foreground">{t('orRegisterWith')}</span>
</div>
</div>
<OAuthButtons />
</CardContent>
<CardFooter className="justify-center">
<p className="text-sm text-muted-foreground">
{t('hasAccount')}{' '}
<Link href="/login" className="font-medium text-primary hover:underline">
{t('loginLink')}
</Link>
</p>
</CardFooter>
</Card>
);
}