Some checks failed
CI / Lint → Typecheck → Test → Build (22) (push) Failing after 16s
CI / E2E Tests (push) Has been skipped
CodeQL Analysis / CodeQL (javascript-typescript) (push) Failing after 50s
Deploy / Build API Image (push) Failing after 25s
Deploy / Build Web Image (push) Failing after 11s
Deploy / Build AI Services Image (push) Failing after 10s
E2E Tests / Playwright E2E (push) Failing after 12s
Security Scanning / Dependency Audit (pnpm) (push) Failing after 4s
Security Scanning / Trivy Scan — API Image (push) Failing after 1m16s
Security Scanning / Trivy Scan — Web Image (push) Failing after 1m2s
Security Scanning / Trivy Scan — AI Services Image (push) Failing after 50s
Security Scanning / Trivy Filesystem Scan (push) Failing after 38s
Deploy / Deploy to Staging (push) Has been skipped
Deploy / Smoke Test Staging (push) Has been skipped
Deploy / Deploy to Production (push) Has been skipped
Deploy / Smoke Test Production (push) Has been skipped
Security Scanning / Security Gate (push) Failing after 0s
Deploy / Rollback Production (push) Has been skipped
Deploy / Rollback Staging (push) Failing after 10m50s
Two new B2B roles for CĐT (project developers) and KCN operators, provisioned by
admin. Each account owns a subset of ProjectDevelopment / IndustrialPark records
and can CRUD them from the dashboard; admin retains full access.
Phase 1 — Schema
- Extend UserRole enum with DEVELOPER + PARK_OPERATOR (before ADMIN)
- ProjectDevelopment.ownerId FK (User, ON DELETE SET NULL) + index
- IndustrialPark.ownerId FK + index
- Migration 20260420030000
Phase 2a — Backend authorization
- CreateProjectCommand + CreateIndustrialParkCommand accept ownerId; controllers
auto-set it to the caller's user id when role=DEVELOPER / PARK_OPERATOR
- Update + Delete commands gain (requesterUserId, requesterRole) and enforce
ADMIN-or-owner via ForbiddenException; reassigning ownerId is admin-only
- Search params gain optional ownerId filter wired through Prisma repos
- New endpoints: GET /projects/mine/list, GET /industrial/parks/mine/list
- user-rate-limit guard: add DEVELOPER + PARK_OPERATOR entries (300/window)
Phase 2b — Admin provision
- ProvisionDeveloperCommand/Handler: create user (role=DEVELOPER), pre-validate
target projects have no existing owner, batch-assign ownerId
- ProvisionParkOperatorCommand/Handler: same for PARK_OPERATOR + IndustrialPark
- POST /admin/accounts/developers, POST /admin/accounts/park-operators (admin-only)
- DTOs with phone/password/fullName/email + optional {project,park}Ids[]
Phase 2c — Project stats for developer dashboard
- GetProjectStatsQuery + handler: aggregates linkedListingCount, activeListingCount,
totalInquiries, unreadInquiries, savedByUsers via Property → Listing → Inquiry chain
- GET /projects/:id/stats — admin sees all, DEVELOPER only their own (403 otherwise)
Phase 3 — Frontend
- Dashboard layout role-aware: DEVELOPER sees "Dự án của tôi" + CRM + Profile (hides
listings/analytics/subscription); PARK_OPERATOR sees "KCN của tôi" equivalent
- /projects dashboard page switches to duAnApi.searchMine() when role=DEVELOPER
- /industrial-parks page switches to industrialApi.searchMine() when role=PARK_OPERATOR
- Admin nav gains "Tài khoản CĐT" + "Tài khoản KCN" entries
- New pages /admin/accounts/developers + /admin/accounts/park-operators with
checkbox-based multi-select for linking entities
- adminApi.provisionDeveloper + provisionParkOperator + types
- duAnApi.searchMine + getStats; industrialApi.searchMine
- Login demo accounts list includes CĐT Vingroup + KCN VSIP
Phase 4 — Seed (prisma/seed-b2b-accounts.ts)
- DEVELOPER "CĐT Vingroup" (+84912000001) owns 4 projects
- DEVELOPER "CĐT Masterise Homes" (+84912000003) owns 2 projects
- PARK_OPERATOR "Vận hành KCN VSIP" (+84912000002) owns 2 seeded KCN
- Password Velik@2026 for all
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
207 lines
8.3 KiB
TypeScript
207 lines
8.3 KiB
TypeScript
'use client';
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { ChevronDown, Loader2, Sparkles } from 'lucide-react';
|
|
import { useRouter, useSearchParams } 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 { Badge } from '@/components/ui/badge';
|
|
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 { loginSchema, type LoginFormData } from '@/lib/validations/auth';
|
|
|
|
const DEMO_PASSWORD = 'Velik@2026';
|
|
|
|
const DEMO_ACCOUNTS: {
|
|
phone: string;
|
|
name: string;
|
|
role: 'ADMIN' | 'AGENT' | 'SELLER' | 'BUYER' | 'DEVELOPER' | 'PARK_OPERATOR';
|
|
badgeClass: string;
|
|
}[] = [
|
|
{ phone: '+84876677771', name: 'Hồ Ngọc Hải', role: 'ADMIN', badgeClass: 'bg-red-500/10 text-red-600 border-red-500/20' },
|
|
{ phone: '+84900000002', name: 'Nguyễn Văn An', role: 'AGENT', badgeClass: 'bg-blue-500/10 text-blue-600 border-blue-500/20' },
|
|
{ phone: '+84900000005', name: 'Phạm Đức Dũng', role: 'SELLER', badgeClass: 'bg-amber-500/10 text-amber-600 border-amber-500/20' },
|
|
{ phone: '+84900000004', name: 'Lê Minh Cường', role: 'BUYER', badgeClass: 'bg-emerald-500/10 text-emerald-600 border-emerald-500/20' },
|
|
{ phone: '+84912000001', name: 'CĐT Vingroup', role: 'DEVELOPER', badgeClass: 'bg-violet-500/10 text-violet-600 border-violet-500/20' },
|
|
{ phone: '+84912000002', name: 'KCN VSIP', role: 'PARK_OPERATOR', badgeClass: 'bg-cyan-500/10 text-cyan-600 border-cyan-500/20' },
|
|
];
|
|
|
|
export default function LoginPage() {
|
|
const router = useRouter();
|
|
const searchParams = useSearchParams();
|
|
const { login, isLoading, error, clearError } = useAuthStore();
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
const [demoOpen, setDemoOpen] = useState(true);
|
|
const t = useTranslations('auth');
|
|
|
|
const oauthError = searchParams.get('error');
|
|
const oauthErrorMessage = oauthError
|
|
? t(`oauthErrors.${oauthError}` as Parameters<typeof t>[0]) ?? t('oauthErrors.default')
|
|
: null;
|
|
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
setValue,
|
|
formState: { errors },
|
|
} = useForm<LoginFormData>({
|
|
resolver: zodResolver(loginSchema),
|
|
});
|
|
|
|
const fillDemoAccount = (phone: string) => {
|
|
setValue('phone', phone, { shouldValidate: true });
|
|
setValue('password', DEMO_PASSWORD, { shouldValidate: true });
|
|
clearError();
|
|
};
|
|
|
|
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">{t('loginTitle')}</CardTitle>
|
|
<CardDescription>{t('loginDescription')}</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
{/* Demo accounts panel — MVP only */}
|
|
<div className="mb-4 rounded-lg border border-primary/20 bg-primary/5">
|
|
<button
|
|
type="button"
|
|
onClick={() => setDemoOpen(!demoOpen)}
|
|
className="flex w-full items-center justify-between px-3 py-2 text-sm font-medium"
|
|
aria-expanded={demoOpen}
|
|
>
|
|
<span className="inline-flex items-center gap-2">
|
|
<Sparkles className="h-4 w-4 text-primary" aria-hidden="true" />
|
|
{t('demoAccountsTitle')}
|
|
</span>
|
|
<ChevronDown
|
|
className={`h-4 w-4 text-muted-foreground transition-transform ${demoOpen ? 'rotate-180' : ''}`}
|
|
aria-hidden="true"
|
|
/>
|
|
</button>
|
|
{demoOpen && (
|
|
<div className="space-y-2 border-t border-primary/20 p-3">
|
|
<p className="text-xs text-muted-foreground">
|
|
{t('demoAccountsHint')} <code className="rounded bg-muted px-1 py-0.5 font-mono text-xs">{DEMO_PASSWORD}</code>
|
|
</p>
|
|
<ul className="flex flex-col gap-1.5">
|
|
{DEMO_ACCOUNTS.map((acc) => (
|
|
<li key={acc.phone}>
|
|
<button
|
|
type="button"
|
|
onClick={() => fillDemoAccount(acc.phone)}
|
|
className="flex w-full items-center gap-2 rounded-md border bg-card px-2.5 py-1.5 text-left text-xs transition-colors hover:border-primary/40 hover:bg-primary/5"
|
|
>
|
|
<Badge variant="outline" className={`shrink-0 ${acc.badgeClass}`}>
|
|
{acc.role}
|
|
</Badge>
|
|
<span className="flex-1 truncate font-medium">{acc.name}</span>
|
|
<span className="font-mono text-muted-foreground">{acc.phone}</span>
|
|
</button>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4" noValidate>
|
|
{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">
|
|
{t('dismiss')}
|
|
</button>
|
|
</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">
|
|
<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="current-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>
|
|
|
|
<Button type="submit" className="w-full" disabled={isLoading}>
|
|
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" aria-hidden="true" />}
|
|
{t('loginButton')}
|
|
</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('orLoginWith')}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<OAuthButtons />
|
|
</CardContent>
|
|
<CardFooter className="justify-center">
|
|
<p className="text-sm text-muted-foreground">
|
|
{t('noAccount')}{' '}
|
|
<Link href="/register" className="font-medium text-primary hover:underline">
|
|
{t('registerLink')}
|
|
</Link>
|
|
</p>
|
|
</CardFooter>
|
|
</Card>
|
|
);
|
|
}
|