fix: keep session when refresh fails on network error or 5xx (WealthLedger/frontend#97)
This commit was merged in pull request #100.
This commit is contained in:
@@ -5,6 +5,9 @@ import { apiClient, configureAuthClient, type paths } from "@/shared/api";
|
||||
|
||||
import { type User } from "./auth-context";
|
||||
|
||||
// A 5xx or network error means the server is unreachable, not that the session ended.
|
||||
const HTTP_SERVER_ERROR = 500;
|
||||
|
||||
type Authentication
|
||||
= paths["/api/v1/auth/login"]["post"]["responses"][200]["content"]["application/json"];
|
||||
|
||||
@@ -32,6 +35,8 @@ function useAuthProviderVm() {
|
||||
const accessTokenRef = useRef<string | undefined>(undefined);
|
||||
const [ready, setReady] = useState(false);
|
||||
const [signedOut, setSignedOut] = useState(false);
|
||||
const [unavailable, setUnavailable] = useState(false);
|
||||
const unavailableRef = useRef(false);
|
||||
|
||||
const generationRef = useRef(0);
|
||||
const userRef = useRef<undefined | User>(undefined);
|
||||
@@ -49,12 +54,18 @@ function useAuthProviderVm() {
|
||||
return next;
|
||||
}, []);
|
||||
|
||||
const markUnavailable = useCallback((isUnavailable: boolean) => {
|
||||
unavailableRef.current = isUnavailable;
|
||||
setUnavailable(isUnavailable);
|
||||
}, []);
|
||||
|
||||
const applyAuthentication = useCallback(
|
||||
(authentication: Authentication) => {
|
||||
const changedIdentity
|
||||
= userRef.current && userRef.current.id !== authentication.user.id;
|
||||
|
||||
setSignedOut(false);
|
||||
markUnavailable(false);
|
||||
accessTokenRef.current = authentication.accessToken;
|
||||
|
||||
if (changedIdentity) {
|
||||
@@ -67,16 +78,17 @@ function useAuthProviderVm() {
|
||||
userRef.current = authentication.user;
|
||||
setUser(authentication.user);
|
||||
},
|
||||
[queryClient],
|
||||
[markUnavailable, queryClient],
|
||||
);
|
||||
|
||||
const clearSession = useCallback(() => {
|
||||
markUnavailable(false);
|
||||
generationRef.current += 1;
|
||||
userRef.current = undefined;
|
||||
setUser(undefined);
|
||||
accessTokenRef.current = undefined;
|
||||
queryClient.clear();
|
||||
}, [queryClient]);
|
||||
}, [markUnavailable, queryClient]);
|
||||
|
||||
const refresh = useCallback((): Promise<boolean> => {
|
||||
if (refreshPromiseRef.current) return refreshPromiseRef.current;
|
||||
@@ -86,11 +98,12 @@ function useAuthProviderVm() {
|
||||
if (started !== generationRef.current) return false;
|
||||
|
||||
try {
|
||||
const { data } = await apiClient.POST("/api/v1/auth/refresh");
|
||||
const { data, response } = await apiClient.POST("/api/v1/auth/refresh");
|
||||
if (started !== generationRef.current) return false;
|
||||
|
||||
if (!data) {
|
||||
clearSession();
|
||||
if (response.status >= HTTP_SERVER_ERROR) markUnavailable(true);
|
||||
else clearSession();
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -100,7 +113,7 @@ function useAuthProviderVm() {
|
||||
return true;
|
||||
}
|
||||
catch {
|
||||
if (started === generationRef.current) clearSession();
|
||||
if (started === generationRef.current) markUnavailable(true);
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -112,7 +125,7 @@ function useAuthProviderVm() {
|
||||
refreshPromiseRef.current = pending;
|
||||
|
||||
return pending;
|
||||
}, [applyAuthentication, clearSession, serialize]);
|
||||
}, [applyAuthentication, clearSession, markUnavailable, serialize]);
|
||||
|
||||
useEffect(() => {
|
||||
void refresh().finally(() => setReady(true));
|
||||
@@ -146,6 +159,7 @@ function useAuthProviderVm() {
|
||||
clear: clearSession,
|
||||
generation: () => generationRef.current,
|
||||
refresh,
|
||||
unavailable: () => unavailableRef.current,
|
||||
});
|
||||
});
|
||||
useEffect(() => () => configureAuthClient(), []);
|
||||
@@ -157,7 +171,9 @@ function useAuthProviderVm() {
|
||||
ready,
|
||||
register: (email: string, password: string) =>
|
||||
authenticate("/api/v1/auth/register", email, password),
|
||||
retry: () => void refresh(),
|
||||
signedOut,
|
||||
unavailable,
|
||||
user,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user