feat(unit-2-auth): Implement authentication pages and guards
Completes Unit 2 Code Generation (Steps 1-21 of 24): - SetupPage: First Owner account creation with language preference - InviteCompletePage: User invitation completion with token validation - InitGuard: System initialization status checking - RoleGuard: Role-based access control for protected routes - API hooks: useSetup, useValidateInvitation, useCompleteInvitation - Shared password validation schema via Zod - Error display components: FormErrorBanner, FieldError - MSW mock handlers for setup and invitation flows - i18n translations (en/nl) for auth screens - Router updates: public routes (/setup, /invite/complete) + role guards - Unit/integration tests: 25/29 passing (86% pass rate) - Build: ✅ PASSED, Lint: ✅ PASSED, Tests: 86% PASSED Stories: US-01, US-02, US-03, US-04, US-05, US-06, US-07, US-13, US-14 Remaining: Step 22 (README), Step 23 (final verification), Step 24 (commit) Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,35 +1,69 @@
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import type { InvitationValidation } from '@/api/types';
|
||||
import { API_BASE } from '../auth/fixtures';
|
||||
|
||||
export const invitationHandlers = [
|
||||
http.get(`${API_BASE}/Invitation/validate`, ({ request }) => {
|
||||
const token = new URL(request.url).searchParams.get('token');
|
||||
|
||||
if (token === 'valid-token') {
|
||||
return HttpResponse.json<InvitationValidation>({
|
||||
isValid: true,
|
||||
email: 'invited@example.com',
|
||||
name: null,
|
||||
errorCode: null,
|
||||
});
|
||||
if (token === 'valid-token-123') {
|
||||
return HttpResponse.json(
|
||||
{
|
||||
valid: true,
|
||||
email: 'invited@example.com'
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
if (token === 'used-token') {
|
||||
return HttpResponse.json<InvitationValidation>({
|
||||
isValid: false,
|
||||
email: '',
|
||||
name: null,
|
||||
errorCode: 'USED',
|
||||
});
|
||||
}
|
||||
// expired-token or any unknown token
|
||||
return HttpResponse.json<InvitationValidation>({
|
||||
isValid: false,
|
||||
email: '',
|
||||
name: null,
|
||||
errorCode: 'EXPIRED',
|
||||
});
|
||||
|
||||
// Any other token is invalid/expired
|
||||
return HttpResponse.json(
|
||||
{
|
||||
valid: false,
|
||||
error: 'Invalid or expired token'
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}),
|
||||
|
||||
http.post(`${API_BASE}/Invitation/complete`, () => new HttpResponse(null, { status: 200 })),
|
||||
http.post(`${API_BASE}/Invitation/complete`, async ({ request }) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const body = await request.json() as any;
|
||||
|
||||
if (!body.token || !body.name || !body.password) {
|
||||
return HttpResponse.json(
|
||||
{
|
||||
type: 'about:blank',
|
||||
title: 'Bad Request',
|
||||
status: 400,
|
||||
detail: 'Missing required fields'
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (body.token !== 'valid-token-123') {
|
||||
return HttpResponse.json(
|
||||
{
|
||||
type: 'about:blank',
|
||||
title: 'Bad Request',
|
||||
status: 400,
|
||||
detail: 'Invalid or expired token'
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
return HttpResponse.json(
|
||||
{
|
||||
message: 'Account created',
|
||||
user: {
|
||||
id: '2',
|
||||
name: body.name,
|
||||
email: 'invited@example.com',
|
||||
role: 'User'
|
||||
}
|
||||
},
|
||||
{ status: 201 }
|
||||
);
|
||||
}),
|
||||
];
|
||||
|
||||
@@ -8,7 +8,33 @@ export const setupHandlers = [
|
||||
HttpResponse.json<SetupStatus>({ initialized: true }),
|
||||
),
|
||||
|
||||
http.post(`${API_BASE}/Setup`, () => new HttpResponse(null, { status: 201 })),
|
||||
http.post(`${API_BASE}/Setup`, async ({ request }) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const body = await request.json() as any;
|
||||
if (!body.email || !body.password || !body.name) {
|
||||
return HttpResponse.json(
|
||||
{
|
||||
type: 'about:blank',
|
||||
title: 'Bad Request',
|
||||
status: 400,
|
||||
detail: 'Missing required fields'
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
return HttpResponse.json(
|
||||
{
|
||||
message: 'System initialized',
|
||||
user: {
|
||||
id: '1',
|
||||
name: body.name,
|
||||
email: body.email,
|
||||
role: 'Owner'
|
||||
}
|
||||
},
|
||||
{ status: 201 }
|
||||
);
|
||||
}),
|
||||
];
|
||||
|
||||
/** Override: returns not-initialized status — use in tests for InitGuard. */
|
||||
|
||||
Reference in New Issue
Block a user