Commit 8627e21d authored by Arthur.Pautrel's avatar Arthur.Pautrel
Browse files

ex02 finished

parent 8c0ad0cf
const joiValidation = (schema) => {
return async (req, res, next) => {
try {
const body = req.body;
await schema.validateAsync(body);
} catch (err) {
return res.json({ success: false, error: err})
}
next();
}
}
module.exports = joiValidation;
\ No newline at end of file
......@@ -8,9 +8,11 @@
"name": "ex02",
"version": "1.0.0",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.2",
"cors": "^2.8.5",
"express": "^4.17.2",
"joi": "^17.6.0",
"mongoose": "^6.2.7",
"vue": "^3.2.31",
"vue-router": "^4.0.12"
......@@ -27,6 +29,37 @@
"node": ">=6.0.0"
}
},
"node_modules/@hapi/hoek": {
"version": "9.2.1",
"resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.2.1.tgz",
"integrity": "sha512-gfta+H8aziZsm8pZa0vj04KO6biEiisppNgA1kbJvFrrWu9Vm7eaUEy76DIxsuTaWvti5fkJVhllWc6ZTE+Mdw=="
},
"node_modules/@hapi/topo": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz",
"integrity": "sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg==",
"dependencies": {
"@hapi/hoek": "^9.0.0"
}
},
"node_modules/@sideway/address": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/@sideway/address/-/address-4.1.3.tgz",
"integrity": "sha512-8ncEUtmnTsMmL7z1YPB47kPUq7LpKWJNFPsRzHiIajGC5uXlWGn+AmkYPcHNl8S4tcEGx+cnORnNYaw2wvL+LQ==",
"dependencies": {
"@hapi/hoek": "^9.0.0"
}
},
"node_modules/@sideway/formula": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@sideway/formula/-/formula-3.0.0.tgz",
"integrity": "sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg=="
},
"node_modules/@sideway/pinpoint": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@sideway/pinpoint/-/pinpoint-2.0.0.tgz",
"integrity": "sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ=="
},
"node_modules/@types/node": {
"version": "17.0.21",
"resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.21.tgz",
......@@ -189,6 +222,11 @@
}
]
},
"node_modules/bcryptjs": {
"version": "2.4.3",
"resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
"integrity": "sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms="
},
"node_modules/body-parser": {
"version": "1.19.2",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.2.tgz",
......@@ -496,6 +534,18 @@
"node": ">= 0.10"
}
},
"node_modules/joi": {
"version": "17.6.0",
"resolved": "https://registry.npmjs.org/joi/-/joi-17.6.0.tgz",
"integrity": "sha512-OX5dG6DTbcr/kbMFj0KGYxuew69HPcAE3K/sZpEV2nP6e/j/C0HV+HNiBPCASxdx5T7DMoa0s8UeHWMnb6n2zw==",
"dependencies": {
"@hapi/hoek": "^9.0.0",
"@hapi/topo": "^5.0.0",
"@sideway/address": "^4.1.3",
"@sideway/formula": "^3.0.0",
"@sideway/pinpoint": "^2.0.0"
}
},
"node_modules/kareem": {
"version": "2.3.4",
"resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.4.tgz",
......@@ -1045,6 +1095,37 @@
"resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.17.3.tgz",
"integrity": "sha512-7yJPvPV+ESz2IUTPbOL+YkIGyCqOyNIzdguKQuJGnH7bg1WTIifuM21YqokFt/THWh1AkCRn9IgoykTRCBVpzA=="
},
"@hapi/hoek": {
"version": "9.2.1",
"resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.2.1.tgz",
"integrity": "sha512-gfta+H8aziZsm8pZa0vj04KO6biEiisppNgA1kbJvFrrWu9Vm7eaUEy76DIxsuTaWvti5fkJVhllWc6ZTE+Mdw=="
},
"@hapi/topo": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz",
"integrity": "sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg==",
"requires": {
"@hapi/hoek": "^9.0.0"
}
},
"@sideway/address": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/@sideway/address/-/address-4.1.3.tgz",
"integrity": "sha512-8ncEUtmnTsMmL7z1YPB47kPUq7LpKWJNFPsRzHiIajGC5uXlWGn+AmkYPcHNl8S4tcEGx+cnORnNYaw2wvL+LQ==",
"requires": {
"@hapi/hoek": "^9.0.0"
}
},
"@sideway/formula": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@sideway/formula/-/formula-3.0.0.tgz",
"integrity": "sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg=="
},
"@sideway/pinpoint": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@sideway/pinpoint/-/pinpoint-2.0.0.tgz",
"integrity": "sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ=="
},
"@types/node": {
"version": "17.0.21",
"resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.21.tgz",
......@@ -1187,6 +1268,11 @@
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA=="
},
"bcryptjs": {
"version": "2.4.3",
"resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
"integrity": "sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms="
},
"body-parser": {
"version": "1.19.2",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.2.tgz",
......@@ -1412,6 +1498,18 @@
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
"integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g=="
},
"joi": {
"version": "17.6.0",
"resolved": "https://registry.npmjs.org/joi/-/joi-17.6.0.tgz",
"integrity": "sha512-OX5dG6DTbcr/kbMFj0KGYxuew69HPcAE3K/sZpEV2nP6e/j/C0HV+HNiBPCASxdx5T7DMoa0s8UeHWMnb6n2zw==",
"requires": {
"@hapi/hoek": "^9.0.0",
"@hapi/topo": "^5.0.0",
"@sideway/address": "^4.1.3",
"@sideway/formula": "^3.0.0",
"@sideway/pinpoint": "^2.0.0"
}
},
"kareem": {
"version": "2.3.4",
"resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.4.tgz",
......
......@@ -7,9 +7,11 @@
"start": "nodemon index.js"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.2",
"cors": "^2.8.5",
"express": "^4.17.2",
"joi": "^17.6.0",
"mongoose": "^6.2.7",
"vue": "^3.2.31",
"vue-router": "^4.0.12"
......
const express = require('express');
const joiValidation = require('../middlewares/joiValidation');
const { registerSchema, loginSchema } = require('../schemas');
const { login } = require("../services/login");
const { register } = require("../services/register");
var router = express.Router();
router.post("/login", (req, res) => {
router.post("/login", joiValidation(loginSchema), (req, res) => {
const {email, password} = req.body;
const result = login(email, password, res);
})
router.post("/register", async (req, res) => {
router.post("/register", joiValidation(registerSchema), async (req, res) => {
const {email, username, firstName, lastName, password, repeatPassword} = req.body;
const result = await register(email, username, firstName, lastName, password, repeatPassword);
res.json(result);
......
const registerSchema = require('./register');
const loginSchema = require('./login');
module.exports = {
registerSchema,
loginSchema
}
\ No newline at end of file
const Joi = require('joi');
module.exports = Joi.object({
email: Joi.string()
.email({minDomainSegments: 2, tlds: {allow: ['con', 'net']}}),
password: Joi.string()
.pattern(new RegExp('^[a-zA-Z0-9]{3,30}$'))
})
\ No newline at end of file
const Joi = require('joi');
module.exports = Joi.object({
email: Joi.string()
.email({minDomainSegments: 2, tlds: {allow: ['con', 'net']}}),
username: Joi.string()
.alphanum()
.min(3)
.max(30)
.required(),
firstName: Joi.string().required(),
lastName: Joi.string().required(),
password: Joi.string()
.pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),
repeatPassword: Joi.ref('password')
}).with('password', 'repeatPassword');
\ No newline at end of file
const Users = require("../models/user");
var bcrypt = require('bcryptjs');
const login = (email, password, res) => {
Users.findOne({'email': email}, function(err, result) {
if (err) throw (err);
if (result != "") {
if (result.email == email) {
if (result.password == password) {
if (bcrypt.compareSync(password, result.password)) {
return res.json({ success: true, data: result});
} else {
return res.json({ success: false, error: 'Password do not match'});
......
......@@ -4,12 +4,16 @@ const register = async (email, username, firstName, lastName, password, repeatPa
if (password == repeatPassword) {
try {
var bcrypt = require('bcryptjs');
var salt = bcrypt.genSaltSync(10);
var hashPass = bcrypt.hashSync("B4c0/\/", salt);
const newUser = {
email,
username,
firstName,
lastName,
password
hashPass
}
const createdUser = await Users.create(newUser);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment