-
-
Save debugmodedotnet/8f291a474f5e859775ca243d57bec59b to your computer and use it in GitHub Desktop.
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
// This service acts performs various actions such with Azure AD B2C | |
// It use an external library MSAL.JS minumim version 1.4. Check package.json | |
// Modify environment.ts file to update client id, tenant , policy etc. on changes in Azure portal | |
// First draft by - Dhananjay Kumar in Sprint 0 | |
// Last modified by - Dhananjay Kumar in Sprint 0 | |
// Unit Test - 0 | |
import { Inject, Injectable } from '@angular/core'; | |
declare var Msal: any; | |
import { environment } from 'src/environments/environment'; | |
@Injectable({ | |
providedIn:'root' | |
}) | |
export class AuthenticationService { | |
private clientApplication: any; | |
private userRequest = { | |
scopes: environment.b2cScopes, | |
authority: environment.authority | |
}; | |
// configuration to create UserAgent | |
// Modify redirectUri to window.location.origin before pushing and production | |
// To cache the token uncomment cache section | |
private msalConfig: any = { | |
auth: { | |
clientId: environment.clientID, | |
authority: environment.authority, | |
redirectUri: 'http://localhost:4200/', | |
navigateToLoginRequestUrl: true, | |
validateAuthority: false, | |
}, | |
cache: { | |
cacheLocation: "localStorage", | |
storeAuthStateInCookie: true, // Set this to "true" to save cache in cookies to address trusted zones limitations in IE | |
}, | |
}; | |
constructor() {} | |
public login(): void { | |
this.clientApplication = new Msal.UserAgentApplication(this.msalConfig); | |
//this.clientApplication.loginRedirect(this.userRequest); | |
this.clientApplication.loginPopup(this.userRequest).then(response=>{ | |
console.log(response); | |
}) | |
.catch(err=>{ | |
console.log(err) | |
}) | |
console.log(this.clientApplication); | |
// console.log(this.userRequest); | |
// clientApplication.loginRedirect({ | |
// scopes: ['openid', 'profile'], | |
// authority: 'https://geek97.b2clogin.com/geek97.onmicrosoft.com/b2c_1_geek97login' | |
// }); | |
const user = this.clientApplication.getAccount(); | |
// saving returned user in local storage | |
localStorage.setItem('user', JSON.stringify(user)); | |
} | |
public logout(): void { | |
this.clientApplication.logout(); | |
localStorage.removeItem('user'); | |
} | |
public isOnline(): boolean { | |
return this.clientApplication.getAccount() != null; | |
// return true; | |
} | |
public getUser(): any { | |
return this.clientApplication.getAccount(); | |
} | |
//function to get token which will be further passed in the API | |
public getAuthenticationToken(): Promise<any> { | |
return this.clientApplication.acquireTokenSilent(this.userRequest) | |
.then(token => { | |
return token; | |
}).catch(error => { | |
return this.clientApplication.acquireTokenPopup(this.userRequest) | |
.then(token => { | |
return Promise.resolve(token); | |
}).catch(innererror => { | |
console.error('Could not retrieve token from popup.', innererror); | |
return Promise.resolve(''); | |
}); | |
}); | |
} | |
private authCallback(errorDesc: any, token: any, error: any, tokenType: any): void { | |
// if (error) { | |
// console.error(`${error} ${errorDesc}`); | |
// } | |
} | |
// function to check whether user is logged in | |
isLoggedIn(): boolean { | |
const user = JSON.parse(localStorage.getItem('user')); | |
console.log(user); | |
if (user) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment