From 0888e069c1efbf336a284ae5dfb5f52fc98f4a81 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Wed, 17 Jan 2024 22:49:43 +0300 Subject: [PATCH] Added auth check error handling --- src/frontend/src/authStatusCheck.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/frontend/src/authStatusCheck.ts b/src/frontend/src/authStatusCheck.ts index ef37ba1a6..80b878b02 100644 --- a/src/frontend/src/authStatusCheck.ts +++ b/src/frontend/src/authStatusCheck.ts @@ -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'; @@ -13,21 +14,23 @@ const sleep = async (timeout: number): Promise => { export const initAuthUserState = async (store: AppStore): Promise => { 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()); };