Merge pull request 'try this' (#21) from development into main
Reviewed-on: #21
This commit is contained in:
@@ -123,16 +123,8 @@ export const Route = createRootRouteWithContext<{
|
||||
playerQueries.auth()
|
||||
);
|
||||
|
||||
console.log('__root beforeLoad auth data:', auth);
|
||||
|
||||
return { auth };
|
||||
} 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 {};
|
||||
}
|
||||
},
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
createServerOnlyFn,
|
||||
} from "@tanstack/react-start";
|
||||
import { getRequest, setResponseHeader } from "@tanstack/react-start/server";
|
||||
import { redirect as redirect } from "@tanstack/react-router";
|
||||
import UserRoles from "supertokens-node/recipe/userroles";
|
||||
import UserMetadata from "supertokens-node/recipe/usermetadata";
|
||||
import { getSessionForStart } from "@/lib/supertokens/recipes/start-session";
|
||||
@@ -48,28 +47,14 @@ 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);
|
||||
|
||||
if (session.context.session?.tryRefresh) {
|
||||
if (options?.isServerFunction) {
|
||||
throw new Error("SESSION_REFRESH_REQUIRED");
|
||||
}
|
||||
|
||||
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");
|
||||
logger.info("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) {
|
||||
logger.error("Unauthenticated user", session.context);
|
||||
throw new Error("Unauthenticated");
|
||||
@@ -109,25 +94,9 @@ export const superTokensFunctionMiddleware = createMiddleware({
|
||||
const request = getRequest();
|
||||
|
||||
try {
|
||||
const context = await getSessionContext(request, { isServerFunction: true });
|
||||
const context = await getSessionContext(request);
|
||||
return next({ context });
|
||||
} 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;
|
||||
}
|
||||
});
|
||||
@@ -138,7 +107,7 @@ export const superTokensAdminFunctionMiddleware = createMiddleware({
|
||||
const request = getRequest();
|
||||
|
||||
try {
|
||||
const context = await getSessionContext(request, { isServerFunction: true });
|
||||
const context = await getSessionContext(request);
|
||||
|
||||
if (context.roles?.includes("Admin")) {
|
||||
return next({ context });
|
||||
@@ -147,22 +116,6 @@ export const superTokensAdminFunctionMiddleware = createMiddleware({
|
||||
logger.error("Unauthorized user in admin function.", context);
|
||||
throw new Error("Unauthorized");
|
||||
} 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;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user