|
|
|
|
@@ -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,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);
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
throw new Error("Unauthenticated");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const from = encodeURIComponent(url.pathname + url.search);
|
|
|
|
|
throw redirect({
|
|
|
|
|
to: "/refresh-session",
|
|
|
|
|
search: { redirect: from }
|
|
|
|
|
});
|
|
|
|
|
logger.info("Session needs refresh - treating as unauthenticated");
|
|
|
|
|
throw new Error("Unauthenticated");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!session.context.userAuthId) {
|
|
|
|
|
@@ -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;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|