-
Notifications
You must be signed in to change notification settings - Fork 6
/
authentication.service.ts
65 lines (53 loc) · 1.84 KB
/
authentication.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Injectable } from '@angular/core';
import * as Msal from 'msal';
import { User } from 'msal/lib-commonjs/User';
import { environment } from '../environments/environment';
@Injectable()
export class AuthenticationService {
private authority = 'https://login.microsoftonline.com/tfp/' + environment.tenant + '/' + environment.signUpSignInPolicy;
private clientApplication: Msal.UserAgentApplication;
constructor() {
this.clientApplication =
new Msal.UserAgentApplication(
environment.clientID,
this.authority,
this.authCallback);
}
public login(): void {
this.clientApplication.loginRedirect(environment.b2cScopes);
}
public logout(): void {
this.clientApplication.logout();
}
public isOnline(): boolean {
return this.clientApplication.getUser() != null;
}
public getUser(): User {
return this.clientApplication.getUser();
}
public getAuthenticationToken(): Promise<string> {
return this.clientApplication.acquireTokenSilent(environment.b2cScopes)
.then(token => {
console.log('Got silent access token: ', token);
return token;
}).catch(error => {
console.log('Could not silently retrieve token from storage.', error);
return this.clientApplication.acquireTokenPopup(environment.b2cScopes)
.then(token => {
console.log('Got popup access token: ', token);
return Promise.resolve(token);
}).catch(innererror => {
console.log('Could not retrieve token from popup.', innererror);
return Promise.resolve('');
});
});
}
private authCallback(errorDesc: any, token: any, error: any, tokenType: any) {
console.log('auth callback');
if (token) {
console.log('Got token', token);
} else {
console.log(error + ':' + errorDesc);
}
}
}