1- import { UserAgentApplication , Logger } from 'msal' ;
1+ import { LogLevel , PublicClientApplication } from '@azure/ msal-browser ' ;
22
33const ACCESS_TOKEN = 'rideshare_access_token' ;
4- const ID_TOKEN = 'rideshare_id_token' ;
5- const EXPIRES_AT = 'rideshare_expires_at' ;
64const USER_DETAILS = 'rideshare_user_details' ;
5+ let _user = null ;
6+ let _account = null ;
7+ let _loginRequest ;
8+
9+
710
8- let logger = new Logger ( ( level , message , containsPii ) => {
9- console . log ( message ) ;
10- } ) ;
1111
1212export class Authentication {
1313 constructor ( ) {
1414 // The window values below should by set by public/js/settings.js
15- this . _scopes = window . authScopes ;
16- this . _clientId = window . authClientId ;
17- this . _authority = window . authAuthority ;
18-
19- var cb = this . _tokenCallback . bind ( this ) ;
20- var opts = {
21- validateAuthority : false
15+ const msalConfig = {
16+ auth : {
17+ clientId : window . authClientId ,
18+ authority : window . authAuthority
19+ } ,
20+ cache : {
21+ cacheLocation : 'localStorage'
22+ } ,
23+ system : {
24+ loggerOptions : {
25+ loggerCallback : ( level , message , containsPii ) => {
26+ if ( containsPii ) {
27+ return ;
28+ }
29+ switch ( level ) {
30+ case LogLevel . Error :
31+ console . error ( message ) ;
32+ return ;
33+ case LogLevel . Info :
34+ console . info ( message ) ;
35+ return ;
36+ case LogLevel . Verbose :
37+ console . debug ( message ) ;
38+ return ;
39+ case LogLevel . Warning :
40+ console . warn ( message ) ;
41+ return ;
42+ default :
43+ return ;
44+ }
45+ } ,
46+ logLevel : LogLevel . Verbose
47+ }
48+ }
2249 } ;
23- this . _userAgentApplication = new UserAgentApplication (
24- this . _clientId ,
25- this . _authority ,
26- cb ,
27- opts
28- ) ;
29- }
3050
31- _tokenCallback ( errorDesc , token , error , tokenType ) {
32- this . _error = error ;
33- if ( tokenType === 'access_token' ) {
34- //localStorage.setItem(ACCESS_TOKEN, token);
35- // Please note: do NOT do this in production! Should grab this value from the auth service.
36- //let expiresAt = 60 * 1000 + new Date().getTime();
37- //localStorage.setItem(EXPIRES_AT, expiresAt);
38- this . _token = token ;
39- } else {
40- //localStorage.removeItem(ACCESS_TOKEN);
41- }
51+ this . _publicClientApplication = new PublicClientApplication ( msalConfig ) ;
52+
53+ _loginRequest = {
54+ scopes : window . authScopes
55+ } ;
4256 }
4357
4458 getUser ( ) {
45- return this . _userAgentApplication . getUser ( ) ;
59+ return this . _user ;
4660 }
4761
4862 getError ( ) {
4963 return this . _error ;
5064 }
5165
5266 getAccessToken ( ) {
53- return this . _userAgentApplication . acquireTokenSilent ( this . _scopes ) . then (
67+ return this . _publicClientApplication . acquireTokenSilent ( _loginRequest ) . then (
5468 accessToken => {
5569 return accessToken ;
5670 } ,
5771 error => {
58- return this . _userAgentApplication . acquireTokenPopup ( this . _scopes ) . then (
72+ return this . _publicClientApplication . acquireTokenPopup ( _loginRequest ) . then (
5973 accessToken => {
6074 return accessToken ;
6175 } ,
@@ -68,10 +82,11 @@ export class Authentication {
6882 }
6983
7084 login ( ) {
71- //this._userAgentApplication.loginRedirect(this._scopes);
72- return this . _userAgentApplication . loginPopup ( this . _scopes ) . then (
85+ return this . _publicClientApplication . loginPopup ( _loginRequest ) . then (
7386 idToken => {
74- const user = this . _userAgentApplication . getUser ( ) ;
87+ _account = idToken . account ;
88+ const user = idToken . account . user ;
89+ _user = user ;
7590 if ( user ) {
7691 return user ;
7792 } else {
@@ -85,19 +100,22 @@ export class Authentication {
85100 }
86101
87102 logout ( ) {
88- this . _userAgentApplication . logout ( ) ;
103+ const logoutRequest = {
104+ account : _account . homeAccountId ,
105+ } ;
106+ this . _publicClientApplication . logoutPopup ( logoutRequest ) ;
89107 }
90108
91109 isAuthenticated ( ) {
92110 return ! ! this . getUser ( ) ;
93111 }
94112
95113 getAccessTokenOrLoginWithPopup ( ) {
96- return this . _userAgentApplication
97- . acquireTokenSilent ( this . _scopes )
114+ return this . _publicClientApplication
115+ . acquireTokenSilent ( _loginRequest )
98116 . catch ( err => {
99- return this . _userAgentApplication . loginPopup ( ) . then ( ( ) => {
100- return this . _userAgentApplication . acquireTokenSilent ( this . _scopes ) ;
117+ return this . _publicClientApplication . loginPopup ( ) . then ( ( ) => {
118+ return this . _publicClientApplication . acquireTokenSilent ( _loginRequest ) ;
101119 } ) ;
102120 } ) ;
103121 }
0 commit comments