Skip to content

Commit 096f780

Browse files
authored
Switch to use login with redirect through MSAL (#241)
1 parent dc730f7 commit 096f780

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

frontend/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class AppImpl extends React.Component<AppProps> {
3131
private initializingAuthenticated = false
3232

3333
private onVisibilityChange = () => {
34-
if (!document.hidden) {
34+
if (!document.hidden && this.initializedAuthenticated) {
3535
this.refreshStaleData()
3636
}
3737
}

frontend/src/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ window.onerror = (message, source, lineno, colno) => {
1313
return
1414
}
1515

16-
try {
16+
try {
1717
LogError(`${source}:${lineno}:${colno} ${message}`, window.location.pathname)
1818
} catch {
1919
console.debug('Fatal error: ', message, source, lineno, colno)

frontend/src/utils/msal.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ export const initializeMsal = async () => {
2525
cacheLocation: 'localStorage',
2626
},
2727
})
28+
29+
const pca = await pcaPromise
30+
31+
try {
32+
const response = await pca.handleRedirectPromise()
33+
if (response?.account) {
34+
pca.setActiveAccount(response.account)
35+
cachedAuthResult = response
36+
}
37+
} catch {
38+
// Allow the app to continue in unauthenticated state
39+
}
2840
}
2941

3042
const getScopes = (): string[] => {
@@ -48,13 +60,10 @@ export const isAuthEnabled = (): boolean => {
4860
return authConfig?.enabled ?? false
4961
}
5062

51-
export const loginWithPopup = async () => {
63+
export const loginWithRedirect = async () => {
5264
if (!authConfig?.enabled || !pcaPromise) return
5365
const pca = await pcaPromise
54-
cachedAuthResult = await pca.loginPopup({ scopes: getScopes() })
55-
if (cachedAuthResult.account) {
56-
pca.setActiveAccount(cachedAuthResult.account)
57-
}
66+
await pca.loginRedirect({ scopes: getScopes() })
5867
}
5968

6069
export const loginSilently = async (): Promise<boolean> => {
@@ -87,10 +96,6 @@ export const logout = async () => {
8796
return
8897
}
8998
const pca = await pcaPromise
90-
const account = pca.getActiveAccount() ?? pca.getAllAccounts()[0]
91-
if (account) {
92-
await pca.logoutPopup({ account })
93-
}
9499
cachedAuthResult = null
95-
window.location.href = '/'
100+
await pca.logoutRedirect({ postLogoutRedirectUri: window.location.origin })
96101
}

frontend/src/views/Authorization/LoginView.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
Button,
88
} from '@mui/joy'
99
import React from 'react'
10-
import { loginWithPopup } from '@/utils/msal'
10+
import { loginWithRedirect } from '@/utils/msal'
1111
import { setTitle } from '@/utils/dom'
12-
import { NavigationPaths, WithNavigate } from '@/utils/navigation'
12+
import { WithNavigate } from '@/utils/navigation'
1313
import { connect } from 'react-redux'
1414
import { AppDispatch } from '@/store/store'
1515
import { pushStatus } from '@/store/statusSlice'
@@ -26,8 +26,7 @@ class LoginViewImpl extends React.Component<LoginViewProps> {
2626

2727
private handleLogin = async () => {
2828
try {
29-
await loginWithPopup()
30-
this.props.navigate(NavigationPaths.HomeView())
29+
await loginWithRedirect()
3130
} catch (error) {
3231
this.props.pushStatus((error as Error).message, 'error', 5000)
3332
}

0 commit comments

Comments
 (0)