mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 14:25:23 +02:00
fix(auth): resolve concurrent session token refresh and state syncing issues to prevent frequent logouts
This commit is contained in:
+45
-20
@@ -72,37 +72,62 @@ export async function signIn(email: string, password: string): Promise<AuthSessi
|
||||
};
|
||||
|
||||
localStorage.setItem(SESSION_KEY, JSON.stringify(session));
|
||||
window.dispatchEvent(new Event('session-refreshed'));
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function refreshSession(refreshToken: string): Promise<AuthSession> {
|
||||
const response = await fetch(`${SUPABASE_URL}/auth/v1/token?grant_type=refresh_token`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'apikey': SUPABASE_ANON_KEY,
|
||||
},
|
||||
body: JSON.stringify({ refresh_token: refreshToken }),
|
||||
});
|
||||
let activeRefreshPromise: Promise<AuthSession> | null = null;
|
||||
|
||||
if (!response.ok) {
|
||||
localStorage.removeItem(SESSION_KEY);
|
||||
throw new Error('Session expired. Please sign in again.');
|
||||
export async function refreshSession(refreshToken: string): Promise<AuthSession> {
|
||||
if (activeRefreshPromise) {
|
||||
return activeRefreshPromise;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const session: AuthSession = {
|
||||
access_token: data.access_token,
|
||||
refresh_token: data.refresh_token,
|
||||
user: { id: data.user.id, email: data.user.email },
|
||||
};
|
||||
activeRefreshPromise = (async () => {
|
||||
try {
|
||||
const response = await fetch(`${SUPABASE_URL}/auth/v1/token?grant_type=refresh_token`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'apikey': SUPABASE_ANON_KEY,
|
||||
},
|
||||
body: JSON.stringify({ refresh_token: refreshToken }),
|
||||
});
|
||||
|
||||
localStorage.setItem(SESSION_KEY, JSON.stringify(session));
|
||||
return session;
|
||||
if (!response.ok) {
|
||||
if (response.status === 400 || response.status === 401 || response.status === 403) {
|
||||
localStorage.removeItem(SESSION_KEY);
|
||||
window.dispatchEvent(new Event('session-refreshed'));
|
||||
throw new Error('Session expired. Please sign in again.');
|
||||
} else {
|
||||
throw new Error(`Server error (${response.status}). Please try again later.`);
|
||||
}
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const session: AuthSession = {
|
||||
access_token: data.access_token,
|
||||
refresh_token: data.refresh_token,
|
||||
user: { id: data.user.id, email: data.user.email },
|
||||
};
|
||||
|
||||
localStorage.setItem(SESSION_KEY, JSON.stringify(session));
|
||||
window.dispatchEvent(new Event('session-refreshed'));
|
||||
return session;
|
||||
} catch (error) {
|
||||
// If the error was not already thrown as "Session expired", we just propagate it.
|
||||
throw error;
|
||||
} finally {
|
||||
activeRefreshPromise = null;
|
||||
}
|
||||
})();
|
||||
|
||||
return activeRefreshPromise;
|
||||
}
|
||||
|
||||
export function signOut(): void {
|
||||
localStorage.removeItem(SESSION_KEY);
|
||||
window.dispatchEvent(new Event('session-refreshed'));
|
||||
}
|
||||
|
||||
export function getStoredSession(): AuthSession | null {
|
||||
|
||||
Reference in New Issue
Block a user