- Fix Next.js build failure: remove duplicate route at (dashboard)/listings/[id] that conflicted with (public)/listings/[id] (same URL path in two route groups) - Fix 772 ESLint errors: auto-fix import ordering (import-x/order), remove unused imports/variables, convert empty interfaces to type aliases, replace require() with ESM imports, fix consistent-type-imports violations - Add CLAUDE.md for developer onboarding documentation - All checks pass: 0 lint errors, typecheck clean, 230 tests passing, build success Co-Authored-By: Paperclip <noreply@paperclip.ing>
141 lines
5.1 KiB
TypeScript
141 lines
5.1 KiB
TypeScript
'use client';
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { Loader2 } from 'lucide-react';
|
|
import Link from 'next/link';
|
|
import { useRouter, useSearchParams } from 'next/navigation';
|
|
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 { useAuthStore } from '@/lib/auth-store';
|
|
import { loginSchema, type LoginFormData } from '@/lib/validations/auth';
|
|
|
|
export default function LoginPage() {
|
|
const router = useRouter();
|
|
const searchParams = useSearchParams();
|
|
const { login, isLoading, error, clearError } = useAuthStore();
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
|
|
const oauthError = searchParams.get('error');
|
|
const OAUTH_ERROR_MESSAGES: Record<string, string> = {
|
|
oauth_failed: 'Đăng nhập bằng mạng xã hội thất bại. Vui lòng thử lại.',
|
|
access_denied: 'Bạn đã từ chối quyền truy cập. Vui lòng thử lại.',
|
|
invalid_request: 'Yêu cầu đăng nhập không hợp lệ. Vui lòng thử lại.',
|
|
server_error: 'Lỗi máy chủ. Vui lòng thử lại sau.',
|
|
temporarily_unavailable: 'Dịch vụ tạm thời không khả dụng. Vui lòng thử lại sau.',
|
|
};
|
|
const oauthErrorMessage = oauthError
|
|
? OAUTH_ERROR_MESSAGES[oauthError] ?? 'Đã xảy ra lỗi khi đăng nhập. Vui lòng thử lại.'
|
|
: null;
|
|
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
formState: { errors },
|
|
} = useForm<LoginFormData>({
|
|
resolver: zodResolver(loginSchema),
|
|
});
|
|
|
|
const onSubmit = async (data: LoginFormData) => {
|
|
try {
|
|
await login(data);
|
|
router.push('/');
|
|
} catch {
|
|
// Error is handled by the store
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Card>
|
|
<CardHeader className="space-y-1 text-center">
|
|
<CardTitle className="text-2xl font-bold">Đăng nhập</CardTitle>
|
|
<CardDescription>Nhập số điện thoại và mật khẩu để đăng nhập</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
|
|
{oauthErrorMessage && (
|
|
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive" role="alert">
|
|
{oauthErrorMessage}
|
|
</div>
|
|
)}
|
|
{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">
|
|
Bỏ qua
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="phone">Số điện thoại</Label>
|
|
<Input
|
|
id="phone"
|
|
type="tel"
|
|
placeholder="0912345678"
|
|
autoComplete="tel"
|
|
{...register('phone')}
|
|
aria-invalid={!!errors.phone}
|
|
/>
|
|
{errors.phone && (
|
|
<p className="text-sm text-destructive" role="alert">{errors.phone.message}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<div className="flex items-center justify-between">
|
|
<Label htmlFor="password">Mật khẩu</Label>
|
|
<button
|
|
type="button"
|
|
className="text-xs text-muted-foreground hover:text-primary"
|
|
onClick={() => setShowPassword(!showPassword)}
|
|
>
|
|
{showPassword ? 'Ẩn' : 'Hiện'}
|
|
</button>
|
|
</div>
|
|
<Input
|
|
id="password"
|
|
type={showPassword ? 'text' : 'password'}
|
|
placeholder="Nhập mật khẩu"
|
|
autoComplete="current-password"
|
|
{...register('password')}
|
|
aria-invalid={!!errors.password}
|
|
/>
|
|
{errors.password && (
|
|
<p className="text-sm text-destructive" role="alert">{errors.password.message}</p>
|
|
)}
|
|
</div>
|
|
|
|
<Button type="submit" className="w-full" disabled={isLoading}>
|
|
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
|
Đăng nhập
|
|
</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">Hoặc đăng nhập với</span>
|
|
</div>
|
|
</div>
|
|
|
|
<OAuthButtons />
|
|
</CardContent>
|
|
<CardFooter className="justify-center">
|
|
<p className="text-sm text-muted-foreground">
|
|
Chưa có tài khoản?{' '}
|
|
<Link href="/register" className="font-medium text-primary hover:underline">
|
|
Đăng ký
|
|
</Link>
|
|
</p>
|
|
</CardFooter>
|
|
</Card>
|
|
);
|
|
}
|