From 1e2fd4ffdb9988dcaa1936e49bd1c82cac80471e Mon Sep 17 00:00:00 2001 From: pilinux Date: Fri, 1 Sep 2023 17:24:26 +0200 Subject: [PATCH] refactoring --- app/routes/login/index.jsx | 18 ++++-------------- app/validator.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 14 deletions(-) create mode 100644 app/validator.js diff --git a/app/routes/login/index.jsx b/app/routes/login/index.jsx index 0ef23bc..8d70ce5 100644 --- a/app/routes/login/index.jsx +++ b/app/routes/login/index.jsx @@ -4,21 +4,11 @@ import { Form, Link } from "@remix-run/react"; import { useState } from "react"; import { remoteApi } from "~/api.server"; -import { sha512, sha3_512 } from "~/crypto"; -import { jwtExpiry } from "~/jwt"; import { cookieHandler } from "~/cookies.server"; -function validateEmail(email) { - const regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; - return regex.test(email); -} - -function validatePassword(password) { - // ,;.:-_#'+*@<>!"§$|%&/()[]=?{}\ - const regex = - /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[,;.:\-\_#\'+*@<>!"§$|%&/\(\)\[\]=?\{\}\\]).{6,}$/; - return regex.test(password); -} +import { sha512, sha3_512 } from "~/crypto"; +import { jwtExpiry } from "~/jwt"; +import { validateEmail, validatePassword } from "~/validator"; // https://remix.run/docs/en/main/guides/resource-routes // on server-side @@ -174,7 +164,7 @@ export default function Login() { if (res.status === 200) { // authentication successful, perform client-side redirect - window.location.href = "/logout"; + window.location.href = "/protected"; } if (res.status !== 200 && res.status < 500) { setAuthError("Invalid email or password"); diff --git a/app/validator.js b/app/validator.js new file mode 100644 index 0000000..01295ee --- /dev/null +++ b/app/validator.js @@ -0,0 +1,11 @@ +export function validateEmail(email) { + const regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; + return regex.test(email); +} + +export function validatePassword(password) { + // ,;.:-_#'+*@<>!"§$|%&/()[]=?{}\ + const regex = + /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[,;.:\-\_#\'+*@<>!"§$|%&/\(\)\[\]=?\{\}\\]).{6,}$/; + return regex.test(password); +}