auth fix?
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
|||||||
Outlet,
|
Outlet,
|
||||||
Scripts,
|
Scripts,
|
||||||
createRootRouteWithContext,
|
createRootRouteWithContext,
|
||||||
|
isRedirect,
|
||||||
} from "@tanstack/react-router";
|
} from "@tanstack/react-router";
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { DefaultCatchBoundary } from "@/components/DefaultCatchBoundary";
|
import { DefaultCatchBoundary } from "@/components/DefaultCatchBoundary";
|
||||||
@@ -124,6 +125,8 @@ export const Route = createRootRouteWithContext<{
|
|||||||
);
|
);
|
||||||
return { auth };
|
return { auth };
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
if (isRedirect(error) || error instanceof Response) throw error;
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
const { doesSessionExist, attemptRefreshingSession } = await import('supertokens-web-js/recipe/session');
|
const { doesSessionExist, attemptRefreshingSession } = await import('supertokens-web-js/recipe/session');
|
||||||
|
|
||||||
|
|||||||
@@ -29,9 +29,16 @@ function RouteComponent() {
|
|||||||
const urlParams = new URLSearchParams(window.location.search);
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
const redirect = urlParams.get('redirect');
|
const redirect = urlParams.get('redirect');
|
||||||
|
|
||||||
if (redirect && !redirect.includes('_serverFn') && !redirect.includes('/api/')) {
|
const safe =
|
||||||
|
redirect &&
|
||||||
|
redirect.startsWith('/') &&
|
||||||
|
!redirect.startsWith('/refresh-session') &&
|
||||||
|
!redirect.includes('_serverFn') &&
|
||||||
|
!redirect.includes('/api/');
|
||||||
|
|
||||||
|
if (safe) {
|
||||||
logger.info("Refresh session route: redirecting to", redirect);
|
logger.info("Refresh session route: redirecting to", redirect);
|
||||||
window.location.href = decodeURIComponent(redirect);
|
window.location.href = redirect;
|
||||||
} else {
|
} else {
|
||||||
logger.info("Refresh session route: redirecting to home");
|
logger.info("Refresh session route: redirecting to home");
|
||||||
window.location.href = '/';
|
window.location.href = '/';
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { setUserMetadata, superTokensFunctionMiddleware, getSessionContext } from "@/utils/supertokens";
|
import { setUserMetadata, superTokensFunctionMiddleware, getSessionContext } from "@/utils/supertokens";
|
||||||
import { createServerFn } from "@tanstack/react-start";
|
import { createServerFn } from "@tanstack/react-start";
|
||||||
|
import { isRedirect } from "@tanstack/react-router";
|
||||||
import { Player, playerInputSchema, playerUpdateSchema, PlayerStats } from "@/features/players/types";
|
import { Player, playerInputSchema, playerUpdateSchema, PlayerStats } from "@/features/players/types";
|
||||||
import { Match } from "@/features/matches/types";
|
import { Match } from "@/features/matches/types";
|
||||||
import { pbAdmin } from "@/lib/pocketbase/client";
|
import { pbAdmin } from "@/lib/pocketbase/client";
|
||||||
@@ -26,13 +27,8 @@ export const fetchMe = createServerFn()
|
|||||||
phone: context.phone
|
phone: context.phone
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
// logger.info("FetchMe: Session error", error)
|
if (isRedirect(error) || error instanceof Response) throw error;
|
||||||
if (error?.response?.status === 401) {
|
|
||||||
const errorData = error?.response?.data;
|
|
||||||
if (errorData?.error === "SESSION_REFRESH_REQUIRED") {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return { user: undefined, roles: [], metadata: {}, phone: undefined };
|
return { user: undefined, roles: [], metadata: {}, phone: undefined };
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export const backendConfig = (): TypeInput => {
|
|||||||
cookieSameSite: "lax",
|
cookieSameSite: "lax",
|
||||||
cookieSecure: process.env.NODE_ENV === "production",
|
cookieSecure: process.env.NODE_ENV === "production",
|
||||||
cookieDomain: process.env.COOKIE_DOMAIN || undefined,
|
cookieDomain: process.env.COOKIE_DOMAIN || undefined,
|
||||||
olderCookieDomain: undefined,
|
olderCookieDomain: process.env.OLDER_COOKIE_DOMAIN ?? "",
|
||||||
antiCsrf: process.env.NODE_ENV === "production" ? "VIA_TOKEN" : "NONE",
|
antiCsrf: process.env.NODE_ENV === "production" ? "VIA_TOKEN" : "NONE",
|
||||||
|
|
||||||
// Debug only
|
// Debug only
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { logger } from "../../logger";
|
|||||||
import { ErrorType, ServerError, ServerResult } from "../types";
|
import { ErrorType, ServerError, ServerResult } from "../types";
|
||||||
import { pbAdmin } from "../../pocketbase/client";
|
import { pbAdmin } from "../../pocketbase/client";
|
||||||
import { getRequest } from "@tanstack/react-start/server";
|
import { getRequest } from "@tanstack/react-start/server";
|
||||||
|
import { isRedirect } from "@tanstack/react-router";
|
||||||
|
|
||||||
export const createServerError = (
|
export const createServerError = (
|
||||||
type: ErrorType,
|
type: ErrorType,
|
||||||
@@ -26,6 +27,8 @@ export const toServerResult = async <T>(
|
|||||||
const data = await serverFn();
|
const data = await serverFn();
|
||||||
return { success: true, data };
|
return { success: true, data };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (isRedirect(error) || error instanceof Response) throw error;
|
||||||
|
|
||||||
const duration = Date.now() - startTime;
|
const duration = Date.now() - startTime;
|
||||||
logger.error('Server Fn Error', error);
|
logger.error('Server Fn Error', error);
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import { serverFnLoggingMiddleware } from "./activities";
|
|||||||
import { pbAdmin } from "@/lib/pocketbase/client";
|
import { pbAdmin } from "@/lib/pocketbase/client";
|
||||||
const logger = new Logger("Middleware");
|
const logger = new Logger("Middleware");
|
||||||
|
|
||||||
|
export const PUBLIC_ROUTES = ["/login", "/logout", "/refresh-session"];
|
||||||
|
|
||||||
const verifySuperTokensSession = async (
|
const verifySuperTokensSession = async (
|
||||||
request: Request
|
request: Request
|
||||||
) => {
|
) => {
|
||||||
@@ -57,10 +59,16 @@ export const getSessionContext = createServerOnlyFn(async (request: Request, opt
|
|||||||
}
|
}
|
||||||
|
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
const from = encodeURIComponent(url.pathname + url.search);
|
|
||||||
|
if (PUBLIC_ROUTES.some((route) => url.pathname.startsWith(route))) {
|
||||||
|
throw new Error("Unauthenticated");
|
||||||
|
}
|
||||||
|
|
||||||
|
const from = url.pathname + url.search;
|
||||||
|
|
||||||
throw redirect({
|
throw redirect({
|
||||||
to: "/refresh-session",
|
to: "/refresh-session",
|
||||||
search: { redirect: from }
|
search: from === "/" ? {} : { redirect: from },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user