-
Notifications
You must be signed in to change notification settings - Fork 88
feat: Adds signInFeature type and validation for form fields in EmailPassword Recipe #976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
6367701
8722640
5bd1f30
24e6502
6addbe7
caa3b5d
c491dca
1d45f4d
44129ed
809d14e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,6 +23,7 @@ import { | |||||
TypeNormalisedInputResetPasswordUsingTokenFeature, | ||||||
NormalisedFormField, | ||||||
TypeInputFormField, | ||||||
TypeInputSignIn, | ||||||
} from "./types"; | ||||||
import { NormalisedAppinfo, UserContext } from "../../types"; | ||||||
import { FORM_FIELD_EMAIL_ID, FORM_FIELD_PASSWORD_ID } from "./constants"; | ||||||
|
@@ -41,7 +42,11 @@ export function validateAndNormaliseUserInput( | |||||
config === undefined ? undefined : config.signUpFeature | ||||||
); | ||||||
|
||||||
let signInFeature = validateAndNormaliseSignInConfig(recipeInstance, appInfo, signUpFeature); | ||||||
let signInFeature = validateAndNormaliseSignInConfig( | ||||||
recipeInstance, | ||||||
appInfo, | ||||||
config === undefined ? undefined : config.signInFeature | ||||||
); | ||||||
|
||||||
let resetPasswordUsingTokenFeature = validateAndNormaliseResetPasswordUsingTokenConfig(signUpFeature); | ||||||
|
||||||
|
@@ -114,25 +119,58 @@ function validateAndNormaliseResetPasswordUsingTokenConfig( | |||||
}; | ||||||
} | ||||||
|
||||||
function normaliseSignInFormFields(formFields: NormalisedFormField[]) { | ||||||
return formFields | ||||||
.filter((filter) => filter.id === FORM_FIELD_EMAIL_ID || filter.id === FORM_FIELD_PASSWORD_ID) | ||||||
.map((field) => { | ||||||
return { | ||||||
id: field.id, | ||||||
// see issue: https://github.com/supertokens/supertokens-node/issues/36 | ||||||
validate: field.id === FORM_FIELD_EMAIL_ID ? field.validate : defaultValidator, | ||||||
optional: false, | ||||||
}; | ||||||
function normaliseSignInFormFields(formFields?: TypeInputFormField[]) { | ||||||
let normalisedFormFields: NormalisedFormField[] = []; | ||||||
if (formFields !== undefined) { | ||||||
formFields.forEach((field) => { | ||||||
if (field.id === FORM_FIELD_PASSWORD_ID) { | ||||||
normalisedFormFields.push({ | ||||||
id: field.id, | ||||||
validate: field.validate === undefined ? defaultValidator : field.validate, | ||||||
optional: false, | ||||||
}); | ||||||
} else if (field.id === FORM_FIELD_EMAIL_ID) { | ||||||
normalisedFormFields.push({ | ||||||
id: field.id, | ||||||
validate: field.validate === undefined ? defaultEmailValidator : field.validate, | ||||||
optional: false, | ||||||
}); | ||||||
} else { | ||||||
normalisedFormFields.push({ | ||||||
id: field.id, | ||||||
validate: field.validate === undefined ? defaultValidator : field.validate, | ||||||
optional: field.optional === undefined ? false : field.optional, | ||||||
}); | ||||||
} | ||||||
}); | ||||||
} | ||||||
if (!normalisedFormFields.some((field) => field.id === FORM_FIELD_PASSWORD_ID)) { | ||||||
// no password field give by user | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment "no password field give by user" contains a grammatical error. It should be "no password field given by user" to maintain proper grammar. This same typo appears in another comment about email fields as well. Consider correcting both instances for consistency.
Suggested change
Spotted by Diamond |
||||||
normalisedFormFields.push({ | ||||||
id: FORM_FIELD_PASSWORD_ID, | ||||||
validate: defaultValidator, | ||||||
optional: false, | ||||||
}); | ||||||
} | ||||||
if (!normalisedFormFields.some((field) => field.id === FORM_FIELD_EMAIL_ID)) { | ||||||
// no email field give by user | ||||||
normalisedFormFields.push({ | ||||||
id: FORM_FIELD_EMAIL_ID, | ||||||
validate: defaultEmailValidator, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should fall back to the (normalized) email field validator for sign-up to avoid breaking apps. |
||||||
optional: false, | ||||||
}); | ||||||
} | ||||||
return normalisedFormFields; | ||||||
} | ||||||
|
||||||
function validateAndNormaliseSignInConfig( | ||||||
_: Recipe, | ||||||
__: NormalisedAppinfo, | ||||||
signUpConfig: TypeNormalisedInputSignUp | ||||||
config?: TypeInputSignIn | ||||||
): TypeNormalisedInputSignIn { | ||||||
let formFields: NormalisedFormField[] = normaliseSignInFormFields(signUpConfig.formFields); | ||||||
let formFields: NormalisedFormField[] = normaliseSignInFormFields( | ||||||
config === undefined ? undefined : config.formFields | ||||||
); | ||||||
|
||||||
return { | ||||||
formFields, | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,7 +12,8 @@ | |||||
* License for the specific language governing permissions and limitations | ||||||
* under the License. | ||||||
*/ | ||||||
export const version = "22.1.0"; | ||||||
export const version = "21.2.0"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The version number appears to be downgraded from
Suggested change
Spotted by Diamond |
||||||
|
||||||
|
||||||
export const cdiSupported = ["5.3"]; | ||||||
|
||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1887,4 +1887,71 @@ describe(`signinFeature: ${printPath("[test/emailpassword/signinFeature.test.js] | |
); | ||
assert(invalidEmailResponse.status === "WRONG_CREDENTIALS_ERROR"); | ||
}); | ||
|
||
// test case where more than the configured form fields are passed. | ||
it("test bad case when too many formFields are passed", async function () { | ||
const connectionURI = await startST(); | ||
STExpress.init({ | ||
supertokens: { | ||
connectionURI, | ||
}, | ||
appInfo: { | ||
apiDomain: "api.supertokens.io", | ||
appName: "SuperTokens", | ||
websiteDomain: "supertokens.io", | ||
}, | ||
recipeList: [ | ||
EmailPassword.init({ | ||
signInFeature: { | ||
formFields: [ | ||
{ | ||
id: "testField", | ||
}, | ||
], | ||
}, | ||
}), | ||
Session.init({ getTokenTransferMethod: () => "cookie" }), | ||
], | ||
}); | ||
const app = express(); | ||
|
||
app.use(middleware()); | ||
|
||
app.use(errorHandler()); | ||
|
||
let response = await new Promise((resolve) => | ||
request(app) | ||
.post("/auth/signin") | ||
.send({ | ||
formFields: [ | ||
{ | ||
id: "password", | ||
value: "validpass123", | ||
}, | ||
{ | ||
id: "email", | ||
value: "[email protected]", | ||
}, | ||
{ | ||
id: "testField", | ||
value: "some value", | ||
}, | ||
{ | ||
id: "extraField", | ||
value: "some value", | ||
}, | ||
], | ||
}) | ||
.expect(400) | ||
.end((err, res) => { | ||
if (err) { | ||
resolve(undefined); | ||
} else { | ||
resolve(JSON.parse(res.text)); | ||
} | ||
}) | ||
); | ||
|
||
assert(response.message == "Are you sending too many formFields?"); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If done this way, it'd be a breaking change: if someone added an override for their email validators in the sign up config, they'd expect it to be applied here. I'd prefer if we kept it that way as well.