/
LoginService.ts
247 lines (237 loc) · 8.43 KB
/
LoginService.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { Helper, CustomException } from "./Helper";
import {
IUserEntity,
LoginFormRequestEntity,
PhysicalVerificationLoginRequest,
IChangePasswordEntity
} from "./Entities"
export namespace LoginService {
/**
* To login with your credentials, call **loginWithCredentials()**. After successful login, this will redirect you to the redirect_url that you mentioned earlier while initialising the sdk.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/5gphdk6vapp56-classic-login#call-login-api for more details.
* @example
* ```js
* cidaas.loginWithCredentials({
* username: 'xxxx@gmail.com',
* username_type: 'email',
* password: '123456',
* requestId: 'your requestId',
* });
* ```
*/
export function loginWithCredentials(options: LoginFormRequestEntity) {
try {
const url = window.webAuthSettings.authority + "/login-srv/login";
let form = Helper.createForm(url, options)
document.body.appendChild(form);
form.submit();
} catch (ex) {
throw new CustomException(ex, 417);
}
};
/**
* To login with social providers, call **loginWithSocial()**. This will redirect you to the facebook login page.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/9mi5uqxhqlsm5-social-login#call-social-login-api for more details
* @example
* ```js
* cidaas.loginWithSocial({
* provider: 'facebook',
* requestId: 'your requestId',
* });
* ```
*/
export function loginWithSocial(
options: { provider: string; requestId: string; },
queryParams: { dc: string; device_fp: string }
) {
try {
var _serviceURL = window.webAuthSettings.authority + "/login-srv/social/login/" + options.provider.toLowerCase() + "/" + options.requestId;
if (queryParams && queryParams.dc && queryParams.device_fp) {
_serviceURL = _serviceURL + "?dc=" + queryParams.dc + "&device_fp=" + queryParams.device_fp;
}
window.location.href = _serviceURL;
} catch (ex) {
console.log(ex);
}
};
/**
* To register with social providers, call **registerWithSocial()**. This will redirect you to the facebook login page.
* @example
* Note: giving the queryParams is not required.
* ```js
* queryParams = {
* dc: dc,
* device_fp: device_fp
*
* cidaas.registerWithSocial({
* provider: 'facebook',
* requestId: 'your requestId',
* }, queryParams);
* ```
*/
export function registerWithSocial(
options: { provider: string; requestId: string; },
queryParams: { dc: string; device_fp: string }) {
try {
var _serviceURL = window.webAuthSettings.authority + "/login-srv/social/register/" + options.provider.toLowerCase() + "/" + options.requestId;
if (queryParams && queryParams.dc && queryParams.device_fp) {
_serviceURL = _serviceURL + "?dc=" + queryParams.dc + "&device_fp=" + queryParams.device_fp;
}
window.location.href = _serviceURL;
} catch (ex) {
console.log(ex);
}
};
/**
* To authenticate without using password, call **passwordlessLogin()**.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/k1lwsraxk0rjc-login-passwordless-request for more details.
* @example
* ```js
* cidaas.passwordlessLogin({
* requestId: 'your requestId',
* sub: 'your user sub',
* statusId: 'status id from authenticateMFA()'
* verificationType: 'your verificationType. e.g. email'
* });
* ```
*/
export function passwordlessLogin(options: PhysicalVerificationLoginRequest) {
try {
const url = window.webAuthSettings.authority + "/login-srv/verification/login";
let form = Helper.createForm(url, options)
document.body.appendChild(form);
form.submit();
} catch (ex) {
throw new CustomException(ex, 417);
}
};
/**
* To continue after Consent acceptance, call **consentContinue()**.
* @example
* ```js
* cidaas.consentContinue({
* name: 'your consent name',
* version: 'your consent version',
* client_id: 'your client id',
* track_id: 'your track id',
* sub: 'your sub'
* });
* ```
*/
export function consentContinue(options: {track_id: string}) {
try {
const url = window.webAuthSettings.authority + "/login-srv/precheck/continue/" + options.track_id;
let form = Helper.createForm(url, options)
document.body.appendChild(form);
form.submit();
} catch (ex) {
throw new CustomException(ex, 417);
}
};
/**
* To continue after MFA completion, call **mfaContinue()**.
* @example
* ```js
* cidaas.mfaContinue({
* trackingCode: 'your tracking Code',
* track_id: 'your track id',
* sub: 'your sub',
* requestId: 'your request id'
* });
* ```
*/
export function mfaContinue(options: PhysicalVerificationLoginRequest & { track_id: string }) {
try {
const url = window.webAuthSettings.authority + "/login-srv/precheck/continue/" + options.track_id;
let form = Helper.createForm(url, options)
document.body.appendChild(form);
form.submit();
} catch (ex) {
throw new CustomException(ex, 417);
}
};
/**
* to handle changing password by first login attempt after registration, call **firstTimeChangePassword()**.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/fd8f478d96f58-continue-authentication-flow-after-prechecks for more details.
* @example
* ```js
* cidaas.firstTimeChangePassword({
* sub: 'your sub',
* old_password: 'your old password',
* new_password: 'your new password',
* confirm_password: 'your new password',
* loginSettingsId: 'loginSettingsId'
* });
* ```
*/
export function firstTimeChangePassword(options: IChangePasswordEntity) {
try {
const url = window.webAuthSettings.authority + "/login-srv/precheck/continue/" + options.loginSettingsId;
let form = Helper.createForm(url, options)
document.body.appendChild(form);
form.submit();
} catch (ex) {
throw new CustomException(ex, 417);
}
};
/**
* For progressive registration, call **progressiveRegistration()**. While logging in If the API returns 417 with the error message MissingRequiredFields, call the **getMissingFields** to get the list of missing fileds and proceed with progressive registration. In the sample request only the required fields are added, however you must provide the missing fields along with the required fields.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/l7sknp2pytryr-progressive-registration for more details.
* @example
* ```js
* const options = {
* sub: 'your sub',
* }
* const headers = {
* trackId: 'the track id received while logging in',
* requestId: 'request id of the session',
* acceptlanguage: 'your locale/browser locale (OPTIONAL)',
* }
* cidaas.progressiveRegistration(options, headers)
* .then(function(response) {
* // type your code here
* })
* .catch(function (ex) {
* // your failure code here
* });
* ```
*/
export function progressiveRegistration(options: IUserEntity, headers: {
requestId: string;
trackId: string;
acceptlanguage: string;
}) {
var serviceURL = window.webAuthSettings.authority + "/login-srv/progressive/update/user";
return Helper.createHttpPromise(options, serviceURL, undefined, "POST", undefined, headers);
};
/**
* To automatically do user login after successful registration, call **loginAfterRegister()**. Make sure to turn on "auto login after register" switch on the admin ui to activate loginAfterRegister flow.
* Please refer to the api document https://docs.cidaas.com/docs/cidaas-iam/qwwamc2f378wi-auto-login-after-register for more details.
* @example
* ```js
* cidaas.loginAfterRegister({
* device_id: 'your device id',
* dc: 'device capacity'
* rememberMe: false,
* trackId: 'your track id',
* device_fp: 'device fingerprint'
* });
* ```
*/
export function loginAfterRegister(options: {
device_id?: string;
dc?: string;
rememberMe?: boolean;
trackId?: string;
device_fp?: string;
}) {
try {
const url = window.webAuthSettings.authority + "/login-srv/login/handle/afterregister/" + options.trackId;
let form = Helper.createForm(url, options)
document.body.appendChild(form);
form.submit();
} catch (ex) {
throw new CustomException(ex, 417);
}
};
}