-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
405 lines (390 loc) · 15.6 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* Copyright (c) 2022, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
import { getNormalisedUserContext } from "../../utils";
import { RecipeFunctionOptions } from "../recipeModule/types";
import Multitenancy from "../multitenancy/recipe";
import { StateObject } from "../thirdparty/types";
import Recipe from "./recipe";
import { UserInput, RecipeInterface, PreAPIHookContext, PostAPIHookContext } from "./types";
import { User } from "../../types";
export default class RecipeWrapper {
static init(config?: UserInput) {
return Recipe.init(config);
}
static signOut(input?: { userContext?: any }) {
return Recipe.getInstanceOrThrow().signOut({
userContext: getNormalisedUserContext(input?.userContext),
});
}
/**
* Submit a new password for the user
*
* @param formFields List of fields to send to the API exposed by the backend SDK (Refer to the {@link https://supertokens.com/docs/fdi API spec} to know more)
*
* @param userContext (OPTIONAL) Refer to {@link https://supertokens.com/docs/thirdpartyemailpassword/advanced-customizations/user-context the documentation}
*
* @param options (OPTIONAL) Use this to configure additional properties (for example pre api hooks)
*
* @returns `{status: "OK"}` if successfull
*
* @returns `{status: "RESET_PASSWORD_INVALID_TOKEN_ERROR"}` if the token in the URL is invalid
*
* @returns `{status: "FIELD_ERROR", formFields}` if the form field values are incorrect
*
* @throws STGeneralError if the API exposed by the backend SDKs returns `status: "GENERAL_ERROR"`
*/
static submitNewPassword(input: {
formFields: {
id: string;
value: string;
}[];
options?: RecipeFunctionOptions;
userContext?: any;
}): Promise<
| {
status: "OK";
fetchResponse: Response;
}
| {
status: "RESET_PASSWORD_INVALID_TOKEN_ERROR";
fetchResponse: Response;
}
| {
status: "FIELD_ERROR";
formFields: {
id: string;
error: string;
}[];
fetchResponse: Response;
}
> {
return Recipe.getInstanceOrThrow().recipeImplementation.submitNewPassword({
...input,
userContext: getNormalisedUserContext(input.userContext),
});
}
/**
* Send an email to the user for password reset
*
* @param formFields List of fields to send to the API exposed by the backend SDK (Refer to the {@link https://supertokens.com/docs/fdi API spec} to know more)
*
* @param userContext (OPTIONAL) Refer to {@link https://supertokens.com/docs/thirdpartyemailpassword/advanced-customizations/user-context the documentation}
*
* @param options (OPTIONAL) Use this to configure additional properties (for example pre api hooks)
*
* @returns `{status: "OK"}` if successfull
*
* @returns `{status: "FIELD_ERROR", formFields}` if the formFields dont match the ones in the configured in the backend SDKs
*
* @throws STGeneralError if the API exposed by the backend SDKs returns `status: "GENERAL_ERROR"`
*/
static sendPasswordResetEmail(input: {
formFields: {
id: string;
value: string;
}[];
options?: RecipeFunctionOptions;
userContext?: any;
}): Promise<
| {
status: "OK";
fetchResponse: Response;
}
| {
status: "PASSWORD_RESET_NOT_ALLOWED";
reason: string;
fetchResponse: Response;
}
| {
status: "FIELD_ERROR";
formFields: {
id: string;
error: string;
}[];
fetchResponse: Response;
}
> {
return Recipe.getInstanceOrThrow().recipeImplementation.sendPasswordResetEmail({
...input,
userContext: getNormalisedUserContext(input.userContext),
});
}
/**
* Get the current login state from storage, this is also used when calling signInUp
*
* @param userContext Refer to {@link https://supertokens.com/docs/thirdpartyemailpassword/advanced-customizations/user-context the documentation}
*
* @returns State object from storage
*/
static getStateAndOtherInfoFromStorage<CustomStateProperties>(input?: {
userContext?: any;
}): (StateObject & CustomStateProperties) | undefined {
return Recipe.getInstanceOrThrow().recipeImplementation.getStateAndOtherInfoFromStorage({
...input,
userContext: getNormalisedUserContext(input?.userContext),
});
}
/**
* Check if an email exists
*
* @param email The email to check
*
* @param userContext (OPTIONAL) Refer to {@link https://supertokens.com/docs/thirdpartyemailpassword/advanced-customizations/user-context the documentation}
*
* @param options (OPTIONAL) Use this to configure additional properties (for example pre api hooks)
*
* @returns `{status: "OK", doesExist: boolean}`
*
* @throws STGeneralError if the API exposed by the backend SDKs returns `status: "GENERAL_ERROR"`
*/
static doesEmailExist(input: { email: string; options?: RecipeFunctionOptions; userContext?: any }): Promise<{
status: "OK";
doesExist: boolean;
fetchResponse: Response;
}> {
return Recipe.getInstanceOrThrow().recipeImplementation.doesEmailExist({
...input,
userContext: getNormalisedUserContext(input.userContext),
});
}
/**
* Sign up a user with email and password
*
* @param formFields List of fields to send to the API exposed by the backend SDK (Refer to the {@link https://supertokens.com/docs/fdi API spec} to know more). Note that the form fields must match the ones configured in the backend SDKs
*
* @param userContext (OPTIONAL) Refer to {@link https://supertokens.com/docs/thirdpartyemailpassword/advanced-customizations/user-context the documentation}
*
* @param options (OPTIONAL) Use this to configure additional properties (for example pre api hooks)
*
* @returns `{status: "OK"}` if successfull
*
* @returns `{status: "FIELD_ERROR", formFields}` if the formFields dont match the ones in the configured in the backend SDKs
*
* @returns `{status: "SIGN_UP_NOT_ALLOWED"}` if the user can't sign up because of security reasons
*
* @throws STGeneralError if the API exposed by the backend SDKs returns `status: "GENERAL_ERROR"`
*/
static emailPasswordSignUp(input: {
formFields: {
id: string;
value: string;
}[];
options?: RecipeFunctionOptions;
userContext?: any;
}): Promise<
| {
status: "OK";
user: User;
fetchResponse: Response;
}
| {
status: "FIELD_ERROR";
formFields: {
id: string;
error: string;
}[];
fetchResponse: Response;
}
| {
status: "SIGN_UP_NOT_ALLOWED";
reason: string;
fetchResponse: Response;
}
> {
return Recipe.getInstanceOrThrow().recipeImplementation.emailPasswordSignUp({
...input,
userContext: getNormalisedUserContext(input.userContext),
});
}
/**
* Sign in a user with email and password
*
* @param formFields List of fields to send to the API exposed by the backend SDK (Refer to the {@link https://supertokens.com/docs/fdi API spec} to know more). Note that the form fields must match the ones configured in the backend SDKs
*
* @param userContext (OPTIONAL) Refer to {@link https://supertokens.com/docs/thirdpartyemailpassword/advanced-customizations/user-context the documentation}
*
* @param options (OPTIONAL) Use this to configure additional properties (for example pre api hooks)
*
* @returns `{status: "OK"}` if successfull
*
* @returns `{status: "FIELD_ERROR", formFields}` if the formFields dont match the ones in the configured in the backend SDKs
*
* @returns `{status: "WRONG_CREDENTIALS_ERROR"}` if the credentials are invalid
*
* @returns `{status: "SIGN_IN_NOT_ALLOWED"}` if the user can't sign in because of security reasons
*
* @throws STGeneralError if the API exposed by the backend SDKs returns `status: "GENERAL_ERROR"`
*/
static emailPasswordSignIn(input: {
formFields: {
id: string;
value: string;
}[];
options?: RecipeFunctionOptions;
userContext?: any;
}): Promise<
| {
status: "OK";
user: User;
fetchResponse: Response;
}
| {
status: "FIELD_ERROR";
formFields: {
id: string;
error: string;
}[];
fetchResponse: Response;
}
| {
status: "WRONG_CREDENTIALS_ERROR";
fetchResponse: Response;
}
| {
status: "SIGN_IN_NOT_ALLOWED";
reason: string;
fetchResponse: Response;
}
> {
return Recipe.getInstanceOrThrow().recipeImplementation.emailPasswordSignIn({
...input,
userContext: getNormalisedUserContext(input.userContext),
});
}
/**
* Reads and returns the reset password token from the current URL
*
* @param userContext Refer to {@link https://supertokens.com/docs/thirdpartyemailpassword/advanced-customizations/user-context the documentation}
*
* @returns The "token" query parameter from the current location
*/
static getResetPasswordTokenFromURL(input?: { userContext?: any }): string {
return Recipe.getInstanceOrThrow().recipeImplementation.getResetPasswordTokenFromURL({
...input,
userContext: getNormalisedUserContext(input?.userContext),
});
}
/**
* Reads and returns the tenant id from the current URL
*
* @param userContext Refer to {@link https://supertokens.com/docs/thirdpartyemailpassword/advanced-customizations/user-context the documentation}
*
* @returns The "tenantId" query parameter from the current location
*/
static getTenantIdFromURL(input?: { userContext?: any }): string | undefined {
return Recipe.getInstanceOrThrow().recipeImplementation.getTenantIdFromURL({
...input,
userContext: getNormalisedUserContext(input?.userContext),
});
}
/**
* Sign up/Sign in the user, this method uses the login attempt information from storage
*
* @param userContext (OPTIONAL) Refer to {@link https://supertokens.com/docs/thirdpartyemailpassword/advanced-customizations/user-context the documentation}
*
* @param options (OPTIONAL) Use this to configure additional properties (for example pre api hooks)
*
* @returns `{status: OK, user, createdNewRecipeUser: boolean}` if succesful
*
* @returns `{status: "NO_EMAIL_GIVEN_BY_PROVIDER"}` if the correct scopes are not configured for the third party provider
* @returns `{status: "SIGN_IN_UP_NOT_ALLOWED", reason: string}` if signing in with this user is not allowed if because of account linking conflicts
*/
static thirdPartySignInAndUp(input?: { userContext?: any; options?: RecipeFunctionOptions }): Promise<
| {
status: "OK";
user: User;
createdNewRecipeUser: boolean;
fetchResponse: Response;
}
| {
status: "NO_EMAIL_GIVEN_BY_PROVIDER";
fetchResponse: Response;
}
| {
status: "SIGN_IN_UP_NOT_ALLOWED";
reason: string;
fetchResponse: Response;
}
> {
return Recipe.getInstanceOrThrow().recipeImplementation.thirdPartySignInAndUp({
...input,
userContext: getNormalisedUserContext(input?.userContext),
});
}
/**
* Get the URL that should be opened for third party authentication
*
* @param thirdPartyId The identifier for the third party provider. The value must match one of the providers configured with the backend SDK
*
* @param frontendRedirectURI The URL that should be used for redirection after the third party flow finishes.
*
* @param redirectURIOnProviderDashboard (OPTIONAL) The redirect URL that is configured on the provider dashboard. Not required if the value is same as frontendRedirectURI
*
* @param userContext (OPTIONAL) Refer to {@link https://supertokens.com/docs/thirdparty/advanced-customizations/user-context the documentation}
*
* @param options (OPTIONAL) Use this to configure additional properties (for example pre api hooks)
*
* @returns URL string
*
* @throws STGeneralError if the API exposed by the backend SDKs returns `status: "GENERAL_ERROR"`
*/
static async getAuthorisationURLWithQueryParamsAndSetState(input: {
thirdPartyId: string;
frontendRedirectURI: string;
redirectURIOnProviderDashboard?: string;
userContext?: any;
options?: RecipeFunctionOptions;
}): Promise<string> {
const userContext = getNormalisedUserContext(input.userContext);
const recipe = Recipe.getInstanceOrThrow();
const tenantId = await Multitenancy.getInstanceOrThrow().recipeImplementation.getTenantId({ userContext });
return recipe.recipeImplementation.getAuthorisationURLWithQueryParamsAndSetState({
tenantId,
...input,
userContext,
});
}
}
const init = RecipeWrapper.init;
const submitNewPassword = RecipeWrapper.submitNewPassword;
const sendPasswordResetEmail = RecipeWrapper.sendPasswordResetEmail;
const getStateAndOtherInfoFromStorage = RecipeWrapper.getStateAndOtherInfoFromStorage;
const doesEmailExist = RecipeWrapper.doesEmailExist;
const emailPasswordSignUp = RecipeWrapper.emailPasswordSignUp;
const emailPasswordSignIn = RecipeWrapper.emailPasswordSignIn;
const thirdPartySignInAndUp = RecipeWrapper.thirdPartySignInAndUp;
const getAuthorisationURLWithQueryParamsAndSetState = RecipeWrapper.getAuthorisationURLWithQueryParamsAndSetState;
const getResetPasswordTokenFromURL = RecipeWrapper.getResetPasswordTokenFromURL;
const getTenantIdFromURL = RecipeWrapper.getTenantIdFromURL;
const signOut = RecipeWrapper.signOut;
export {
init,
submitNewPassword,
sendPasswordResetEmail,
getStateAndOtherInfoFromStorage,
doesEmailExist,
emailPasswordSignUp,
emailPasswordSignIn,
thirdPartySignInAndUp,
getAuthorisationURLWithQueryParamsAndSetState,
signOut,
getResetPasswordTokenFromURL,
getTenantIdFromURL,
UserInput,
RecipeInterface,
RecipeFunctionOptions,
PreAPIHookContext,
PostAPIHookContext,
};