From 9365b84318a6c85c199893ca440aa9a9f1055a94 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Sat, 20 Jan 2024 18:23:12 +0300 Subject: [PATCH] Removed auth status check experiment --- src/frontend/src/authStatusCheck.ts | 36 ------------------- .../src/features/auth/hooks/useAuth.ts | 18 ++++++---- src/frontend/src/index.tsx | 7 +--- 3 files changed, 13 insertions(+), 48 deletions(-) delete mode 100644 src/frontend/src/authStatusCheck.ts diff --git a/src/frontend/src/authStatusCheck.ts b/src/frontend/src/authStatusCheck.ts deleted file mode 100644 index 80b878b02..000000000 --- a/src/frontend/src/authStatusCheck.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { API_URL } from './config'; -import { type GetAuthStatusResponse } from './features/auth'; -import { actions } from './features/auth/store'; -import { type AppStore } from './store'; - -const sleep = async (timeout: number): Promise => { - await new Promise(resolve => { - setTimeout(() => { - resolve({}); - }, timeout); - }); -}; - -export const initAuthUserState = async (store: AppStore): Promise => { - let retryCount = 0; - let waitInterval = 5000; - - do { - 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()); - } - - return; - } catch (err) { - await sleep(waitInterval); - retryCount++; - waitInterval *= 2; - } - } while (retryCount < 4); -}; diff --git a/src/frontend/src/features/auth/hooks/useAuth.ts b/src/frontend/src/features/auth/hooks/useAuth.ts index f9dce37a0..177d42c51 100644 --- a/src/frontend/src/features/auth/hooks/useAuth.ts +++ b/src/frontend/src/features/auth/hooks/useAuth.ts @@ -22,7 +22,13 @@ interface UseAuthHookResult { export default function useAuth(): UseAuthHookResult { const user = useAppSelector(state => state.auth.user); const dispatch = useAppDispatch(); - const [getAuthStatus, getAuthStatusRequest] = authApi.useLazyGetStatusQuery(); + + const { refetch, isFetching } = authApi.useGetStatusQuery( + {}, + { + skip: !!user, + }, + ); const signIn = useCallback(() => { dispatch(actions.signIn()); @@ -54,16 +60,16 @@ export default function useAuth(): UseAuthHookResult { }, [signOut]); const completeLogin = useCallback(() => { - void getAuthStatus({}); - }, [getAuthStatus]); + void refetch(); + }, [refetch]); const completeLogout = useCallback(() => { - void getAuthStatus({}); - }, [getAuthStatus]); + void refetch(); + }, [refetch]); return { user, - isLoggingIn: getAuthStatusRequest.isFetching, + isLoggingIn: isFetching, login, logout, completeLogin, diff --git a/src/frontend/src/index.tsx b/src/frontend/src/index.tsx index 8b94911f9..fff8eaa7a 100644 --- a/src/frontend/src/index.tsx +++ b/src/frontend/src/index.tsx @@ -3,8 +3,7 @@ import { initBrowserMockApi } from 'tests/mockApi'; import { createRoot } from 'react-dom/client'; import App from './App'; import AppProvider from './AppProvider'; -import { initAuthUserState } from './authStatusCheck'; -import { FAKE_AUTH_ENABLED, GOOGLE_ANALYTICS_ENABLED, MSW_ENABLED } from './config'; +import { GOOGLE_ANALYTICS_ENABLED, MSW_ENABLED } from './config'; import { initGoogleAnalytics } from './googleAnalytics'; import store from './store'; @@ -17,10 +16,6 @@ void (async () => { initGoogleAnalytics(); } - if (!FAKE_AUTH_ENABLED) { - await initAuthUserState(store); - } - const container = document.getElementById('root') as HTMLElement; const root = createRoot(container);