Skip to content

Commit

Permalink
Added auth check error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Jan 17, 2024
1 parent 14a3e8c commit 0888e06
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/frontend/src/authStatusCheck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { API_URL } from './config';
import { type GetAuthStatusResponse } from './features/auth';
import { actions } from './features/auth/store';
import { type AppStore } from './store';

Expand All @@ -13,21 +14,23 @@ const sleep = async (timeout: number): Promise<void> => {
export const initAuthUserState = async (store: AppStore): Promise<void> => {
let retryCount = 0;
let waitInterval = 5000;
let response: Response;

do {
response = await fetch(`${API_URL}/api/v1/auth/status`);
try {
const response = await fetch(`${API_URL}/api/v1/auth/status`);
const user = (await response.json()) as GetAuthStatusResponse;

if (user.isAuthenticated) {
store.dispatch(actions.signIn());
} else {
store.dispatch(actions.signOut());
}

if (response.ok) {
store.dispatch(actions.signIn());
return;
} catch (err) {
await sleep(waitInterval);
retryCount++;
waitInterval *= 2;
}

await sleep(waitInterval);

retryCount++;
waitInterval *= 2;
} while (retryCount < 4);

store.dispatch(actions.signOut());
};

0 comments on commit 0888e06

Please sign in to comment.