mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat: auth client middleware
This commit is contained in:
18
apps/client/app/middleware/auth.ts
Normal file
18
apps/client/app/middleware/auth.ts
Normal 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 });
|
||||
};
|
||||
Reference in New Issue
Block a user