mirror of
https://github.com/open-metadata/OpenMetadata.git
synced 2025-08-24 17:08:28 +00:00
fix(ui): Update flags from AuthProvider only (#6566)
This commit is contained in:
parent
45e6c2c7d7
commit
c6d35f7c4c
@ -109,11 +109,13 @@ export const AuthProvider = ({
|
|||||||
let silentSignInRetries = 0;
|
let silentSignInRetries = 0;
|
||||||
|
|
||||||
const onLoginHandler = () => {
|
const onLoginHandler = () => {
|
||||||
|
setLoading(true);
|
||||||
authenticatorRef.current?.invokeLogin();
|
authenticatorRef.current?.invokeLogin();
|
||||||
};
|
};
|
||||||
|
|
||||||
const onLogoutHandler = () => {
|
const onLogoutHandler = () => {
|
||||||
authenticatorRef.current?.invokeLogout();
|
authenticatorRef.current?.invokeLogout();
|
||||||
|
setLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onRenewIdTokenHandler = () => {
|
const onRenewIdTokenHandler = () => {
|
||||||
@ -349,11 +351,13 @@ export const AuthProvider = ({
|
|||||||
const handleFailedLogin = () => {
|
const handleFailedLogin = () => {
|
||||||
setIsSigningIn(false);
|
setIsSigningIn(false);
|
||||||
setIsUserAuthenticated(false);
|
setIsUserAuthenticated(false);
|
||||||
|
setLoading(false);
|
||||||
history.push(ROUTES.SIGNIN);
|
history.push(ROUTES.SIGNIN);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSuccessfulLogin = (user: OidcUser) => {
|
const handleSuccessfulLogin = (user: OidcUser) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
setIsUserAuthenticated(true);
|
||||||
getUserByName(getNameFromEmail(user.profile.email), userAPIQueryFields)
|
getUserByName(getNameFromEmail(user.profile.email), userAPIQueryFields)
|
||||||
.then((res: AxiosResponse) => {
|
.then((res: AxiosResponse) => {
|
||||||
if (res.data) {
|
if (res.data) {
|
||||||
|
@ -16,7 +16,7 @@ import {
|
|||||||
InteractionRequiredAuthError,
|
InteractionRequiredAuthError,
|
||||||
InteractionStatus,
|
InteractionStatus,
|
||||||
} from '@azure/msal-browser';
|
} from '@azure/msal-browser';
|
||||||
import { useAccount, useIsAuthenticated, useMsal } from '@azure/msal-react';
|
import { useAccount, useMsal } from '@azure/msal-react';
|
||||||
import { AxiosError } from 'axios';
|
import { AxiosError } from 'axios';
|
||||||
import React, {
|
import React, {
|
||||||
forwardRef,
|
forwardRef,
|
||||||
@ -28,7 +28,6 @@ import React, {
|
|||||||
import { useMutex } from 'react-context-mutex';
|
import { useMutex } from 'react-context-mutex';
|
||||||
import { oidcTokenKey } from '../../constants/constants';
|
import { oidcTokenKey } from '../../constants/constants';
|
||||||
import { msalLoginRequest } from '../../utils/AuthProvider.util';
|
import { msalLoginRequest } from '../../utils/AuthProvider.util';
|
||||||
import { useAuthContext } from '../auth-provider/AuthProvider';
|
|
||||||
import {
|
import {
|
||||||
AuthenticatorRef,
|
AuthenticatorRef,
|
||||||
OidcUser,
|
OidcUser,
|
||||||
@ -46,10 +45,7 @@ const MsalAuthenticator = forwardRef<AuthenticatorRef, Props>(
|
|||||||
{ children, onLoginSuccess, onLogoutSuccess, onLoginFailure }: Props,
|
{ children, onLoginSuccess, onLogoutSuccess, onLoginFailure }: Props,
|
||||||
ref
|
ref
|
||||||
) => {
|
) => {
|
||||||
const { setIsAuthenticated, loading, setLoadingIndicator } =
|
|
||||||
useAuthContext();
|
|
||||||
const { instance, accounts, inProgress } = useMsal();
|
const { instance, accounts, inProgress } = useMsal();
|
||||||
const isMsalAuthenticated = useIsAuthenticated();
|
|
||||||
const account = useAccount(accounts[0] || {});
|
const account = useAccount(accounts[0] || {});
|
||||||
const MutexRunner = useMutex();
|
const MutexRunner = useMutex();
|
||||||
const mutex = new MutexRunner('fetchIdToken');
|
const mutex = new MutexRunner('fetchIdToken');
|
||||||
@ -64,12 +60,16 @@ const MsalAuthenticator = forwardRef<AuthenticatorRef, Props>(
|
|||||||
};
|
};
|
||||||
|
|
||||||
const login = () => {
|
const login = () => {
|
||||||
setLoadingIndicator(true);
|
try {
|
||||||
instance.loginPopup(msalLoginRequest);
|
instance.loginPopup(msalLoginRequest);
|
||||||
|
} catch (error) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.error(error);
|
||||||
|
onLoginFailure(error as AxiosError);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
setLoadingIndicator(false);
|
|
||||||
handleOnLogoutSuccess();
|
handleOnLogoutSuccess();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -87,14 +87,13 @@ const MsalAuthenticator = forwardRef<AuthenticatorRef, Props>(
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
setIsAuthenticated(isMsalAuthenticated);
|
|
||||||
localStorage.setItem(oidcTokenKey, idToken);
|
localStorage.setItem(oidcTokenKey, idToken);
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchIdToken = async (
|
const fetchIdToken = async (
|
||||||
shouldFaullbackToPopup = false
|
shouldFallbackToPopup = false
|
||||||
): Promise<OidcUser> => {
|
): Promise<OidcUser> => {
|
||||||
const tokenRequest = {
|
const tokenRequest = {
|
||||||
account: account || accounts[0], // This is an example - Select account based on your app's requirements
|
account: account || accounts[0], // This is an example - Select account based on your app's requirements
|
||||||
@ -107,7 +106,7 @@ const MsalAuthenticator = forwardRef<AuthenticatorRef, Props>(
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (
|
if (
|
||||||
error instanceof InteractionRequiredAuthError &&
|
error instanceof InteractionRequiredAuthError &&
|
||||||
shouldFaullbackToPopup
|
shouldFallbackToPopup
|
||||||
) {
|
) {
|
||||||
const response = await instance
|
const response = await instance
|
||||||
.loginPopup(tokenRequest)
|
.loginPopup(tokenRequest)
|
||||||
@ -140,15 +139,11 @@ const MsalAuthenticator = forwardRef<AuthenticatorRef, Props>(
|
|||||||
fetchIdToken(true)
|
fetchIdToken(true)
|
||||||
.then((user) => {
|
.then((user) => {
|
||||||
if ((user as OidcUser).id_token) {
|
if ((user as OidcUser).id_token) {
|
||||||
setLoadingIndicator(false);
|
|
||||||
onLoginSuccess(user as OidcUser);
|
onLoginSuccess(user as OidcUser);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(onLoginFailure)
|
.catch(onLoginFailure)
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
if (loading) {
|
|
||||||
setLoadingIndicator(false);
|
|
||||||
}
|
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user