Skip to content

Commit

Permalink
Try to fetch auth status before app is rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Jan 17, 2024
1 parent aba2ba2 commit 14a3e8c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
33 changes: 33 additions & 0 deletions src/frontend/src/authStatusCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { API_URL } from './config';
import { actions } from './features/auth/store';
import { type AppStore } from './store';

const sleep = async (timeout: number): Promise<void> => {
await new Promise(resolve => {
setTimeout(() => {
resolve({});
}, timeout);
});
};

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`);

if (response.ok) {
store.dispatch(actions.signIn());
return;
}

await sleep(waitInterval);

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

store.dispatch(actions.signOut());
};
18 changes: 6 additions & 12 deletions src/frontend/src/features/auth/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ interface UseAuthHookResult {
export default function useAuth(): UseAuthHookResult {
const user = useAppSelector(state => state.auth.user);
const dispatch = useAppDispatch();

const { refetch, isFetching } = authApi.useGetStatusQuery(
{},
{
skip: !!user,
},
);
const [getAuthStatus, getAuthStatusRequest] = authApi.useLazyGetStatusQuery();

const signIn = useCallback(() => {
dispatch(actions.signIn());
Expand Down Expand Up @@ -60,16 +54,16 @@ export default function useAuth(): UseAuthHookResult {
}, [signOut]);

const completeLogin = useCallback(() => {
void refetch();
}, [refetch]);
void getAuthStatus({});
}, [getAuthStatus]);

const completeLogout = useCallback(() => {
void refetch();
}, [refetch]);
void getAuthStatus({});
}, [getAuthStatus]);

return {
user,
isLoggingIn: isFetching,
isLoggingIn: getAuthStatusRequest.isFetching,
login,
logout,
completeLogin,
Expand Down
7 changes: 6 additions & 1 deletion src/frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { initBrowserMockApi } from 'tests/mockApi';
import { createRoot } from 'react-dom/client';
import App from './App';
import AppProvider from './AppProvider';
import { GOOGLE_ANALYTICS_ENABLED, MSW_ENABLED } from './config';
import { initAuthUserState } from './authStatusCheck';
import { FAKE_AUTH_ENABLED, GOOGLE_ANALYTICS_ENABLED, MSW_ENABLED } from './config';
import { initGoogleAnalytics } from './googleAnalytics';
import store from './store';

Expand All @@ -16,6 +17,10 @@ void (async () => {
initGoogleAnalytics();
}

if (!FAKE_AUTH_ENABLED) {
await initAuthUserState(store);
}

const container = document.getElementById('root') as HTMLElement;
const root = createRoot(container);

Expand Down

0 comments on commit 14a3e8c

Please sign in to comment.