Commit b7a5e04a authored by Arthur.Pautrel's avatar Arthur.Pautrel
Browse files

Fixes before starting TP10

parent 32f64388
......@@ -2,12 +2,22 @@ const jwt = require("jsonwebtoken");
const config = process.env;
const verifyToken = (req, res, next) => {
const ensureSignedOut = (req, res, next) => {
const token =
req.body.token || req.query.token || req.headers["x-access-token"];
if (token) {
return res.send("You are already logged in");
}
return next();
};
const ensureSignedIn = (req, res, next) => {
const token =
req.body.token || req.query.token || req.headers["x-access-token"];
if (!token) {
return res.send("A token is required for authentication");
return res.send("You are no longer signed in");
}
try {
const decoded = jwt.verify(token, config.TOKEN_KEY);
......@@ -18,4 +28,7 @@ const verifyToken = (req, res, next) => {
return next();
};
module.exports = verifyToken;
\ No newline at end of file
module.exports = {
ensureSignedIn,
ensureSignedOut
}
\ No newline at end of file
......@@ -6,12 +6,13 @@ const { register } = require("../services/register");
const auth = require("../middlewares/auth");
var router = express.Router();
router.post("/login", auth, joiValidation(loginSchema), (req, res) => {
router.post("/login", auth.ensureSignedOut, joiValidation(loginSchema), async (req, res) => {
const {email, password} = req.body;
const result = login(email, password, req, res);
const result = await login(email, password);
res.json(result);
})
router.post("/register", auth, joiValidation(registerSchema), async (req, res) => {
router.post("/register", auth.ensureSignedOut, 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);
......
......@@ -2,7 +2,7 @@ const Joi = require('joi');
module.exports = Joi.object({
email: Joi.string()
.email({minDomainSegments: 2, tlds: {allow: ['con', 'net']}}),
.email({minDomainSegments: 2, tlds: {allow: ['com', 'net', 'fr']}}),
password: Joi.string()
.pattern(new RegExp('^[a-zA-Z0-9]{3,30}$'))
})
\ No newline at end of file
......@@ -2,7 +2,7 @@ const Joi = require('joi');
module.exports = Joi.object({
email: Joi.string()
.email({minDomainSegments: 2, tlds: {allow: ['con', 'net']}}),
.email({minDomainSegments: 2, tlds: {allow: ['com', 'net', 'fr']}}),
username: Joi.string()
.alphanum()
.min(3)
......@@ -11,6 +11,7 @@ module.exports = Joi.object({
firstName: Joi.string().required(),
lastName: Joi.string().required(),
password: Joi.string()
.pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),
.pattern(new RegExp('^[a-zA-Z0-9]{3,30}$'))
.required(),
repeatPassword: Joi.ref('password')
}).with('password', 'repeatPassword');
\ No newline at end of file
const Users = require("../models/user");
var bcrypt = require('bcryptjs');
var jwt = require('jsonwebtoken');
const login = (email, password, req, res) => {
Users.findOne({'email': email}, function(err, result) {
if (err) throw (err);
if (result != "") {
if (result.email == email) {
if (bcrypt.compareSync(password, result.password)) {
const token = jwt.sign(
{ email: email },
process.env.TOKEN_KEY,
{
expiresIn: "2h",
}
);
data.token = token;
req.session.email = email;
return res.json({ success: true, data: result});
} else {
return res.json({ success: false, error: 'Password do not match'});
}
}
const login = async (email, password) => {
try {
const user = await Users.findOne({'email': email});
if (!user) {
return { success: false, error: 'Email not registered'};
} else {
if (bcrypt.compareSync(password, user.password)) {
return { success: true, data: user};
} else {
return { success: false, error: 'Password do not match'};
}
}
return res.json({ success: false, error: 'Email not registered'});
});
} catch (err) {
return {success: false, error: err || "error"};
}
}
module.exports = {
......
const Users = require("../models/user");
const register = async (email, username, firstName, lastName, password, repeatPassword) =>{
const register = async (email, username, firstName, lastName, pass, repeatPassword) =>{
if (password == repeatPassword) {
if (pass == repeatPassword) {
try {
var bcrypt = require('bcryptjs');
var salt = bcrypt.genSaltSync(10);
var hashPass = bcrypt.hashSync("B4c0/\/", salt);
var password = bcrypt.hashSync(pass, salt);
const newUser = {
email,
username,
firstName,
lastName,
hashPass
password
}
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