SessionProvider: centralised state management

this replaces auth-provider, centralising user data

can be extended to keep additional data

allows for user data to propogate components throughout the app

provides useSession and useAuthenticatedSession()
This commit is contained in:
Oliver Bryan
2026-01-09 06:14:09 +00:00
parent 3d963579a3
commit ac0de68d47
13 changed files with 172 additions and 199 deletions

View File

@@ -1,44 +1,29 @@
import type { UserRecord } from "@issue/shared";
import { useEffect, useRef, useState } from "react";
import { useEffect } from "react";
import { useNavigate, useSearchParams } from "react-router-dom";
import Loading from "@/components/loading";
import LogInForm from "@/components/login-form";
import { clearAuth, getServerURL, setCsrfToken } from "@/lib/utils";
import { useSession } from "@/components/session-provider";
export default function Login() {
const navigate = useNavigate();
const [searchParams] = useSearchParams();
const [checking, setChecking] = useState(true);
const checkedRef = useRef(false);
const { user, isLoading } = useSession();
useEffect(() => {
if (checkedRef.current) return;
checkedRef.current = true;
if (!isLoading && user) {
const next = searchParams.get("next") || "/app";
navigate(next, { replace: true });
}
}, [user, isLoading, navigate, searchParams]);
fetch(`${getServerURL()}/auth/me`, {
credentials: "include",
})
.then(async (res) => {
if (res.ok) {
const data = (await res.json()) as { user: UserRecord; csrfToken: string };
setCsrfToken(data.csrfToken);
localStorage.setItem("user", JSON.stringify(data.user));
const next = searchParams.get("next") || "/app";
navigate(next, { replace: true });
} else {
clearAuth();
setChecking(false);
}
})
.catch(() => {
setChecking(false);
});
}, [navigate, searchParams]);
if (checking) {
if (isLoading) {
return <Loading message="Checking authentication" />;
}
if (user) {
return <Loading message="Redirecting" />;
}
return (
<div className="flex flex-col items-center justify-center gap-4 w-full h-[100vh]">
<LogInForm />