- User model with email/hashed_password/is_admin/notification_prefs - JWT auth: POST /api/auth/register, /login, /me - First registered user auto-promoted to admin - Migration 0005: users table + user_id FK on follows (clears global follows) - Follows, dashboard, settings, admin endpoints all require authentication - Admin endpoints (settings writes, celery triggers) require is_admin - Frontend: login/register pages, Zustand auth store (localStorage persist) - AuthGuard component gates all app routes, shows app shell only when authed - Sidebar shows user email + logout; Admin nav link visible to admins only - Admin panel (/settings): user list with delete + promote/demote, LLM config, data source settings, and manual celery controls Authored-By: Jack Levy
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { usePathname, useRouter } from "next/navigation";
|
|
import { useAuthStore } from "@/stores/authStore";
|
|
import { Sidebar } from "./Sidebar";
|
|
|
|
const PUBLIC_PATHS = ["/login", "/register"];
|
|
|
|
export function AuthGuard({ children }: { children: React.ReactNode }) {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const token = useAuthStore((s) => s.token);
|
|
// Zustand persist hydrates asynchronously — wait for it before rendering
|
|
const [hydrated, setHydrated] = useState(false);
|
|
|
|
useEffect(() => {
|
|
setHydrated(true);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (!hydrated) return;
|
|
if (!token && !PUBLIC_PATHS.includes(pathname)) {
|
|
router.replace("/login");
|
|
}
|
|
}, [hydrated, token, pathname, router]);
|
|
|
|
if (!hydrated) return null;
|
|
|
|
// Public pages (login/register) render without the app shell
|
|
if (PUBLIC_PATHS.includes(pathname)) {
|
|
return <>{children}</>;
|
|
}
|
|
|
|
// Not logged in yet — blank while redirecting
|
|
if (!token) return null;
|
|
|
|
// Authenticated: render the full app shell
|
|
return (
|
|
<div className="flex h-screen bg-background">
|
|
<Sidebar />
|
|
<main className="flex-1 overflow-auto">
|
|
<div className="container mx-auto px-6 py-6 max-w-7xl">
|
|
{children}
|
|
</div>
|
|
</main>
|
|
</div>
|
|
);
|
|
}
|