try this
This commit is contained in:
@@ -123,16 +123,8 @@ export const Route = createRootRouteWithContext<{
|
|||||||
playerQueries.auth()
|
playerQueries.auth()
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log('__root beforeLoad auth data:', auth);
|
|
||||||
|
|
||||||
return { auth };
|
return { auth };
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error?.options?.to && error?.options?.statusCode) {
|
|
||||||
console.log('__root beforeLoad: Re-throwing redirect', error.options);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.error('__root beforeLoad error:', error);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import {
|
|||||||
createServerOnlyFn,
|
createServerOnlyFn,
|
||||||
} from "@tanstack/react-start";
|
} from "@tanstack/react-start";
|
||||||
import { getRequest, setResponseHeader } from "@tanstack/react-start/server";
|
import { getRequest, setResponseHeader } from "@tanstack/react-start/server";
|
||||||
import { redirect as redirect } from "@tanstack/react-router";
|
|
||||||
import UserRoles from "supertokens-node/recipe/userroles";
|
import UserRoles from "supertokens-node/recipe/userroles";
|
||||||
import UserMetadata from "supertokens-node/recipe/usermetadata";
|
import UserMetadata from "supertokens-node/recipe/usermetadata";
|
||||||
import { getSessionForStart } from "@/lib/supertokens/recipes/start-session";
|
import { getSessionForStart } from "@/lib/supertokens/recipes/start-session";
|
||||||
@@ -48,26 +47,12 @@ const verifySuperTokensSession = async (
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getSessionContext = createServerOnlyFn(async (request: Request, options?: { isServerFunction?: boolean }) => {
|
export const getSessionContext = createServerOnlyFn(async (request: Request) => {
|
||||||
const session = await verifySuperTokensSession(request);
|
const session = await verifySuperTokensSession(request);
|
||||||
|
|
||||||
if (session.context.session?.tryRefresh) {
|
if (session.context.session?.tryRefresh) {
|
||||||
if (options?.isServerFunction) {
|
logger.info("Session needs refresh - treating as unauthenticated");
|
||||||
throw new Error("SESSION_REFRESH_REQUIRED");
|
throw new Error("Unauthenticated");
|
||||||
}
|
|
||||||
|
|
||||||
const url = new URL(request.url);
|
|
||||||
|
|
||||||
if (url.pathname === '/refresh-session') {
|
|
||||||
logger.warn("Already on refresh-session page but session needs refresh - treating as unauthenticated");
|
|
||||||
throw new Error("Unauthenticated");
|
|
||||||
}
|
|
||||||
|
|
||||||
const from = encodeURIComponent(url.pathname + url.search);
|
|
||||||
throw redirect({
|
|
||||||
to: "/refresh-session",
|
|
||||||
search: { redirect: from }
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!session.context.userAuthId) {
|
if (!session.context.userAuthId) {
|
||||||
@@ -109,25 +94,9 @@ export const superTokensFunctionMiddleware = createMiddleware({
|
|||||||
const request = getRequest();
|
const request = getRequest();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const context = await getSessionContext(request, { isServerFunction: true });
|
const context = await getSessionContext(request);
|
||||||
return next({ context });
|
return next({ context });
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.message === "SESSION_REFRESH_REQUIRED") {
|
|
||||||
throw new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
error: "SESSION_REFRESH_REQUIRED",
|
|
||||||
message: "Session needs to be refreshed",
|
|
||||||
shouldRetry: true
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
status: 440,
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"X-Session-Expired": "true"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -138,7 +107,7 @@ export const superTokensAdminFunctionMiddleware = createMiddleware({
|
|||||||
const request = getRequest();
|
const request = getRequest();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const context = await getSessionContext(request, { isServerFunction: true });
|
const context = await getSessionContext(request);
|
||||||
|
|
||||||
if (context.roles?.includes("Admin")) {
|
if (context.roles?.includes("Admin")) {
|
||||||
return next({ context });
|
return next({ context });
|
||||||
@@ -147,22 +116,6 @@ export const superTokensAdminFunctionMiddleware = createMiddleware({
|
|||||||
logger.error("Unauthorized user in admin function.", context);
|
logger.error("Unauthorized user in admin function.", context);
|
||||||
throw new Error("Unauthorized");
|
throw new Error("Unauthorized");
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.message === "SESSION_REFRESH_REQUIRED") {
|
|
||||||
throw new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
error: "SESSION_REFRESH_REQUIRED",
|
|
||||||
message: "Session needs to be refreshed",
|
|
||||||
shouldRetry: true
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
status: 440,
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"X-Session-Expired": "true"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user