Backend (FastAPI): - core/auth.py: PBKDF2-HMAC-SHA256 password hashing (120k iterations, random salt), HS256 JWT creation/verification (7-day expiry, HN_SECRET_KEY via config) and get_current_user Bearer dependency - models/user.py: User model (username unique/indexed, password_hash, created_at); schemas/auth.py: register/login/token/user schemas - api/v1/auth.py: POST /api/v1/auth/register (201), /login, /logout (204), GET /me; 400 on duplicate username, 401 on bad credentials, invalid/expired/unknown tokens - requirements: PyJWT; env templates + docker-compose expose HN_SECRET_KEY (dev default, override in production) - tests: 24 new backend tests (hashing, tokens, all endpoints) Frontend (React + TypeScript): - api/client.ts: AUTH_TOKEN localStorage persistence, automatic Bearer token attachment, 401 clears the stored token, typed login/register/logout/me helpers - auth.tsx: AuthProvider (session restore + login/register/logout) and RequireAuth route guard - pages: Login, Register, SignOut and a protected /account placeholder; header shows sign-in/register or user/sign-out - styles: auth component classes in globals.css - tests: unit tests for the client, provider and pages; Playwright E2E spec covering register/login/logout, session restore, invalid token cleanup and protected-route redirects (API mocked) Note: the milestone's /api/auth/* paths map to the project's established /api/v1/* prefix (see M2); the HN_SECRET_KEY default is newzy-dev-secret-change-me.
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
/**
|
|
* Vitest setup: registers jest-dom matchers, provides a working
|
|
* `localStorage` and cleans up the DOM after every test (React Testing
|
|
* Library auto-cleanup needs this when globals are disabled).
|
|
*/
|
|
import '@testing-library/jest-dom/vitest';
|
|
import { cleanup } from '@testing-library/react';
|
|
import { afterEach } from 'vitest';
|
|
|
|
/**
|
|
* In-memory Storage. Node >= 22 exposes an experimental `localStorage`
|
|
* global that is `undefined` without `--localstorage-file`, and the jsdom
|
|
* environment does not override it — so the auth tests (which persist the
|
|
* JWT under AUTH_TOKEN) get a real storage implementation here.
|
|
*/
|
|
class MemoryStorage implements Storage {
|
|
private store = new Map<string, string>();
|
|
|
|
get length(): number {
|
|
return this.store.size;
|
|
}
|
|
|
|
clear(): void {
|
|
this.store.clear();
|
|
}
|
|
|
|
getItem(key: string): string | null {
|
|
return this.store.has(key) ? this.store.get(key)! : null;
|
|
}
|
|
|
|
key(index: number): string | null {
|
|
return Array.from(this.store.keys())[index] ?? null;
|
|
}
|
|
|
|
removeItem(key: string): void {
|
|
this.store.delete(key);
|
|
}
|
|
|
|
setItem(key: string, value: string): void {
|
|
this.store.set(key, String(value));
|
|
}
|
|
}
|
|
|
|
Object.defineProperty(globalThis, 'localStorage', {
|
|
configurable: true,
|
|
writable: true,
|
|
value: new MemoryStorage(),
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
});
|