-
Notifications
You must be signed in to change notification settings - Fork 53
/
auth-helper.ts
71 lines (63 loc) · 2.23 KB
/
auth-helper.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
66
67
68
69
70
71
/// <reference path="references.d.ts" />
import * as tnsOauth from './tns-oauth';
import * as TnsOAuth from './tns-oauth-interfaces';
export class AuthHelper {
public credentials: TnsOAuth.ITnsOAuthCredentials;
public tokenResult: TnsOAuth.ITnsOAuthTokenResult;
constructor() {
this.tokenResult = tnsOauth.getTokenFromCache();
}
public login(successPage?: string): Promise<string> {
return new Promise((resolve, reject) => {
tnsOauth.loginViaAuthorizationCodeFlow(this.credentials, successPage)
.then((response: TnsOAuth.ITnsOAuthTokenResult) => {
this.tokenResult = response;
resolve(response.accessToken);
})
.catch((er) => {
reject(er);
});
});
}
public refreshToken(): Promise<string> {
return new Promise((resolve, reject) => {
tnsOauth.refreshToken(this.credentials)
.then((response: TnsOAuth.ITnsOAuthTokenResult) => {
this.tokenResult = response;
resolve(response.accessToken);
})
.catch((er) => {
reject(er);
});
});
}
public _logout(successPage?: string, cookieDomains?: string[]): Promise<void> {
return new Promise<void>((resolve, reject) => {
try {
tnsOauth.logout(cookieDomains, successPage);
this.tokenResult = null;
resolve();
} catch (er) {
reject(er);
}
});
}
public accessTokenExpired(): boolean {
if (this.tokenResult && this.tokenResult.accessTokenExpiration) {
return this.tokenResult.accessTokenExpiration < (new Date());
} else {
return true;
}
}
public refreshTokenExpired(): boolean {
if (this.tokenResult && this.tokenResult.refreshTokenExpiration) {
if (this.tokenResult.refreshTokenExpiration) {
return this.tokenResult.refreshTokenExpiration < (new Date());
} else {
return false;
}
} else {
return true;
}
}
}