Prevent sending requests that are known to fail
In case it's still unknown if auth is set up on the server, only the initial request to detect this should be sent. Since all other requests will also just fail, they should be blocked until the auth initialization is completed. While the access token is getting refreshed, no request will succeed since the access token is expired; thus, they should not be sent.
This commit is contained in:
@@ -18,6 +18,10 @@ export class AuthManager {
|
||||
|
||||
private static accessToken: string | null = null;
|
||||
|
||||
private static authInitialized: boolean = false;
|
||||
|
||||
private static refreshingToken: boolean = false;
|
||||
|
||||
static isAuthRequired(): boolean | null {
|
||||
return AppStorage.session.getItemParsed(AuthManager.AUTH_REQUIRED_KEY, null);
|
||||
}
|
||||
@@ -90,4 +94,24 @@ export class AuthManager {
|
||||
static useListenToReactSessionContextRefreshEvent(): void {
|
||||
useSessionStorage(AuthManager.REACT_SESSION_REFRESH_KEY, 0);
|
||||
}
|
||||
|
||||
static isAuthInitialized(): boolean {
|
||||
return AuthManager.authInitialized;
|
||||
}
|
||||
|
||||
static setAuthInitialized(value: boolean): void {
|
||||
AuthManager.authInitialized = value;
|
||||
}
|
||||
|
||||
static isRefreshingToken(): boolean {
|
||||
return AuthManager.refreshingToken;
|
||||
}
|
||||
|
||||
static setIsRefreshingToken(value: boolean): void {
|
||||
AuthManager.refreshingToken = value;
|
||||
}
|
||||
|
||||
static shouldQueueRequests(): boolean {
|
||||
return !AuthManager.isAuthInitialized() || AuthManager.isRefreshingToken();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { ReactNode, useEffect } from 'react';
|
||||
import { ReactNode } from 'react';
|
||||
import { useSessionContext } from '@/features/authentication/SessionContext.tsx';
|
||||
import { SplashScreen } from '@/features/authentication/components/SplashScreen.tsx';
|
||||
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
||||
@@ -17,17 +17,17 @@ export const AuthGuard = ({ children }: { children: ReactNode }) => {
|
||||
|
||||
requestManager.useGetAbout({
|
||||
skip: isAuthRequired !== null,
|
||||
onCompleted: () => AuthManager.setAuthRequired(false),
|
||||
onCompleted: () => {
|
||||
if (AuthManager.isAuthInitialized()) {
|
||||
return;
|
||||
}
|
||||
|
||||
AuthManager.setAuthRequired(false);
|
||||
AuthManager.setAuthInitialized(true);
|
||||
requestManager.processQueues();
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const onUnload = () => AuthManager.setAuthRequired(null);
|
||||
|
||||
window.addEventListener('beforeunload', onUnload);
|
||||
|
||||
return () => window.removeEventListener('beforeunload', onUnload);
|
||||
}, []);
|
||||
|
||||
if (isAuthRequired === null) {
|
||||
return <SplashScreen />;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user