1
1
import { Router } from '@reach/router'
2
- import * as Msal from 'msal'
2
+ import { UserAgentApplication } from 'msal'
3
3
import * as React from 'react'
4
4
5
+ import './../styles/App.css'
5
6
import { AuthProvider } from './AuthContext'
6
7
import { AuthResponseBar } from './AuthResponseBar'
7
8
import { DefaultComponent } from './DefaultComponent'
@@ -10,16 +11,14 @@ import { Navbar } from './Navbar'
10
11
import { Person } from './Person'
11
12
import { Title } from './Title'
12
13
13
- import './../styles/App.css'
14
-
15
14
// Webpack will automatically replace this variable during build time
16
15
const appConfig = {
17
16
clientID : '' , // defaulted to '' when no OAuth client id is passed in
18
17
}
19
18
20
19
// initialize the UserAgentApplication globally so popup and iframe can run in the background
21
20
// short circuit userAgentApp. If clientID is null so is userAgentApp
22
- const userAgentApp = appConfig . clientID && new Msal . UserAgentApplication ( appConfig . clientID , null , null )
21
+ const userAgentApp = appConfig . clientID && new UserAgentApplication ( appConfig . clientID , null , ( ) => null )
23
22
24
23
// webpack replaces this variable on build time
25
24
let basepath = process . env . WEBPACK_PROP_UI_BASEPATH || '' // default to empty string for testing
@@ -45,18 +44,28 @@ export class App extends React.Component {
45
44
console . warn ( 'AAD Client ID has not been configured. If you are currently in production mode, see the \'deploy\' documentation for details on how to fix this.' )
46
45
this . setState ( { accessToken : '' } )
47
46
}
48
- } else { // normal or 'production' auth flow
47
+ } else if ( userAgentApp === '' ) { // normal or 'production' auth flow
48
+ alert ( 'userAgentApp not initialized' )
49
+ } else {
49
50
let accessToken = null
50
51
try {
51
52
if ( this . state . accessToken !== null ) {
52
53
// log out
53
- await userAgentApp . logout ( )
54
+ if ( userAgentApp ) {
55
+ await userAgentApp . logout ( )
56
+ } else {
57
+ alert ( 'userAgentApp not initialized' )
58
+ }
54
59
} else {
55
60
// log in
56
61
const graphScopes = [ appConfig . clientID ]
57
- await userAgentApp . loginPopup ( graphScopes )
58
- accessToken = await userAgentApp . acquireTokenSilent ( graphScopes ,
59
- 'https://login.microsoftonline.com/microsoft.onmicrosoft.com' )
62
+ if ( userAgentApp ) {
63
+ await userAgentApp . loginPopup ( graphScopes )
64
+ accessToken = await userAgentApp . acquireTokenSilent ( graphScopes ,
65
+ 'https://login.microsoftonline.com/microsoft.onmicrosoft.com' )
66
+ } else {
67
+ alert ( 'userAgentApp not initialized' )
68
+ }
60
69
}
61
70
this . setState ( { accessToken } )
62
71
} catch ( err ) {
@@ -75,12 +84,7 @@ export class App extends React.Component {
75
84
// by linking the accessToken to the app state we can be certain the
76
85
// context will always update and propogate the value to subscribed nodes
77
86
return (
78
- < AuthProvider value = { {
79
- accessToken : this . state . accessToken ,
80
- authResponse : this . state . authResponse ,
81
- handleAuth : this . handleAuth ,
82
- setAuthResponse : this . setAuthResponse ,
83
- } } >
87
+ < AuthProvider value = { { handleAuth : this . handleAuth } } >
84
88
< div className = 'app-container' >
85
89
< Navbar basepath = { basepath } />
86
90
< AuthResponseBar />
0 commit comments