test auth fix idk
This commit is contained in:
@@ -122,26 +122,12 @@ export const Route = createRootRouteWithContext<{
|
|||||||
context.queryClient,
|
context.queryClient,
|
||||||
playerQueries.auth()
|
playerQueries.auth()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log('__root beforeLoad auth data:', auth);
|
||||||
|
|
||||||
return { auth };
|
return { auth };
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (typeof window !== 'undefined') {
|
console.error('__root beforeLoad error:', error);
|
||||||
const { doesSessionExist } = await import('supertokens-web-js/recipe/session');
|
|
||||||
const { refreshManager } = await import('@/lib/supertokens/refresh-manager');
|
|
||||||
|
|
||||||
const sessionExists = await doesSessionExist();
|
|
||||||
if (sessionExists) {
|
|
||||||
try {
|
|
||||||
await refreshManager.refresh();
|
|
||||||
const auth = await ensureServerQueryData(
|
|
||||||
context.queryClient,
|
|
||||||
playerQueries.auth()
|
|
||||||
);
|
|
||||||
return { auth };
|
|
||||||
} catch {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -5,10 +5,14 @@ import { Flex, Loader } from "@mantine/core";
|
|||||||
|
|
||||||
export const Route = createFileRoute("/_authed")({
|
export const Route = createFileRoute("/_authed")({
|
||||||
beforeLoad: ({ context }) => {
|
beforeLoad: ({ context }) => {
|
||||||
|
console.log('_authed beforeLoad context:', context.auth);
|
||||||
|
|
||||||
if (!context.auth?.user) {
|
if (!context.auth?.user) {
|
||||||
|
console.log('_authed: No user in context, redirecting to login');
|
||||||
throw redirect({ to: "/login" });
|
throw redirect({ to: "/login" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('_authed: User found, allowing access');
|
||||||
return {
|
return {
|
||||||
auth: {
|
auth: {
|
||||||
...context.auth,
|
...context.auth,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useRef } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
import { doesSessionExist } from 'supertokens-web-js/recipe/session';
|
|
||||||
import { refreshManager } from '@/lib/supertokens/refresh-manager';
|
import { refreshManager } from '@/lib/supertokens/refresh-manager';
|
||||||
import { logger } from '@/lib/supertokens';
|
import { logger } from '@/lib/supertokens';
|
||||||
|
import { ensureSuperTokensFrontend } from '@/lib/supertokens/client';
|
||||||
|
|
||||||
export function SessionMonitor() {
|
export function SessionMonitor() {
|
||||||
const lastRefreshTimeRef = useRef<number>(0);
|
const lastRefreshTimeRef = useRef<number>(0);
|
||||||
@@ -27,10 +27,13 @@ export function SessionMonitor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
ensureSuperTokensFrontend();
|
||||||
|
|
||||||
|
const { doesSessionExist } = await import('supertokens-web-js/recipe/session');
|
||||||
|
|
||||||
const sessionExists = await doesSessionExist();
|
const sessionExists = await doesSessionExist();
|
||||||
if (!sessionExists) {
|
if (!sessionExists) {
|
||||||
logger.info('Session monitor: no session exists, redirecting to login');
|
logger.info('Session monitor: no session exists, skipping refresh');
|
||||||
window.location.href = '/login';
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,17 +45,13 @@ export function SessionMonitor() {
|
|||||||
lastRefreshTimeRef.current = Date.now();
|
lastRefreshTimeRef.current = Date.now();
|
||||||
logger.info('Session monitor: session refreshed successfully');
|
logger.info('Session monitor: session refreshed successfully');
|
||||||
} else {
|
} else {
|
||||||
logger.warn('Session monitor: refresh returned false, redirecting to login');
|
logger.warn('Session monitor: refresh returned false');
|
||||||
window.location.href = '/login';
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
logger.error('Session monitor: error refreshing session', error);
|
logger.error('Session monitor: error refreshing session', error?.message);
|
||||||
window.location.href = '/login';
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
handleVisibilityChange();
|
|
||||||
|
|
||||||
document.addEventListener('visibilitychange', handleVisibilityChange);
|
document.addEventListener('visibilitychange', handleVisibilityChange);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
|||||||
@@ -10,29 +10,50 @@ import { toServerResult } from "@/lib/tanstack-query/utils/to-server-result";
|
|||||||
import { serverFnLoggingMiddleware } from "@/utils/activities";
|
import { serverFnLoggingMiddleware } from "@/utils/activities";
|
||||||
|
|
||||||
export const fetchMe = createServerFn()
|
export const fetchMe = createServerFn()
|
||||||
.handler(async () =>
|
.handler(async () =>
|
||||||
toServerResult(async () => {
|
toServerResult(async () => {
|
||||||
const request = getRequest();
|
const request = getRequest();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const context = await getSessionContext(request);
|
const context = await getSessionContext(request);
|
||||||
|
|
||||||
await pbAdmin.authPromise;
|
await pbAdmin.authPromise;
|
||||||
const result = await pbAdmin.getPlayerByAuthId(context.userAuthId);
|
const result = await pbAdmin.getPlayerByAuthId(context.userAuthId);
|
||||||
return {
|
return {
|
||||||
user: result || undefined,
|
user: result || undefined,
|
||||||
roles: context.roles,
|
roles: context.roles,
|
||||||
metadata: context.metadata,
|
metadata: context.metadata,
|
||||||
phone: context.phone
|
phone: context.phone
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
// logger.info("FetchMe: Session error", error)
|
logger.info("FetchMe: Error caught", {
|
||||||
if (error?.response?.status === 401) {
|
message: error?.message,
|
||||||
const errorData = error?.response?.data;
|
isResponse: error instanceof Response,
|
||||||
if (errorData?.error === "SESSION_REFRESH_REQUIRED") {
|
status: error instanceof Response ? error.status : error?.response?.status
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error instanceof Response) {
|
||||||
|
const status = error.status;
|
||||||
|
if (status === 440) {
|
||||||
|
logger.info("FetchMe: Session refresh required (440)");
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (error?.response?.status === 440 || error?.response?.status === 401) {
|
||||||
|
const errorData = error?.response?.data;
|
||||||
|
if (errorData?.error === "SESSION_REFRESH_REQUIRED") {
|
||||||
|
logger.info("FetchMe: Session refresh required (legacy)");
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error?.message === "Unauthenticated") {
|
||||||
|
logger.info("FetchMe: No authenticated user (expected when not logged in)");
|
||||||
|
return { user: undefined, roles: [], metadata: {}, phone: undefined };
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.warn("FetchMe: Unexpected error, returning default", error);
|
||||||
return { user: undefined, roles: [], metadata: {}, phone: undefined };
|
return { user: undefined, roles: [], metadata: {}, phone: undefined };
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -6,40 +6,46 @@ export async function handleQueryError(error: any): Promise<void> {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isSessionExpired =
|
if (!error || typeof error !== 'object') {
|
||||||
error?.response?.status === 440 ||
|
throw error;
|
||||||
error?.response?.headers?.get?.('X-Session-Expired') === 'true';
|
|
||||||
|
|
||||||
if (isSessionExpired) {
|
|
||||||
try {
|
|
||||||
const errorData = await error.response.json().catch(() => ({}));
|
|
||||||
|
|
||||||
if (errorData.error === 'SESSION_REFRESH_REQUIRED' && errorData.shouldRetry) {
|
|
||||||
logger.warn('Query detected SESSION_REFRESH_REQUIRED, initiating redirect');
|
|
||||||
|
|
||||||
const currentUrl = window.location.pathname + window.location.search;
|
|
||||||
await refreshManager.redirectToRefresh(currentUrl);
|
|
||||||
|
|
||||||
throw new Error('Redirecting to refresh session');
|
|
||||||
}
|
|
||||||
} catch (parseError) {
|
|
||||||
if (error?.response?.status === 440) {
|
|
||||||
logger.warn('Session expired (440), redirecting to refresh');
|
|
||||||
const currentUrl = window.location.pathname + window.location.search;
|
|
||||||
await refreshManager.redirectToRefresh(currentUrl);
|
|
||||||
throw new Error('Redirecting to refresh session');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error?.response?.status === 401) {
|
if (error instanceof Response) {
|
||||||
try {
|
const status = error.status;
|
||||||
const errorData = typeof error.response.data === 'string'
|
|
||||||
? JSON.parse(error.response.data)
|
|
||||||
: error.response.data;
|
|
||||||
|
|
||||||
if (errorData?.error === 'SESSION_REFRESH_REQUIRED') {
|
if (status === 440) {
|
||||||
logger.warn('Query detected legacy SESSION_REFRESH_REQUIRED (401), initiating redirect');
|
try {
|
||||||
|
const errorData = await error.json();
|
||||||
|
|
||||||
|
if (errorData?.error === 'SESSION_REFRESH_REQUIRED' && errorData?.shouldRetry === true) {
|
||||||
|
logger.warn('Query detected SESSION_REFRESH_REQUIRED (Response), initiating redirect');
|
||||||
|
|
||||||
|
const currentUrl = window.location.pathname + window.location.search;
|
||||||
|
await refreshManager.redirectToRefresh(currentUrl);
|
||||||
|
|
||||||
|
throw new Error('Redirecting to refresh session');
|
||||||
|
}
|
||||||
|
} catch (parseError) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
const status = error?.response?.status;
|
||||||
|
|
||||||
|
if (status === 440) {
|
||||||
|
try {
|
||||||
|
let errorData = error?.response?.data;
|
||||||
|
|
||||||
|
if (typeof errorData === 'string') {
|
||||||
|
try {
|
||||||
|
errorData = JSON.parse(errorData);
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (errorData?.error === 'SESSION_REFRESH_REQUIRED' && errorData?.shouldRetry === true) {
|
||||||
|
logger.warn('Query detected SESSION_REFRESH_REQUIRED (legacy format), initiating redirect');
|
||||||
|
|
||||||
const currentUrl = window.location.pathname + window.location.search;
|
const currentUrl = window.location.pathname + window.location.search;
|
||||||
await refreshManager.redirectToRefresh(currentUrl);
|
await refreshManager.redirectToRefresh(currentUrl);
|
||||||
|
|||||||
Reference in New Issue
Block a user