Skip to content

Instantly share code, notes, and snippets.

@thrixton thrixton/msal.service.ts
Last active Dec 19, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.