-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to fetch auth status before app is rendered
- Loading branch information
Showing
3 changed files
with
45 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters