fix refresh issue

This commit is contained in:
yohlo
2025-09-24 12:20:36 -05:00
parent 36f3bb77d4
commit 81329e4354
7 changed files with 74 additions and 87 deletions

View File

@@ -1,38 +1,33 @@
import { createFileRoute } from '@tanstack/react-router'
import { useEffect } from 'react'
import { useEffect, useRef } from 'react'
import FullScreenLoader from '@/components/full-screen-loader'
import { attemptRefreshingSession } from 'supertokens-web-js/recipe/session'
import { resetRefreshFlag } from '@/lib/supertokens/client'
export const Route = createFileRoute('/refresh-session')({
component: RouteComponent,
})
// https://supertokens.com/docs/additional-verification/session-verification/ssr?uiType=custom
function RouteComponent() {
const hasAttemptedRef = useRef(false);
useEffect(() => {
if (hasAttemptedRef.current) return;
hasAttemptedRef.current = true;
const handleRefresh = async () => {
try {
try {
resetRefreshFlag();
const refreshed = await attemptRefreshingSession()
if (refreshed) {
const urlParams = new URLSearchParams(window.location.search)
const redirect = urlParams.get('redirect')
const isServerFunction = redirect && (
redirect.startsWith('_serverFn') ||
redirect.startsWith('api/') ||
redirect.includes('_serverFn')
);
if (redirect && !isServerFunction) {
if (redirect && !redirect.includes('_serverFn') && !redirect.includes('/api/')) {
window.location.href = decodeURIComponent(redirect)
} else {
const referrer = document.referrer;
const referrerUrl = referrer && !referrer.includes('/_serverFn') && !referrer.includes('/api/')
? referrer
: '/';
window.location.href = referrerUrl;
window.location.href = '/';
}
} else {
window.location.href = '/login'
@@ -42,8 +37,7 @@ function RouteComponent() {
}
}
const timeout = setTimeout(handleRefresh, 100)
return () => clearTimeout(timeout)
setTimeout(handleRefresh, 100)
}, [])
return <FullScreenLoader />