feat: add JWT authentication with auth pages and tests (M5)

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.
This commit is contained in:
Daniel Samson committed 2026-10-30 17:03:00 +00:00
1 parent ed7b30438e
commit b186d0f797
35 files changed
+2422 -36

No files matched your search

+33 -13
View File
@@ -1,8 +1,13 @@
import { BrowserRouter, Link, Route, Routes } from 'react-router-dom';
import { AuthProvider, RequireAuth } from './auth';
import Layout from './components/ui/Layout';
import Account from './pages/Account';
import ArticlePage from './pages/ArticlePage';
import CategoriesPage from './pages/CategoriesPage';
import Home from './pages/Home';
import Login from './pages/Login';
import Register from './pages/Register';
import SignOut from './pages/SignOut';
/** 404 fallback for unknown routes. */
function NotFound() {
@@ -20,11 +25,13 @@ function NotFound() {
}
/**
* Root component: owns the React Router setup.
* Root component: owns the React Router + auth setup.
*
* BrowserRouter wraps the whole app; Layout provides the shared shell
* (header/footer); Routes maps URLs to page components in src/pages/.
* In-app navigation always uses react-router-dom <Link>/<NavLink>.
* BrowserRouter wraps the whole app; AuthProvider restores the JWT
* session and exposes login/register/logout (see src/auth.tsx); Layout
* provides the shared shell (header/footer); Routes maps URLs to page
* components in src/pages/. In-app navigation always uses
* react-router-dom <Link>/<NavLink>.
*/
export default function App() {
return (
@@ -34,15 +41,28 @@ export default function App() {
v7_relativeSplatPath: true,
}}
>
<Layout>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/articles" element={<ArticlePage />} />
<Route path="/articles/:id" element={<ArticlePage />} />
<Route path="/categories" element={<CategoriesPage />} />
<Route path="*" element={<NotFound />} />
</Routes>
</Layout>
<AuthProvider>
<Layout>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/articles" element={<ArticlePage />} />
<Route path="/articles/:id" element={<ArticlePage />} />
<Route path="/categories" element={<CategoriesPage />} />
<Route path="/login" element={<Login />} />
<Route path="/register" element={<Register />} />
<Route path="/sign-out" element={<SignOut />} />
<Route
path="/account"
element={
<RequireAuth>
<Account />
</RequireAuth>
}
/>
<Route path="*" element={<NotFound />} />
</Routes>
</Layout>
</AuthProvider>
</BrowserRouter>
);
}