Skip to content

Commit 0679ac1

Browse files
authored
Smooth out login ux (#242)
1 parent 096f780 commit 0679ac1

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

frontend/src/utils/msal.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ import type { AuthConfig } from '@/api/auth'
1010
let authConfig: AuthConfig | null = null
1111
let cachedAuthResult: AuthenticationResult | null = null
1212
let pcaPromise: Promise<IPublicClientApplication> | null = null
13+
let initPromise: Promise<void> | null = null
1314

14-
export const initializeMsal = async () => {
15+
export const initializeMsal = () => {
16+
if (!initPromise) {
17+
initPromise = doInitializeMsal()
18+
}
19+
return initPromise
20+
}
21+
22+
const doInitializeMsal = async () => {
1523
authConfig = await GetAuthConfig()
1624
if (!authConfig.enabled) return
1725

frontend/src/views/Authorization/LoginView.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Loading } from '@/Loading'
12
import { Logo } from '@/Logo'
23
import { Sheet } from '@mui/joy'
34
import {
@@ -7,9 +8,9 @@ import {
78
Button,
89
} from '@mui/joy'
910
import React from 'react'
10-
import { loginWithRedirect } from '@/utils/msal'
11+
import { initializeMsal, loginSilently, loginWithRedirect } from '@/utils/msal'
1112
import { setTitle } from '@/utils/dom'
12-
import { WithNavigate } from '@/utils/navigation'
13+
import { NavigationPaths, WithNavigate } from '@/utils/navigation'
1314
import { connect } from 'react-redux'
1415
import { AppDispatch } from '@/store/store'
1516
import { pushStatus } from '@/store/statusSlice'
@@ -19,9 +20,25 @@ type LoginViewProps = WithNavigate & {
1920
pushStatus: (message: string, severity: StatusSeverity, timeout?: number) => void
2021
}
2122

22-
class LoginViewImpl extends React.Component<LoginViewProps> {
23-
componentDidMount(): void {
23+
type LoginViewState = {
24+
authReady: boolean
25+
}
26+
27+
class LoginViewImpl extends React.Component<LoginViewProps, LoginViewState> {
28+
constructor(props: LoginViewProps) {
29+
super(props)
30+
this.state = { authReady: false }
31+
}
32+
33+
async componentDidMount(): Promise<void> {
2434
setTitle('Login')
35+
await initializeMsal()
36+
const silentOk = await loginSilently()
37+
if (silentOk) {
38+
this.props.navigate(NavigationPaths.HomeView())
39+
return
40+
}
41+
this.setState({ authReady: true })
2542
}
2643

2744
private handleLogin = async () => {
@@ -33,6 +50,10 @@ class LoginViewImpl extends React.Component<LoginViewProps> {
3350
}
3451

3552
render(): React.ReactNode {
53+
if (!this.state.authReady) {
54+
return <Loading />
55+
}
56+
3657
return (
3758
<Container
3859
component='main'

0 commit comments

Comments
 (0)