mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
refactor: unify backend and frontend servers (#3)
* refactor: unify backend and frontend servers * refactor: correct paths for openapi & drizzle * refactor: move api-client to client * fix: drizzle paths * chore: fix linting issues * fix: form reset issue
This commit is contained in:
26
app/middleware/auth.ts
Normal file
26
app/middleware/auth.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { redirect, type MiddlewareFunction } from "react-router";
|
||||
import { getMe, getStatus } from "~/client/api-client";
|
||||
import { appContext } from "~/context";
|
||||
|
||||
export const authMiddleware: MiddlewareFunction = async ({ context, request }) => {
|
||||
const session = await getMe();
|
||||
|
||||
const isAuthRoute = ["/login", "/onboarding"].includes(new URL(request.url).pathname);
|
||||
|
||||
if (!session.data?.user?.id && !isAuthRoute) {
|
||||
const status = await getStatus();
|
||||
if (!status.data?.hasUsers) {
|
||||
throw redirect("/onboarding");
|
||||
}
|
||||
|
||||
throw redirect("/login");
|
||||
}
|
||||
|
||||
if (session.data?.user?.id) {
|
||||
context.set(appContext, { user: session.data.user, hasUsers: true });
|
||||
|
||||
if (isAuthRoute) {
|
||||
throw redirect("/");
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user