Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Last active April 22, 2020 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/62b8d64ea6be9736cdce48a727053c8e to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/62b8d64ea6be9736cdce48a727053c8e to your computer and use it in GitHub Desktop.
Authentication Service
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