From 17fb106aa89cab3f9995913549f707c696e92ade Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 1 Oct 2020 17:30:02 +0300 Subject: [PATCH] Password rules: move to a helper function. --- src/SignupPage.tsx | 11 ++++++----- src/helpers.tsx | 10 ++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/SignupPage.tsx b/src/SignupPage.tsx index b538631..4961352 100644 --- a/src/SignupPage.tsx +++ b/src/SignupPage.tsx @@ -20,7 +20,7 @@ import { CircularProgress } from "@material-ui/core"; import { Redirect } from "react-router"; import { useCredentials } from "./credentials"; import { useDispatch } from "react-redux"; -import { startTask } from "./helpers"; +import { startTask, PASSWORD_MIN_LENGTH, enforcePasswordRules } from "./helpers"; import { login } from "./store/actions"; import { Link } from "react-router-dom"; @@ -34,8 +34,6 @@ interface FormErrors { errorGeneral?: string; } -const PASSWORD_MIN_LENGTH = 8; - export default function SignupPage() { const credentials = useCredentials(); const dispatch = useDispatch(); @@ -67,8 +65,11 @@ export default function SignupPage() { } if (!password) { errors.errorPassword = fieldRequired; - } else if (password.length < PASSWORD_MIN_LENGTH) { - errors.errorPassword = `Passwourds should be at least ${PASSWORD_MIN_LENGTH} digits long.`; + } else { + const passwordRulesError = enforcePasswordRules(password); + if (passwordRulesError) { + errors.errorPassword = passwordRulesError; + } } if (process.env.NODE_ENV !== "development") { diff --git a/src/helpers.tsx b/src/helpers.tsx index 373b25b..d77d789 100644 --- a/src/helpers.tsx +++ b/src/helpers.tsx @@ -201,3 +201,13 @@ export function parseDate(prop: ICAL.Property) { return {}; } + +export const PASSWORD_MIN_LENGTH = 8; + +export function enforcePasswordRules(password: string): string | undefined { + + if (password.length < PASSWORD_MIN_LENGTH) { + return `Passwourds should be at least ${PASSWORD_MIN_LENGTH} digits long.`; + } + return undefined; +}