From 39c9e5ada37509ad66e066653e716b0cec2abbda Mon Sep 17 00:00:00 2001 From: amarell Date: Tue, 19 Apr 2022 22:06:42 +0200 Subject: [PATCH 1/4] Code refactoring --- config.js | 2 +- controllers/userController.js | 5 +++++ package.json | 1 - routes/auth.js | 10 ++++++++++ server.js | 18 ++++++++++-------- 5 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 controllers/userController.js create mode 100644 routes/auth.js diff --git a/config.js b/config.js index 4d47a5b..2e534dd 100644 --- a/config.js +++ b/config.js @@ -4,4 +4,4 @@ const forExport = { DB_OPTIONS, }; -export default forExport; +module.exports = forExport; diff --git a/controllers/userController.js b/controllers/userController.js new file mode 100644 index 0000000..bf5016e --- /dev/null +++ b/controllers/userController.js @@ -0,0 +1,5 @@ +const e = require("express"); +const User = require("../models/userModel"); +const { registerValidation } = require("./../../validation"); + +// module.exports = (req, res) => {}; diff --git a/package.json b/package.json index 1baf1e2..be9f2a0 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,6 @@ "version": "1.0.0", "description": "Online tech shop backend", "main": "index.js", - "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/routes/auth.js b/routes/auth.js new file mode 100644 index 0000000..2198dec --- /dev/null +++ b/routes/auth.js @@ -0,0 +1,10 @@ +const router = require("express").Router(); + +router.get("/", (req, res) => { + res.json({ + message: "API is working", + version: "1.0.0", + }); +}); + +module.exports = router; diff --git a/server.js b/server.js index 3cba2ae..eb82dfc 100644 --- a/server.js +++ b/server.js @@ -1,11 +1,12 @@ -import express, { urlencoded, json } from "express"; -import mongoose from "mongoose"; +let express = require("express"); +let mongoose = require("mongoose"); let port = process.env.PORT || 3000; -import config from "./config.js"; -import cors from "cors"; -import dotenv from "dotenv"; +let config = require("./config.js"); +let cors = require("cors"); +require("dotenv").config(); -dotenv.config(); +// Import routes here +let authRoutes = require("./routes/auth"); const app = express(); @@ -16,15 +17,16 @@ app.listen(port, () => { }); app.use( - urlencoded({ + express.urlencoded({ extended: true, }) ); -app.use(json()); +app.use(express.json()); // Here we will add all the routes! // app.use("/api", userRoute) +app.use("/", authRoutes); const mongo = mongoose.connect(process.env.DB_PATH, config.DB_OPTIONS); From c7cfcb8fe69ae4540a3c3f9249695a20224c85dc Mon Sep 17 00:00:00 2001 From: amarell Date: Tue, 19 Apr 2022 22:15:52 +0200 Subject: [PATCH 2/4] Added register route --- controllers/userController.js | 34 ++++++++++++++++++++++++++++++++-- routes/auth.js | 3 +++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/controllers/userController.js b/controllers/userController.js index bf5016e..c72cef4 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -1,5 +1,35 @@ const e = require("express"); const User = require("../models/userModel"); -const { registerValidation } = require("./../../validation"); -// module.exports = (req, res) => {}; +// todo add validation +// const { registerValidation } = require(""); + +module.exports.register = async (req, res) => { + const emailExists = await User.findOne({ email: req.body.email }); + + if (emailExists) { + res.status(400).json({ + error: "This email is already in use.", + }); + } + + const user = new User({ + first_name: req.body.first_name, + last_name: req.body.last_name, + username: req.body.username, + email: req.body.email, + password: req.body.password, + }); + + user.save((err, savedUser) => { + if (err) { + res.status(400).json({ + status: "Something went wrong", + }); + } else { + res.status(200).json({ + data: savedUser, + }); + } + }); +}; diff --git a/routes/auth.js b/routes/auth.js index 2198dec..b1a5930 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -1,4 +1,5 @@ const router = require("express").Router(); +let userController = require("../controllers/userController"); router.get("/", (req, res) => { res.json({ @@ -7,4 +8,6 @@ router.get("/", (req, res) => { }); }); +router.route("/register").post(userController.register); + module.exports = router; From 1fd9eea1ecdde708a14f9d531ca372654da146f8 Mon Sep 17 00:00:00 2001 From: amarell Date: Tue, 19 Apr 2022 23:37:01 +0200 Subject: [PATCH 3/4] Added some basic validation for registration --- controllers/userController.js | 30 +++++++++++------------ entityFactory.js | 7 ++++++ validation.js | 46 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 entityFactory.js create mode 100644 validation.js diff --git a/controllers/userController.js b/controllers/userController.js index c72cef4..3c489bb 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -1,33 +1,33 @@ const e = require("express"); +const { entityFactory } = require("../entityFactory"); const User = require("../models/userModel"); - -// todo add validation -// const { registerValidation } = require(""); +const { registerValidation } = require("./../validation"); module.exports.register = async (req, res) => { - const emailExists = await User.findOne({ email: req.body.email }); + const errors = registerValidation(req.body); + if (errors.length > 0) { + return res.status(400).json({ + errors, + }); + } + const emailExists = await User.findOne({ email: req.body.email }); if (emailExists) { - res.status(400).json({ + return res.status(400).json({ error: "This email is already in use.", }); } - const user = new User({ - first_name: req.body.first_name, - last_name: req.body.last_name, - username: req.body.username, - email: req.body.email, - password: req.body.password, - }); + const user = new User(); + entityFactory(user, req.body); - user.save((err, savedUser) => { + await user.save((err, savedUser) => { if (err) { - res.status(400).json({ + return res.status(400).json({ status: "Something went wrong", }); } else { - res.status(200).json({ + return res.status(200).json({ data: savedUser, }); } diff --git a/entityFactory.js b/entityFactory.js new file mode 100644 index 0000000..c183134 --- /dev/null +++ b/entityFactory.js @@ -0,0 +1,7 @@ +const entityFactory = (entity, requestBody) => { + Object.keys(requestBody).forEach((key) => { + entity[key] = requestBody[key]; + }); +}; + +module.exports.entityFactory = entityFactory; diff --git a/validation.js b/validation.js new file mode 100644 index 0000000..1c609a2 --- /dev/null +++ b/validation.js @@ -0,0 +1,46 @@ +const registerValidation = (data) => { + const passwordRegex = + /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{8,56}$/; + + let errors = []; + let required_fields = [ + "username", + "password", + "first_name", + "last_name", + "email", + ]; + let missing_fields = []; + + required_fields.forEach((field) => { + if (!data[field]) { + missing_fields.push(field); + } + }); + + if (missing_fields.length > 0) { + let msg = "The field/s "; + missing_fields.forEach((missing_field) => { + msg = msg + missing_field + ", "; + }); + msg = msg.substring(0, msg.length - 2); + msg = msg + " is/are required. "; + + errors.push({ error: msg }); + } + + if (data.username && data.username.length <= 4) { + errors.push({ error: "The username has to be at least 4 chars long" }); + } + + if (!passwordRegex.test(data.password)) { + errors.push({ + error: + "The password must have at least 8 chars, including at least 1 special character and at least 1 digit", + }); + } + + return errors; +}; + +module.exports.registerValidation = registerValidation; From 8750a20ca87c17bcc79418c87dd222038e1c3236 Mon Sep 17 00:00:00 2001 From: amarell Date: Wed, 20 Apr 2022 23:38:51 +0200 Subject: [PATCH 4/4] Login route setup with validation --- controllers/userController.js | 34 ++++++- package-lock.json | 185 ++++++++++++++++++++++++++++++++++ package.json | 1 + routes/auth.js | 1 + utilities/jwt.js | 19 ++++ utilities/utils.js | 22 ++++ validation.js | 36 +++---- 7 files changed, 280 insertions(+), 18 deletions(-) create mode 100644 utilities/jwt.js create mode 100644 utilities/utils.js diff --git a/controllers/userController.js b/controllers/userController.js index 3c489bb..94106ae 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -1,7 +1,8 @@ const e = require("express"); const { entityFactory } = require("../entityFactory"); const User = require("../models/userModel"); -const { registerValidation } = require("./../validation"); +const { registerValidation, loginValidation } = require("./../validation"); +const jwt = require("./../utilities/jwt"); module.exports.register = async (req, res) => { const errors = registerValidation(req.body); @@ -33,3 +34,34 @@ module.exports.register = async (req, res) => { } }); }; + +module.exports.login = async (req, res) => { + const errors = loginValidation(req.body); + + if (errors.length > 0) { + return res.status(400).json({ + errors, + }); + } + + const user = await User.findOne({ email: req.body.email }); + + if (!user) { + return res.status(400).json({ + error: "The user with this email address does not exist", + }); + } + + if (user.password !== req.body.password) { + return res.status(400).json({ + error: "Email or password is incorrect", + }); + } + + let token = jwt.sign(user); + + return res.status(200).json({ + success: true, + token, + }); +}; diff --git a/package-lock.json b/package-lock.json index 9832f75..450a8b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "cors": "^2.8.5", "dotenv": "^16.0.0", "express": "^4.17.3", + "jsonwebtoken": "^8.5.1", "mongodb": "^4.5.0", "mongoose": "^6.3.0", "nodemon": "^2.0.15" @@ -246,6 +247,11 @@ "ieee754": "^1.1.13" } }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=" + }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -554,6 +560,14 @@ "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=" }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -987,6 +1001,51 @@ "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.0.tgz", "integrity": "sha1-Wx85evx11ne96Lz8Dkfh+aPZqJg=" }, + "node_modules/jsonwebtoken": { + "version": "8.5.1", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz", + "integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==", + "dependencies": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^5.6.0" + }, + "engines": { + "node": ">=4", + "npm": ">=1.4.28" + } + }, + "node_modules/jsonwebtoken/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "dependencies": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "dependencies": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "node_modules/kareem": { "version": "2.3.5", "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.5.tgz", @@ -1011,6 +1070,41 @@ "node": ">=8" } }, + "node_modules/lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8=" + }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY=" + }, + "node_modules/lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M=" + }, + "node_modules/lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w=" + }, + "node_modules/lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=" + }, + "node_modules/lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=" + }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=" + }, "node_modules/lowercase-keys": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz", @@ -2150,6 +2244,11 @@ "ieee754": "^1.1.13" } }, + "buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=" + }, "bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -2373,6 +2472,14 @@ "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", "integrity": "sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI=" }, + "ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "requires": { + "safe-buffer": "^5.0.1" + } + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -2686,6 +2793,49 @@ "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.0.tgz", "integrity": "sha1-Wx85evx11ne96Lz8Dkfh+aPZqJg=" }, + "jsonwebtoken": { + "version": "8.5.1", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz", + "integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==", + "requires": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^5.6.0" + }, + "dependencies": { + "ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + } + } + }, + "jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "requires": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "requires": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "kareem": { "version": "2.3.5", "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.5.tgz", @@ -2707,6 +2857,41 @@ "package-json": "^6.3.0" } }, + "lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8=" + }, + "lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY=" + }, + "lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M=" + }, + "lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w=" + }, + "lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=" + }, + "lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=" + }, + "lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=" + }, "lowercase-keys": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz", diff --git a/package.json b/package.json index be9f2a0..b6ff830 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "cors": "^2.8.5", "dotenv": "^16.0.0", "express": "^4.17.3", + "jsonwebtoken": "^8.5.1", "mongodb": "^4.5.0", "mongoose": "^6.3.0", "nodemon": "^2.0.15" diff --git a/routes/auth.js b/routes/auth.js index b1a5930..0eec096 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -9,5 +9,6 @@ router.get("/", (req, res) => { }); router.route("/register").post(userController.register); +router.route("/login").post(userController.login); module.exports = router; diff --git a/utilities/jwt.js b/utilities/jwt.js new file mode 100644 index 0000000..f0907f5 --- /dev/null +++ b/utilities/jwt.js @@ -0,0 +1,19 @@ +let jwt = require("jsonwebtoken"); + +class JWT { + static sign(user) { + return jwt.sign( + { + uid: user._id, + role: user.role, + }, + process.env.JWT_KEY + ); + } + + static verify(token) { + return jwt.verify(token, process.env.JWT_KEY); + } +} + +module.exports = JWT; diff --git a/utilities/utils.js b/utilities/utils.js new file mode 100644 index 0000000..fd24699 --- /dev/null +++ b/utilities/utils.js @@ -0,0 +1,22 @@ +const validateMissingFields = (required_fields, data) => { + let missing_fields = []; + + required_fields.forEach((field) => { + if (!data[field]) { + missing_fields.push(field); + } + }); + + if (missing_fields.length > 0) { + let msg = "The field/s "; + missing_fields.forEach((missing_field) => { + msg = msg + missing_field + ", "; + }); + msg = msg.substring(0, msg.length - 2); + msg = msg + " is/are required. "; + return msg; + } + return 0; +}; + +module.exports.validateMissingFields = validateMissingFields; diff --git a/validation.js b/validation.js index 1c609a2..19c975b 100644 --- a/validation.js +++ b/validation.js @@ -1,3 +1,5 @@ +const { validateMissingFields } = require("./utilities/utils"); + const registerValidation = (data) => { const passwordRegex = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{8,56}$/; @@ -10,30 +12,17 @@ const registerValidation = (data) => { "last_name", "email", ]; - let missing_fields = []; - - required_fields.forEach((field) => { - if (!data[field]) { - missing_fields.push(field); - } - }); - - if (missing_fields.length > 0) { - let msg = "The field/s "; - missing_fields.forEach((missing_field) => { - msg = msg + missing_field + ", "; - }); - msg = msg.substring(0, msg.length - 2); - msg = msg + " is/are required. "; + let missing_fields = validateMissingFields(required_fields, data); - errors.push({ error: msg }); + if (missing_fields) { + errors.push({ error: missing_fields }); } if (data.username && data.username.length <= 4) { errors.push({ error: "The username has to be at least 4 chars long" }); } - if (!passwordRegex.test(data.password)) { + if (data.password && !passwordRegex.test(data.password)) { errors.push({ error: "The password must have at least 8 chars, including at least 1 special character and at least 1 digit", @@ -43,4 +32,17 @@ const registerValidation = (data) => { return errors; }; +const loginValidation = (data) => { + let errors = []; + let required_fields = ["password", "email"]; + let missing_fields = validateMissingFields(required_fields, data); + + if (missing_fields) { + errors.push({ error: missing_fields }); + } + + return errors; +}; + module.exports.registerValidation = registerValidation; +module.exports.loginValidation = loginValidation;