Last active
April 22, 2020 21:19
-
-
Save manoj-choudhari-git/62b8d64ea6be9736cdce48a727053c8e to your computer and use it in GitHub Desktop.
Authentication Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { BroadcastService, MsalService } from '@azure/msal-angular'; | |
import { Logger, CryptoUtils } from 'msal'; | |
@Injectable() | |
export class AuthService { | |
isIframe = false; | |
loggedIn = false; | |
constructor( | |
private broadcastService: BroadcastService, | |
private authService: MsalService) { } | |
initializeAuth() { | |
this.isIframe = window !== window.parent && !window.opener; | |
this.checkoutAccount(); | |
this.broadcastService.subscribe('msal:loginSuccess', () => { | |
this.checkoutAccount(); | |
}); | |
this.authService.handleRedirectCallback((authError, response) => { | |
if (authError) { | |
console.error('Redirect Error: ', authError.errorMessage); | |
return; | |
} | |
console.log('Redirect Success: ', response); | |
}); | |
this.authService.setLogger(new Logger((logLevel, message, piiEnabled) => { | |
console.log('MSAL Logging: ', message); | |
}, { | |
correlationId: CryptoUtils.createNewGuid(), | |
piiLoggingEnabled: false | |
})); | |
} | |
logIn() { | |
const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 | |
|| window.navigator.userAgent.indexOf('Trident/') > -1; | |
if (isIE) { | |
this.authService.loginRedirect(); | |
} | |
else { | |
this.authService.loginPopup(); | |
} | |
} | |
checkoutAccount() { | |
this.loggedIn = !!this.authService.getAccount(); | |
} | |
logOut() { | |
this.authService.logout(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment