-
Notifications
You must be signed in to change notification settings - Fork 81
/
types.ts
334 lines (288 loc) · 11.2 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
/* 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 { LinkClickedScreen } from "./components/themes/linkClickedScreen";
import type { EmailForm } from "./components/themes/signInUp/emailForm";
import type { EmailOrPhoneForm } from "./components/themes/signInUp/emailOrPhoneForm";
import type { LinkSent } from "./components/themes/signInUp/linkSent";
import type { PhoneForm } from "./components/themes/signInUp/phoneForm";
import type { SignInUpFooter } from "./components/themes/signInUp/signInUpFooter";
import type { SignInUpHeader } from "./components/themes/signInUp/signInUpHeader";
import type { UserInputCodeForm } from "./components/themes/signInUp/userInputCodeForm";
import type { UserInputCodeFormFooter } from "./components/themes/signInUp/userInputCodeFormFooter";
import type { UserInputCodeFormHeader } from "./components/themes/signInUp/userInputCodeFormHeader";
import type { ComponentOverride } from "../../components/componentOverride/componentOverride";
import type { FeatureBaseConfig, NormalisedBaseConfig, UserContext, WebJSRecipeInterface } from "../../types";
import type {
GetRedirectionURLContext as AuthRecipeModuleGetRedirectionURLContext,
OnHandleEventContext as AuthRecipeModuleOnHandleEventContext,
Config as AuthRecipeModuleConfig,
NormalisedConfig as NormalisedAuthRecipeModuleConfig,
UserInput as AuthRecipeModuleUserInput,
} from "../authRecipe/types";
import type { Dispatch } from "react";
import type WebJSRecipe from "supertokens-web-js/recipe/passwordless";
import type { RecipeInterface } from "supertokens-web-js/recipe/passwordless";
import type { User } from "supertokens-web-js/types";
export type PreAndPostAPIHookAction =
| "PASSWORDLESS_CREATE_CODE"
| "PASSWORDLESS_CONSUME_CODE"
| "PASSWORDLESS_RESEND_CODE"
| "EMAIL_EXISTS"
| "PHONE_NUMBER_EXISTS";
export type PreAPIHookContext = {
/*
* Pre API Hook action.
*/
action: PreAndPostAPIHookAction;
/*
* Request object containing query params, body, headers.
*/
requestInit: RequestInit;
/*
* URL
*/
url: string;
};
export type GetRedirectionURLContext = AuthRecipeModuleGetRedirectionURLContext;
export type OnHandleEventContext =
| {
action: "SUCCESS";
isNewRecipeUser: boolean;
user: User;
}
| {
action: "PASSWORDLESS_RESTART_FLOW";
}
| {
action: "PASSWORDLESS_CODE_SENT";
isResend: boolean;
}
| AuthRecipeModuleOnHandleEventContext;
export type PasswordlessNormalisedBaseConfig = {
disableDefaultUI?: boolean;
} & NormalisedBaseConfig;
export type NormalisedConfig = {
validateEmailAddress: (email: string) => Promise<string | undefined> | string | undefined;
validatePhoneNumber: (phoneNumber: string) => Promise<string | undefined> | string | undefined;
signInUpFeature: {
resendEmailOrSMSGapInSeconds: number;
defaultCountry?: string;
guessInternationPhoneNumberFromInputPhoneNumber: (
inputPhoneNumber: string,
defaultCountryFromConfig?: string
) => Promise<string | undefined> | string | undefined;
privacyPolicyLink?: string;
termsOfServiceLink?: string;
emailOrPhoneFormStyle: string;
userInputCodeFormStyle: string;
linkSentScreenStyle: string;
disableDefaultUI?: boolean;
};
linkClickedScreenFeature: PasswordlessNormalisedBaseConfig;
contactMethod: "PHONE" | "EMAIL" | "EMAIL_OR_PHONE";
override: {
functions: (originalImplementation: RecipeInterface) => RecipeInterface;
};
} & NormalisedAuthRecipeModuleConfig<GetRedirectionURLContext, PreAndPostAPIHookAction, OnHandleEventContext>;
export type Config = UserInput &
AuthRecipeModuleConfig<GetRedirectionURLContext, PreAndPostAPIHookAction, OnHandleEventContext>;
export type PasswordlessFeatureBaseConfig = {
disableDefaultUI?: boolean;
} & FeatureBaseConfig;
export type SignInUpFeatureConfigInput = {
disableDefaultUI?: boolean;
resendEmailOrSMSGapInSeconds?: number;
/*
* Privacy policy link for the sign-up form.
*/
privacyPolicyLink?: string;
/*
* Terms and conditions link for the sign-up form.
*/
termsOfServiceLink?: string;
emailOrPhoneFormStyle?: string;
userInputCodeFormStyle?: string;
linkSentScreenStyle?: string;
};
export type UserInput = (
| {
contactMethod: "EMAIL";
validateEmailAddress?: (email: string) => Promise<string | undefined> | string | undefined;
signInUpFeature?: SignInUpFeatureConfigInput;
}
| {
contactMethod: "PHONE";
validatePhoneNumber?: (phoneNumber: string) => Promise<string | undefined> | string | undefined;
signInUpFeature?: SignInUpFeatureConfigInput & {
/*
* Must be a two-letter ISO country code (e.g.: "US")
*/
defaultCountry?: string;
};
}
| {
contactMethod: "EMAIL_OR_PHONE";
validateEmailAddress?: (email: string) => Promise<string | undefined> | string | undefined;
validatePhoneNumber?: (phoneNumber: string) => Promise<string | undefined> | string | undefined;
signInUpFeature?: SignInUpFeatureConfigInput & {
/*
* Must be a two-letter ISO country code (e.g.: "US")
*/
defaultCountry?: string;
guessInternationPhoneNumberFromInputPhoneNumber?: (
inputPhoneNumber: string,
defaultCountryFromConfig?: string
) => Promise<string | undefined> | string | undefined;
};
}
) & {
override?: {
functions?: (originalImplementation: RecipeInterface) => RecipeInterface;
};
linkClickedScreenFeature?: PasswordlessFeatureBaseConfig;
} & AuthRecipeModuleUserInput<GetRedirectionURLContext, PreAndPostAPIHookAction, OnHandleEventContext>;
export type SignInUpProps = {
recipeImplementation: RecipeImplementation;
config: NormalisedConfig;
onSuccess?: (result: { createdNewRecipeUser: boolean; user: User }) => void;
dispatch: Dispatch<PasswordlessSignInUpAction>;
featureState: {
loginAttemptInfo?: LoginAttemptInfo;
loaded: boolean;
error: string | undefined;
};
userContext?: UserContext;
};
export type LoginAttemptInfo = {
deviceId: string;
preAuthSessionId: string;
contactInfo: string;
contactMethod: "EMAIL" | "PHONE";
lastResend: number;
redirectToPath?: string;
flowType: "USER_INPUT_CODE" | "MAGIC_LINK" | "USER_INPUT_CODE_AND_MAGIC_LINK";
};
/**
* When calling getLoginAttemptInfo/setLoginAttemptInfo from web-js we use generics to get
* access to properties in local storage that web-js does not set by default.
* This allows us to strongly type the response while keeping it dynamic.
*
* In the context of auth-react this type indicates all the additional properties we need.
*/
export type AdditionalLoginAttemptInfoProperties = {
contactInfo: string;
contactMethod: "EMAIL" | "PHONE";
lastResend: number;
redirectToPath?: string;
};
export type RecipeImplementation = WebJSRecipeInterface<typeof WebJSRecipe>;
export type SignInUpEmailFormProps = {
clearError: () => void;
onError: (error: string) => void;
error: string | undefined;
recipeImplementation: RecipeImplementation;
config: NormalisedConfig;
onSuccess?: () => void;
};
export type SignInUpPhoneFormProps = {
clearError: () => void;
onError: (error: string) => void;
error: string | undefined;
recipeImplementation: RecipeImplementation;
config: NormalisedConfig;
onSuccess?: () => void;
};
export type SignInUpEmailOrPhoneFormProps = {
clearError: () => void;
onError: (error: string) => void;
error: string | undefined;
recipeImplementation: RecipeImplementation;
config: NormalisedConfig;
onSuccess?: () => void;
};
export type SignInUpUserInputCodeFormProps = {
clearError: () => void;
onError: (error: string) => void;
error: string | undefined;
recipeImplementation: RecipeImplementation;
config: NormalisedConfig;
loginAttemptInfo: LoginAttemptInfo;
onSuccess?: (result: { createdNewRecipeUser: boolean; user: User }) => void;
};
export type LinkClickedScreenProps = {
recipeImplementation: RecipeImplementation;
config: NormalisedConfig;
requireUserInteraction: boolean;
consumeCode: () => void;
onSuccess?: () => void;
};
export type PasswordlessSignInUpAction =
| {
type: "load";
loginAttemptInfo: LoginAttemptInfo | undefined;
error: string | undefined;
}
| {
type: "startLogin";
loginAttemptInfo: LoginAttemptInfo;
}
| {
type: "resendCode";
timestamp: number;
}
| {
type: "restartFlow";
error: string | undefined;
}
| {
type: "setError";
error: string | undefined;
};
export type SignInUpState = {
error: string | undefined;
loaded: boolean;
loginAttemptInfo: LoginAttemptInfo | undefined;
};
export type SignInUpChildProps = Omit<SignInUpProps, "featureState" | "dispatch">;
export type LinkSentThemeProps = {
clearError: () => void;
onError: (error: string) => void;
error: string | undefined;
loginAttemptInfo: LoginAttemptInfo;
recipeImplementation: RecipeImplementation;
config: NormalisedConfig;
};
export type UserInputCodeFormFooterProps = {
loginAttemptInfo: LoginAttemptInfo;
recipeImplementation: RecipeImplementation;
config: NormalisedConfig;
};
export type UserInputCodeFormHeaderProps = {
loginAttemptInfo: LoginAttemptInfo;
recipeImplementation: RecipeImplementation;
config: NormalisedConfig;
};
export type ComponentOverrideMap = {
PasswordlessSignInUpHeader_Override?: ComponentOverride<typeof SignInUpHeader>;
PasswordlessSignInUpFooter_Override?: ComponentOverride<typeof SignInUpFooter>;
PasswordlessEmailForm_Override?: ComponentOverride<typeof EmailForm>;
PasswordlessPhoneForm_Override?: ComponentOverride<typeof PhoneForm>;
PasswordlessEmailOrPhoneForm_Override?: ComponentOverride<typeof EmailOrPhoneForm>;
PasswordlessUserInputCodeFormHeader_Override?: ComponentOverride<typeof UserInputCodeFormHeader>;
PasswordlessUserInputCodeFormFooter_Override?: ComponentOverride<typeof UserInputCodeFormFooter>;
PasswordlessUserInputCodeForm_Override?: ComponentOverride<typeof UserInputCodeForm>;
PasswordlessLinkSent_Override?: ComponentOverride<typeof LinkSent>;
PasswordlessLinkClickedScreen_Override?: ComponentOverride<typeof LinkClickedScreen>;
};