11import { Router } from '@reach/router'
2- import * as Msal from 'msal'
2+ import { UserAgentApplication } from 'msal'
33import * as React from 'react'
44
5+ import './../styles/App.css'
56import { AuthProvider } from './AuthContext'
67import { AuthResponseBar } from './AuthResponseBar'
78import { DefaultComponent } from './DefaultComponent'
@@ -10,16 +11,14 @@ import { Navbar } from './Navbar'
1011import { Person } from './Person'
1112import { Title } from './Title'
1213
13- import './../styles/App.css'
14-
1514// Webpack will automatically replace this variable during build time
1615const appConfig = {
1716 clientID : '' , // defaulted to '' when no OAuth client id is passed in
1817}
1918
2019// initialize the UserAgentApplication globally so popup and iframe can run in the background
2120// 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 )
2322
2423// webpack replaces this variable on build time
2524let basepath = process . env . WEBPACK_PROP_UI_BASEPATH || '' // default to empty string for testing
@@ -45,18 +44,28 @@ export class App extends React.Component {
4544 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.' )
4645 this . setState ( { accessToken : '' } )
4746 }
48- } else { // normal or 'production' auth flow
47+ } else if ( userAgentApp === '' ) { // normal or 'production' auth flow
48+ alert ( 'userAgentApp not initialized' )
49+ } else {
4950 let accessToken = null
5051 try {
5152 if ( this . state . accessToken !== null ) {
5253 // log out
53- await userAgentApp . logout ( )
54+ if ( userAgentApp ) {
55+ await userAgentApp . logout ( )
56+ } else {
57+ alert ( 'userAgentApp not initialized' )
58+ }
5459 } else {
5560 // log in
5661 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+ }
6069 }
6170 this . setState ( { accessToken } )
6271 } catch ( err ) {
@@ -75,12 +84,7 @@ export class App extends React.Component {
7584 // by linking the accessToken to the app state we can be certain the
7685 // context will always update and propogate the value to subscribed nodes
7786 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 } } >
8488 < div className = 'app-container' >
8589 < Navbar basepath = { basepath } />
8690 < AuthResponseBar />
0 commit comments