Skip to content

Commit

Permalink
fix: Check auth validity before setting isAuthenticated (#8967)
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-determined-ai authored Mar 8, 2024
1 parent f67c473 commit 26c985c
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions webui/react/src/hooks/useAuthCheck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Observable, useObservable } from 'micro-observables';
import { useObservable } from 'micro-observables';
import { useCallback } from 'react';
import { useSearchParams } from 'react-router-dom';

Expand All @@ -9,6 +9,7 @@ import { updateDetApi } from 'services/apiConfig';
import authStore, { AUTH_COOKIE_KEY } from 'stores/auth';
import determinedStore from 'stores/determinedInfo';
import { getCookie } from 'utils/browser';
import handleError from 'utils/error';
import { isAuthFailure } from 'utils/service';

const useAuthCheck = (): (() => Promise<boolean>) => {
Expand Down Expand Up @@ -42,18 +43,22 @@ const useAuthCheck = (): (() => Promise<boolean>) => {
const authToken = jwtToken ?? cookieToken ?? globalStorage.authToken;

/*
* If auth token found, update the API bearer token and validate it with the current user API.
* If an auth token is found, validate it with the current user API, then update
* the API bearer token and set the user as authenticated.
* If an external login URL is provided, redirect there.
* Otherwise mark that we checked the auth and skip auth token validation.
*/

if (authToken) {
updateBearerToken(authToken);

Observable.batch(() => {
try {
await getCurrentUser({});
updateBearerToken(authToken);
authStore.setAuth({ isAuthenticated: true, token: authToken });
authStore.setAuthChecked();
});
} catch (e) {
// If an invalid auth token is detected we need to properly handle the auth error
handleError(e);
}
authStore.setAuthChecked();
} else if (info.externalLoginUri) {
try {
await getCurrentUser({});
Expand Down

0 comments on commit 26c985c

Please sign in to comment.