forked from alexziskind1/nativescript-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
190 lines (160 loc) · 5.71 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/// <reference path="references.d.ts" />
import * as applicationModule from 'application';
import * as utils from 'utils/utils';
import { AuthHelperOffice365 } from './auth-helper-office365';
import { AuthHelperFacebook } from './auth-helper-facebook';
import { AuthHelperGoogle } from './auth-helper-google';
import { AuthHelperUaa } from './auth-helper-uaa';
import { AuthHelperLinkedIn } from './auth-helper-linkedin';
import { AuthHelperSalesforce } from './auth-helper-salesforce';
import { AuthHelperCustom } from './auth-helper-custom';
import * as TnsOAuth from './tns-oauth-interfaces';
export var instance: TnsOAuth.ITnsAuthHelper = null;
export function initOffice365(options: TnsOAuth.ITnsOAuthOptionsOffice365): Promise<any> {
return new Promise(function (resolve, reject) {
try {
if (instance !== null) {
reject("You already ran init");
return;
}
instance = new AuthHelperOffice365(options.clientId, options.scope);
resolve(instance);
} catch (ex) {
console.log("Error in AuthHelperOffice365.init: " + ex);
reject(ex);
}
});
}
export function initFacebook(options: TnsOAuth.ITnsOAuthOptionsFacebook): Promise<any> {
return new Promise(function (resolve, reject) {
try {
if (instance !== null) {
reject("You already ran init");
return;
}
instance = new AuthHelperFacebook(options.clientId, options.clientSecret, options.scope);
resolve(instance);
} catch (ex) {
console.log("Error in AuthHelperFacebook.init: " + ex);
reject(ex);
}
});
}
export function initGoogle(options: TnsOAuth.ITnsOAuthOptionsGoogle): Promise<any> {
return new Promise(function (resolve, reject) {
try {
if (instance !== null) {
reject("You already ran init");
return;
}
instance = new AuthHelperGoogle(options.clientId, options.scope);
resolve(instance);
} catch (ex) {
console.log("Error in AuthHelperGoogle.init: " + ex);
reject(ex);
}
});
}
export function initUaa(options: TnsOAuth.ITnsOAuthOptionsUaa): Promise<any> {
return new Promise(function (resolve, reject) {
try {
if (instance !== null) {
reject("You already ran init");
return;
}
instance = new AuthHelperUaa(options.authority, options.redirectUri, options.clientId, options.clientSecret, options.scope, options.cookieDomains, options.basicAuthHeader);
resolve(instance);
} catch (ex) {
console.log("Error in AuthHelperUaa.init: " + ex);
reject(ex);
}
});
}
export function initLinkedIn(options: TnsOAuth.ITnsOAuthOptionsLinkedIn): Promise<any> {
return new Promise(function (resolve, reject) {
try {
if (instance !== null) {
reject("You already ran init");
return;
}
instance = new AuthHelperLinkedIn(options.clientId, options.clientSecret, options.redirectUri, options.scope);
resolve(instance);
} catch (ex) {
console.log("Error in AuthHelperLinkedIn.init: " + ex);
reject(ex);
}
});
}
export function initSalesforce(options: TnsOAuth.ITnsOAuthOptionsSalesforce): Promise<any> {
return new Promise(function (resolve, reject) {
try {
if (instance !== null) {
reject("You already ran init");
return;
}
instance = new AuthHelperSalesforce(
options.authority,
options.clientId,
options.redirectUri,
options.responseType,
options.scope
);
resolve(instance);
} catch (ex) {
console.log("Error in AuthHelperSalesforce.init: " + ex);
reject(ex);
}
});
}
export function initCustom(options: TnsOAuth.ITnsOAuthOptionsCustom): Promise<any> {
return new Promise(function (resolve, reject) {
try {
if (instance !== null) {
reject("You already ran init");
return;
}
instance = new AuthHelperCustom(options.credentials, options.cookieDomains);
resolve(instance);
} catch (ex) {
console.log("Error in AuthHelperCustom.init: " + ex);
reject(ex);
}
});
}
export function accessToken(): string {
return instance.tokenResult.accessToken;
}
export function login(successPage?: string): Promise<string> {
return instance.login(successPage);
}
export function logout(successPage?: string): Promise<void> {
return instance.logout(successPage);
}
export function accessTokenExpired(): boolean {
return instance.accessTokenExpired();
}
export function ensureValidToken(): Promise<string> {
return new Promise((resolve, reject) => {
if (instance.accessTokenExpired()) {
if (instance.refreshTokenExpired()) {
login()
.then((response: string) => {
resolve(response);
})
.catch((er) => {
reject(er);
});
} else {
instance.refreshToken()
.then((result: string) => {
resolve(result);
})
.catch((er) => {
reject(ErrorEvent);
});
}
} else {
resolve(accessToken());
}
});
}