feat: auth client middleware

This commit is contained in:
Nicolas Meienberger
2025-10-02 21:28:08 +02:00
parent 0120641e3a
commit 2be7e18ab5
15 changed files with 368 additions and 46 deletions

View File

@@ -0,0 +1,18 @@
import { redirect, type MiddlewareFunction } from "react-router";
import { getMe, getStatus } from "~/api-client";
import { appContext } from "~/context";
export const authMiddleware: MiddlewareFunction = async ({ context }) => {
const session = await getMe();
if (!session.data?.user.id) {
const status = await getStatus();
if (!status.data?.hasUsers) {
throw redirect("/register");
}
throw redirect("/login");
}
context.set(appContext, { user: session.data.user, hasUsers: true });
};