Skip to content

Instantly share code, notes, and snippets.

@thrixton
Last active December 19, 2017 00:28
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 thrixton/a2e83b300d5ec29fccfb9987d87f781e to your computer and use it in GitHub Desktop.
Save thrixton/a2e83b300d5ec29fccfb9987d87f781e to your computer and use it in GitHub Desktop.
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