fix(ui): notify auth provider for login failed event (#6484)

This commit is contained in:
Chirag Madlani 2022-08-01 20:17:26 +05:30 committed by GitHub
parent f276a65612
commit cc2dcefa6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View File

@ -557,6 +557,7 @@ export const AuthProvider = ({
<MsalProvider instance={msalInstance}>
<MsalAuthenticator
ref={authenticatorRef}
onLoginFailure={handleFailedLogin}
onLoginSuccess={handleSuccessfulLogin}
onLogoutSuccess={handleSuccessfulLogout}>
{children}

View File

@ -30,10 +30,12 @@ describe('Test MsalAuthenticator component', () => {
it('Checks if the MsalAuthenticator renders', async () => {
const onLogoutMock = jest.fn();
const onLoginMock = jest.fn();
const onFailedLoginMock = jest.fn();
const authenticatorRef = null;
render(
<MsalAuthenticator
ref={authenticatorRef}
onLoginFailure={onFailedLoginMock}
onLoginSuccess={onLoginMock}
onLogoutSuccess={onLogoutMock}>
<p data-testid="children" />

View File

@ -13,6 +13,7 @@
import { InteractionStatus } from '@azure/msal-browser';
import { useAccount, useIsAuthenticated, useMsal } from '@azure/msal-react';
import { AxiosError } from 'axios';
import React, {
forwardRef,
Fragment,
@ -32,11 +33,15 @@ import {
interface Props {
children: ReactNode;
onLoginSuccess: (user: OidcUser) => void;
onLoginFailure: (error: AxiosError) => void;
onLogoutSuccess: () => void;
}
const MsalAuthenticator = forwardRef<AuthenticatorRef, Props>(
({ children, onLoginSuccess, onLogoutSuccess }: Props, ref) => {
(
{ children, onLoginSuccess, onLogoutSuccess, onLoginFailure }: Props,
ref
) => {
const { setIsAuthenticated, loading, setLoadingIndicator } =
useAuthContext();
const { instance, accounts, inProgress } = useMsal();
@ -115,6 +120,7 @@ const MsalAuthenticator = forwardRef<AuthenticatorRef, Props>(
onLoginSuccess(user as OidcUser);
}
})
.catch(onLoginFailure)
.finally(() => {
if (loading) {
setLoadingIndicator(false);