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:
1 parent
ed7b30438e
commit
b186d0f797
35 files changed
+2422
-36
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { FileText, Home, Menu, Settings, X, type LucideIcon } from 'lucide-react';
|
||||
import { FileText, Home, LogOut, Menu, Settings, User, X, type LucideIcon } from 'lucide-react';
|
||||
import { useState } from 'react';
|
||||
import { NavLink } from 'react-router-dom';
|
||||
import { Link, NavLink } from 'react-router-dom';
|
||||
import { useAuth } from '../../auth';
|
||||
import logo from '../../assets/logo.svg';
|
||||
import { cn } from '../../lib/utils';
|
||||
|
||||
@@ -31,6 +32,7 @@ const NAV_ITEMS: NavItem[] = [
|
||||
*/
|
||||
export function Header({ className }: HeaderProps) {
|
||||
const [menuOpen, setMenuOpen] = useState(false);
|
||||
const { username, ready } = useAuth();
|
||||
|
||||
const closeMenu = () => setMenuOpen(false);
|
||||
|
||||
@@ -41,6 +43,35 @@ export function Header({ className }: HeaderProps) {
|
||||
isActive && 'bg-blue-50 text-blue-700 hover:bg-blue-100 hover:text-blue-800',
|
||||
);
|
||||
|
||||
const plainLinkClasses =
|
||||
'inline-flex items-center gap-2 rounded-md px-3 py-2 text-sm font-medium text-slate-600 hover:bg-slate-100 hover:text-slate-900';
|
||||
|
||||
// Auth links: sign-in/register when logged out, account/sign-out when
|
||||
// signed in (rendered once the session restore has settled).
|
||||
const renderAuthLinks = (onClick?: () => void) =>
|
||||
ready &&
|
||||
(username ? (
|
||||
<>
|
||||
<NavLink to="/account" className={linkClasses} onClick={onClick}>
|
||||
<User className="h-5 w-5" aria-hidden="true" />
|
||||
<span>{username}</span>
|
||||
</NavLink>
|
||||
<Link to="/sign-out" className={plainLinkClasses} onClick={onClick}>
|
||||
<LogOut className="h-5 w-5" aria-hidden="true" />
|
||||
<span>Sign out</span>
|
||||
</Link>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Link to="/login" className={plainLinkClasses} onClick={onClick}>
|
||||
<span>Sign in</span>
|
||||
</Link>
|
||||
<Link to="/register" className="btn !px-3 !py-2" onClick={onClick}>
|
||||
<span>Register</span>
|
||||
</Link>
|
||||
</>
|
||||
));
|
||||
|
||||
const renderNav = (id: string, itemClasses?: string) => (
|
||||
<nav id={id} aria-label={`${id === 'mobile-menu' ? 'Mobile' : 'Main'} navigation`} className={itemClasses}>
|
||||
{NAV_ITEMS.map(({ to, label, icon: Icon, end }) => (
|
||||
@@ -73,6 +104,11 @@ export function Header({ className }: HeaderProps) {
|
||||
{/* Desktop navigation */}
|
||||
{renderNav('main-nav', 'hidden items-center gap-1 md:flex')}
|
||||
|
||||
{/* Account (auth) links */}
|
||||
<nav aria-label="Account" className="hidden items-center gap-1 md:flex">
|
||||
{renderAuthLinks()}
|
||||
</nav>
|
||||
|
||||
{/* Mobile menu toggle */}
|
||||
<button
|
||||
type="button"
|
||||
@@ -91,7 +127,14 @@ export function Header({ className }: HeaderProps) {
|
||||
</div>
|
||||
|
||||
{/* Mobile navigation (rendered only while open) */}
|
||||
{menuOpen && renderNav('mobile-menu', 'flex flex-col gap-1 border-t border-slate-200 px-4 pb-4 pt-2 md:hidden')}
|
||||
{menuOpen && (
|
||||
<div className="flex flex-col border-t border-slate-200 px-4 pb-4 pt-2 md:hidden">
|
||||
{renderNav('mobile-menu', 'flex flex-col gap-1')}
|
||||
<div className="mt-1 flex flex-col gap-1 border-t border-slate-200 pt-2">
|
||||
{renderAuthLinks(closeMenu)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</header>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user