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.
143 lines
4.8 KiB
TypeScript
143 lines
4.8 KiB
TypeScript
import { FileText, Home, LogOut, Menu, Settings, User, X, type LucideIcon } from 'lucide-react';
|
|
import { useState } from 'react';
|
|
import { Link, NavLink } from 'react-router-dom';
|
|
import { useAuth } from '../../auth';
|
|
import logo from '../../assets/logo.svg';
|
|
import { cn } from '../../lib/utils';
|
|
|
|
export interface HeaderProps {
|
|
className?: string;
|
|
}
|
|
|
|
interface NavItem {
|
|
to: string;
|
|
label: string;
|
|
icon: LucideIcon;
|
|
/** Match the route exactly (used for "/"). */
|
|
end?: boolean;
|
|
}
|
|
|
|
const NAV_ITEMS: NavItem[] = [
|
|
{ to: '/', label: 'Home', icon: Home, end: true },
|
|
{ to: '/articles', label: 'Articles', icon: FileText },
|
|
{ to: '/categories', label: 'Categories', icon: Settings },
|
|
];
|
|
|
|
/**
|
|
* Main navigation header.
|
|
*
|
|
* Uses react-router-dom <NavLink> (never plain anchors) so navigation stays
|
|
* client-side. Lucide icons mark each item; the mobile menu collapses the
|
|
* same links into a toggleable panel below <md> breakpoints.
|
|
*/
|
|
export function Header({ className }: HeaderProps) {
|
|
const [menuOpen, setMenuOpen] = useState(false);
|
|
const { username, ready } = useAuth();
|
|
|
|
const closeMenu = () => setMenuOpen(false);
|
|
|
|
const linkClasses = ({ isActive }: { isActive: boolean }): string =>
|
|
cn(
|
|
'inline-flex items-center gap-2 rounded-md px-3 py-2 text-sm font-medium transition-colors',
|
|
'text-slate-600 hover:bg-slate-100 hover:text-slate-900',
|
|
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 }) => (
|
|
<NavLink
|
|
key={to}
|
|
to={to}
|
|
end={end}
|
|
className={({ isActive }) => linkClasses({ isActive })}
|
|
onClick={closeMenu}
|
|
>
|
|
<Icon className="h-5 w-5" aria-hidden="true" />
|
|
<span>{label}</span>
|
|
</NavLink>
|
|
))}
|
|
</nav>
|
|
);
|
|
|
|
return (
|
|
<header
|
|
data-testid="site-header"
|
|
className={cn('sticky top-0 z-40 border-b border-slate-200 bg-white/95 backdrop-blur', className)}
|
|
>
|
|
<div className="mx-auto flex max-w-6xl items-center justify-between px-4 py-3 sm:px-6">
|
|
{/* Brand */}
|
|
<NavLink to="/" className="flex items-center gap-2" onClick={closeMenu} aria-label="newzy">
|
|
<img src={logo} alt="" className="h-8 w-8" />
|
|
<span className="text-xl font-bold tracking-tight text-slate-900">newzy</span>
|
|
</NavLink>
|
|
|
|
{/* 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"
|
|
className="inline-flex items-center justify-center rounded-md p-2 text-slate-600 hover:bg-slate-100 hover:text-slate-900 md:hidden"
|
|
aria-label={menuOpen ? 'Close menu' : 'Open menu'}
|
|
aria-expanded={menuOpen}
|
|
aria-controls="mobile-menu"
|
|
onClick={() => setMenuOpen((open) => !open)}
|
|
>
|
|
{menuOpen ? (
|
|
<X className="h-6 w-6" aria-hidden="true" />
|
|
) : (
|
|
<Menu className="h-6 w-6" aria-hidden="true" />
|
|
)}
|
|
</button>
|
|
</div>
|
|
|
|
{/* Mobile navigation (rendered only while open) */}
|
|
{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>
|
|
);
|
|
}
|
|
|
|
export default Header;
|