Skip to content

Commit

Permalink
Allows for the form field values to be of any type. However, inforces…
Browse files Browse the repository at this point in the history
… that the password and email must be of type string
  • Loading branch information
rishabhpoddar committed Nov 4, 2020
1 parent 840c799 commit 20691af
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/build/recipe/emailpassword/api/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions lib/build/recipe/emailpassword/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export declare type TypeInputSignUp = {
disableDefaultImplementation?: boolean;
formFields?: {
id: string;
validate?: (value: string) => Promise<string | undefined>;
validate?: (value: any) => Promise<string | undefined>;
optional?: boolean;
}[];
handleCustomFormFields?: (user: User, formFields: {
id: string;
value: string;
value: any;
}[]) => Promise<void>;
};
export declare type TypeInputSignIn = {
Expand All @@ -30,12 +30,12 @@ export declare type TypeNormalisedInputSignUp = {
formFields: NormalisedFormField[];
handleCustomFormFields: (user: User, formFields: {
id: string;
value: string;
value: any;
}[]) => Promise<void>;
};
export declare type NormalisedFormField = {
id: string;
validate: (value: string) => Promise<string | undefined>;
validate: (value: any) => Promise<string | undefined>;
optional: boolean;
};
export declare type TypeNormalisedInputSignIn = {
Expand Down
4 changes: 2 additions & 2 deletions lib/build/recipe/emailpassword/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import Recipe from "./recipe";
import { TypeInput, TypeNormalisedInput } from "./types";
import { NormalisedAppinfo } from "../../types";
export declare function validateAndNormaliseUserInput(recipeInstance: Recipe, appInfo: NormalisedAppinfo, config?: TypeInput): TypeNormalisedInput;
export declare function defaultPasswordValidator(value: string): Promise<"Password must contain at least 8 characters, including a number" | "Password's length must be lesser than 100 characters" | "Password must contain at least one alphabet" | "Password must contain at least one number" | undefined>;
export declare function defaultEmailValidator(value: string): Promise<"Email is invalid" | undefined>;
export declare function defaultPasswordValidator(value: any): Promise<"Development bug: Please make sure the password field yields a string" | "Password must contain at least 8 characters, including a number" | "Password's length must be lesser than 100 characters" | "Password must contain at least one alphabet" | "Password must contain at least one number" | undefined>;
export declare function defaultEmailValidator(value: any): Promise<"Development bug: Please make sure the email field yields a string" | "Email is invalid" | undefined>;
6 changes: 6 additions & 0 deletions lib/build/recipe/emailpassword/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/ts/recipe/emailpassword/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function validateFormFieldsOrThrowError(
if (typeof curr !== "object" || curr === null) {
throw newBadRequestError(recipeInstance, "All elements of formFields must be an object");
}
if (typeof curr.id !== "string" || typeof curr.value !== "string") {
if (typeof curr.id !== "string" || curr.value === undefined) {
throw newBadRequestError(
recipeInstance,
"All elements of formFields must contain an 'id' and 'value' field"
Expand Down
8 changes: 4 additions & 4 deletions lib/ts/recipe/emailpassword/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export type TypeInputSignUp = {
disableDefaultImplementation?: boolean;
formFields?: {
id: string;
validate?: (value: string) => Promise<string | undefined>;
validate?: (value: any) => Promise<string | undefined>;
optional?: boolean;
}[];
handleCustomFormFields?: (user: User, formFields: { id: string; value: string }[]) => Promise<void>;
handleCustomFormFields?: (user: User, formFields: { id: string; value: any }[]) => Promise<void>;
};

export type TypeInputSignIn = {
Expand All @@ -44,12 +44,12 @@ export type TypeInputSignIn = {
export type TypeNormalisedInputSignUp = {
disableDefaultImplementation: boolean;
formFields: NormalisedFormField[];
handleCustomFormFields: (user: User, formFields: { id: string; value: string }[]) => Promise<void>;
handleCustomFormFields: (user: User, formFields: { id: string; value: any }[]) => Promise<void>;
};

export type NormalisedFormField = {
id: string;
validate: (value: string) => Promise<string | undefined>;
validate: (value: any) => Promise<string | undefined>;
optional: boolean;
};

Expand Down
16 changes: 12 additions & 4 deletions lib/ts/recipe/emailpassword/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,21 @@ function validateAndNormaliseSignupConfig(
};
}

async function defaultValidator(value: string): Promise<string | undefined> {
async function defaultValidator(value: any): Promise<string | undefined> {
return undefined;
}

async function defaultHandleCustomFormFields(user: User, formFields: { id: string; value: string }[]) {}
async function defaultHandleCustomFormFields(user: User, formFields: { id: string; value: any }[]) {}

export async function defaultPasswordValidator(value: string) {
export async function defaultPasswordValidator(value: any) {
// length >= 8 && < 100
// must have a number and a character
// as per https://github.com/supertokens/supertokens-auth-react/issues/5#issuecomment-709512438

if (typeof value !== "string") {
return "Development bug: Please make sure the password field yields a string";
}

if (value.length < 8) {
return "Password must contain at least 8 characters, including a number";
}
Expand All @@ -257,11 +261,15 @@ export async function defaultPasswordValidator(value: string) {
return undefined;
}

export async function defaultEmailValidator(value: string) {
export async function defaultEmailValidator(value: any) {
// We check if the email syntax is correct
// As per https://github.com/supertokens/supertokens-auth-react/issues/5#issuecomment-709512438
// Regex from https://stackoverflow.com/a/46181/3867175

if (typeof value !== "string") {
return "Development bug: Please make sure the email field yields a string";
}

if (
value.match(
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
Expand Down
1 change: 1 addition & 0 deletions test/emailpassword/signupFeature.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@
* - Test password field validation error
* - Test email field validation error
* - Make sure that the input email is trimmed
* - Pass a non string value in the formFields array and make sure it passes through the signUp API and is sent in the handleCustomFormFields as that type
*/

0 comments on commit 20691af

Please sign in to comment.