Last active
December 19, 2017 00:28
-
-
Save thrixton/a2e83b300d5ec29fccfb9987d87f781e to your computer and use it in GitHub Desktop.
MSAL service wrapper from https://github.com/Azure-Samples/active-directory-b2c-javascript-msal-singlepageapp/issues/9#issuecomment-347556074
This file contains hidden or 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 { AppConfig } from '../app.config'; | |
import {UserAgentApplication} from 'msal'; | |
@Injectable() | |
export class MsalService { | |
accessToken: string; | |
userAgentApplication: UserAgentApplication = null; | |
constructor() { | |
this.userAgentApplication = new UserAgentApplication( | |
AppConfig.tenantConfig.clientID, | |
AppConfig.tenantConfig.signInSignUp, | |
this._onTokenCallback | |
) | |
} | |
signIn(): void { | |
const clientApp = this.userAgentApplication; | |
const tenantConfig = AppConfig.tenantConfig; | |
clientApp.loginRedirect(tenantConfig.b2cScopes); | |
} | |
signOut(): void { | |
this.userAgentApplication.logout(); | |
} | |
isAuthenticated(): boolean{ | |
return this.userAgentApplication.getUser() != null; | |
} | |
getToken(){} | |
private _onTokenCallback = (errorDesc: any, token: any, error: any, tokenType: any) => { | |
if (token) { | |
this.accessToken = token; | |
} | |
if (errorDesc) { | |
if (errorDesc.indexOf('AADB2C90118') > -1) { | |
// forgot | |
console.log(errorDesc); | |
this.userAgentApplication = new UserAgentApplication( | |
AppConfig.tenantConfig.clientID, | |
AppConfig.tenantConfig.passwordReset, | |
this._onTokenCallback | |
); | |
this.signIn(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment