- Custom error classes (lib/errors.ts) and signed-cookie session (lib/auth/session.ts, HMAC-SHA256, httpOnly+sameSite=lax) - UserRepository + AuthService (bcrypt hashing, role/status checks, register/login/logout/getCurrentUser/updateProfile) - Auth helpers (getCurrentUser), user validator, API error handler - API routes: register (auto-login), login, logout, me, PATCH users/me, GET admin/migrations (admin-only guard) - middleware.ts redirects unauthenticated page access to /login - Screens: login (login/register toggle), profile, dashboard skeleton, home redirect - vitest: exclude e2e specs, setupFiles for SESSION_SECRET - playwright: run migrate before dev server, set SESSION_SECRET - Unit tests (UserRepository, AuthService, session), integration auth-flow, e2e auth.spec (register/login/logout/protected/401)
56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
/**
|
|
* アプリケーション全体で使用するカスタムエラー。
|
|
* 各エラーは HTTP ステータスコードを保持し、Route Handler でレスポンスへの変換が容易。
|
|
*/
|
|
|
|
export class AppError extends Error {
|
|
constructor(
|
|
message: string,
|
|
public readonly status: number
|
|
) {
|
|
super(message);
|
|
this.name = this.constructor.name;
|
|
}
|
|
}
|
|
|
|
export class ValidationError extends AppError {
|
|
constructor(
|
|
message: string,
|
|
public readonly field?: string
|
|
) {
|
|
super(message, 400);
|
|
this.name = 'ValidationError';
|
|
}
|
|
}
|
|
|
|
export class UnauthorizedError extends AppError {
|
|
constructor(message: string = '認証が必要です') {
|
|
super(message, 401);
|
|
this.name = 'UnauthorizedError';
|
|
}
|
|
}
|
|
|
|
export class ForbiddenError extends AppError {
|
|
constructor(message: string = 'この操作を行う権限がありません') {
|
|
super(message, 403);
|
|
this.name = 'ForbiddenError';
|
|
}
|
|
}
|
|
|
|
export class NotFoundError extends AppError {
|
|
constructor(
|
|
public readonly resource: string,
|
|
public readonly id: number | string
|
|
) {
|
|
super(`${resource} not found: ${id}`, 404);
|
|
this.name = 'NotFoundError';
|
|
}
|
|
}
|
|
|
|
export class ConflictError extends AppError {
|
|
constructor(message: string) {
|
|
super(message, 409);
|
|
this.name = 'ConflictError';
|
|
}
|
|
}
|