-
Notifications
You must be signed in to change notification settings - Fork 81
/
types.ts
449 lines (380 loc) · 12 KB
/
types.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* Copyright (c) 2021, 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 type { InputProps } from "./components/library/input";
import type { ResetPasswordEmail } from "./components/themes/resetPasswordUsingToken/resetPasswordEmail";
import type { SubmitNewPassword } from "./components/themes/resetPasswordUsingToken/submitNewPassword";
import type { SignIn } from "./components/themes/signInAndUp/signIn";
import type { SignInFooter } from "./components/themes/signInAndUp/signInFooter";
import type { SignInForm } from "./components/themes/signInAndUp/signInForm";
import type { SignInHeader } from "./components/themes/signInAndUp/signInHeader";
import type { SignUp } from "./components/themes/signInAndUp/signUp";
import type { SignUpFooter } from "./components/themes/signInAndUp/signUpFooter";
import type { SignUpForm } from "./components/themes/signInAndUp/signUpForm";
import type { SignUpHeader } from "./components/themes/signInAndUp/signUpHeader";
import type { ComponentOverride } from "../../components/componentOverride/componentOverride";
import type {
APIFormField,
FeatureBaseConfig,
FormField,
FormFieldBaseConfig,
NormalisedBaseConfig,
NormalisedFormField,
ThemeBaseProps,
} from "../../types";
import type {
GetRedirectionURLContext as AuthRecipeModuleGetRedirectionURLContext,
OnHandleEventContext as AuthRecipeModuleOnHandleEventContext,
User,
Config as AuthRecipeModuleConfig,
NormalisedConfig as NormalisedAuthRecipeModuleConfig,
UserInput as AuthRecipeModuleUserInput,
} from "../authRecipe/types";
import type React from "react";
import type { Dispatch } from "react";
import type { OverrideableBuilder } from "supertokens-js-override";
import type { RecipeInterface } from "supertokens-web-js/recipe/emailpassword";
export type ComponentOverrideMap = {
EmailPasswordSignIn_Override?: ComponentOverride<typeof SignIn>;
EmailPasswordSignInFooter_Override?: ComponentOverride<typeof SignInFooter>;
EmailPasswordSignInForm_Override?: ComponentOverride<typeof SignInForm>;
EmailPasswordSignInHeader_Override?: ComponentOverride<typeof SignInHeader>;
EmailPasswordSignUp_Override?: ComponentOverride<typeof SignUp>;
EmailPasswordSignUpFooter_Override?: ComponentOverride<typeof SignUpFooter>;
EmailPasswordSignUpForm_Override?: ComponentOverride<typeof SignUpForm>;
EmailPasswordSignUpHeader_Override?: ComponentOverride<typeof SignUpHeader>;
EmailPasswordResetPasswordEmail_Override?: ComponentOverride<typeof ResetPasswordEmail>;
EmailPasswordSubmitNewPassword_Override?: ComponentOverride<typeof SubmitNewPassword>;
};
export type UserInput = {
signInAndUpFeature?: SignInAndUpFeatureUserInput;
resetPasswordUsingTokenFeature?: ResetPasswordUsingTokenUserInput;
override?: {
functions?: (
originalImplementation: RecipeInterface,
builder?: OverrideableBuilder<RecipeInterface>
) => RecipeInterface;
};
} & AuthRecipeModuleUserInput<GetRedirectionURLContext, PreAndPostAPIHookAction, OnHandleEventContext>;
export type Config = UserInput &
AuthRecipeModuleConfig<GetRedirectionURLContext, PreAndPostAPIHookAction, OnHandleEventContext>;
export type NormalisedConfig = {
signInAndUpFeature: NormalisedSignInAndUpFeatureConfig;
resetPasswordUsingTokenFeature: NormalisedResetPasswordUsingTokenFeatureConfig;
override: {
functions: (
originalImplementation: RecipeInterface,
builder?: OverrideableBuilder<RecipeInterface>
) => RecipeInterface;
};
} & NormalisedAuthRecipeModuleConfig<GetRedirectionURLContext, PreAndPostAPIHookAction, OnHandleEventContext>;
export type SignInAndUpFeatureUserInput = {
/*
* Disable default implementation with default routes.
*/
disableDefaultUI?: boolean;
/*
* Should default to Sign up form.
*/
defaultToSignUp?: boolean;
/*
* SignUp form config.
*/
signUpForm?: SignUpFormFeatureUserInput;
/*
* SignIn form config.
*/
signInForm?: SignInFormFeatureUserInput;
};
export type NormalisedSignInAndUpFeatureConfig = {
/*
* Disable default implementation with default routes.
*/
disableDefaultUI: boolean;
/*
* Default to sign up form.
*/
defaultToSignUp: boolean;
/*
* SignUp form config.
*/
signUpForm: NormalisedSignUpFormFeatureConfig;
/*
* SignIn form config.
*/
signInForm: NormalisedSignInFormFeatureConfig;
};
export type SignUpFormFeatureUserInput = FeatureBaseConfig & {
/*
* Form fields for SignUp.
*/
formFields?: FormFieldSignUpConfig[];
/*
* Privacy policy link for sign up form.
*/
privacyPolicyLink?: string;
/*
* Terms and conditions link for sign up form.
*/
termsOfServiceLink?: string;
};
export type NormalisedSignUpFormFeatureConfig = NormalisedBaseConfig & {
/*
* Normalised form fields for SignUp.
*/
formFields: NormalisedFormField[];
/*
* Privacy policy link for sign up form.
*/
privacyPolicyLink?: string;
/*
* Terms and conditions link for sign up form.
*/
termsOfServiceLink?: string;
};
export type SignInFormFeatureUserInput = FeatureBaseConfig & {
/*
* Form fields for SignIn.
*/
formFields?: FormFieldSignInConfig[];
};
export type NormalisedSignInFormFeatureConfig = NormalisedBaseConfig & {
/*
* Normalised form fields for SignIn.
*/
formFields: NormalisedFormField[];
};
export type FormFieldSignInConfig = FormFieldBaseConfig;
export type FormFieldSignUpConfig = FormField;
export type ResetPasswordUsingTokenUserInput = {
/*
* Disable default implementation with default routes.
*/
disableDefaultUI?: boolean;
/*
* submitNewPasswordForm config.
*/
submitNewPasswordForm?: FeatureBaseConfig;
/*
* enterEmailForm config.
*/
enterEmailForm?: FeatureBaseConfig;
};
export type NormalisedResetPasswordUsingTokenFeatureConfig = {
/*
* Disable default implementation with default routes.
*/
disableDefaultUI: boolean;
/*
* Normalised submitNewPasswordForm config.
*/
submitNewPasswordForm: NormalisedSubmitNewPasswordForm;
/*
* Normalised enterEmailForm config.
*/
enterEmailForm: NormalisedEnterEmailForm;
};
export type NormalisedSubmitNewPasswordForm = FeatureBaseConfig & {
formFields: NormalisedFormField[];
};
export type NormalisedEnterEmailForm = FeatureBaseConfig & {
formFields: NormalisedFormField[];
};
/*
* Props Types.
*/
type FormThemeBaseProps = ThemeBaseProps & {
/*
* Form fields to use in the signin form.
*/
formFields: FormFieldThemeProps[];
error: string | undefined;
};
export type SignInThemeProps = FormThemeBaseProps & {
recipeImplementation: RecipeInterface;
clearError: () => void;
onError: (error: string) => void;
config: NormalisedConfig;
signUpClicked?: () => void;
forgotPasswordClick: () => void;
onSuccess: () => void;
};
export type SignUpThemeProps = FormThemeBaseProps & {
recipeImplementation: RecipeInterface;
clearError: () => void;
onError: (error: string) => void;
config: NormalisedConfig;
signInClicked?: () => void;
onSuccess: () => void;
};
export type SignInAndUpThemeProps = {
signInForm: SignInThemeProps;
signUpForm: SignUpThemeProps;
featureState: {
isSignUp: boolean;
};
dispatch: Dispatch<EmailPasswordSignInAndUpAction>;
config: NormalisedConfig;
userContext?: any;
};
export type FormFieldThemeProps = NormalisedFormField & {
/*
* Custom component that replaces the label entirely
*/
labelComponent?: JSX.Element;
/*
* Custom component that replaces the standard input component
*/
inputComponent?: React.FC<InputProps>;
/*
* Show Is required (*) next to label
*/
showIsRequired?: boolean;
/*
* Clears the field after calling the API.
*/
clearOnSubmit?: boolean;
};
export type FormFieldError = {
/*
* Field id.
*/
id: string;
/*
* Error message.
*/
error: string;
};
export type PreAndPostAPIHookAction =
| "EMAIL_PASSWORD_SIGN_UP"
| "EMAIL_PASSWORD_SIGN_IN"
| "SEND_RESET_PASSWORD_EMAIL"
| "SUBMIT_NEW_PASSWORD"
| "EMAIL_EXISTS";
export type PreAPIHookContext = {
/*
* Pre API Hook action.
*/
action: PreAndPostAPIHookAction;
/*
* Request object containing query params, body, headers.
*/
requestInit: RequestInit;
/*
* URL
*/
url: string;
userContext: any;
};
export type GetRedirectionURLContext =
| AuthRecipeModuleGetRedirectionURLContext
| {
/*
* Get Redirection URL Context
*/
action: "RESET_PASSWORD";
};
export type OnHandleEventContext =
| AuthRecipeModuleOnHandleEventContext
| {
/*
* On Handle Event actions
*/
action: "RESET_PASSWORD_EMAIL_SENT" | "PASSWORD_RESET_SUCCESSFUL";
userContext: any;
}
| {
action: "SUCCESS";
isNewUser: boolean;
user: { id: string; email: string };
userContext: any;
};
export type ResetPasswordUsingTokenThemeProps = {
enterEmailForm: EnterEmailProps;
submitNewPasswordForm: SubmitNewPasswordProps | undefined;
config: NormalisedConfig;
userContext?: any;
};
export type EnterEmailProps = FormThemeBaseProps & {
recipeImplementation: RecipeInterface;
error: string | undefined;
clearError: () => void;
onError: (error: string) => void;
config: NormalisedConfig;
onBackButtonClicked: () => void;
};
export type SubmitNewPasswordProps = FormThemeBaseProps & {
recipeImplementation: RecipeInterface;
error: string | undefined;
clearError: () => void;
onError: (error: string) => void;
config: NormalisedConfig;
onSignInClicked: () => void;
token: string;
};
export type EnterEmailStatus = "READY" | "SENT";
export type SubmitNewPasswordStatus = "READY" | "SUCCESS";
export type FormBaseProps<T> = {
footer?: JSX.Element;
formFields: FormFieldThemeProps[];
showLabels: boolean;
buttonLabel: string;
validateOnBlur?: boolean;
clearError: () => void;
onError: (error: string) => void;
onSuccess?: (result: T & { status: "OK" }) => void;
callAPI: (fields: APIFormField[], setValue: (id: string, value: string) => void) => Promise<FormBaseAPIResponse<T>>;
};
export type FormBaseAPIResponse<T> =
| ({
/*
* Success.
*/
status: "OK";
} & T)
| {
/*
* Field validation errors.
*/
status: "FIELD_ERROR";
/*
* Array of Field Id and their corresponding error.
*/
formFields: FormFieldError[];
};
/*
* Add documentMode to document object in order to use to detect if browser is IE.
*/
declare global {
interface Document {
documentMode?: any;
}
}
export type SignInAndUpState = {
user: User | undefined;
error: string | undefined;
isSignUp: boolean;
};
export type EmailPasswordSignInAndUpAction =
| {
type: "setError";
error: string | undefined;
}
| {
type: "setSignUp";
}
| {
type: "setSignIn";
};
export type EmailPasswordSignInAndUpChildProps = Omit<SignInAndUpThemeProps, "featureState" | "dispatch">;