diff --git a/src/libs/routes/user.js b/src/libs/routes/user.js index 94e525d8aa9444408f2ee7e8e01e69e4934b9a91..274e68bb8dd1ae1ae90055d3291df3776ad8fff0 100644 --- a/src/libs/routes/user.js +++ b/src/libs/routes/user.js @@ -14,32 +14,38 @@ const jwt = require('jwt-simple'); function emailSyntax(email) { - regex = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; - return regex.test(email); + const regex = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; + return regex.test(email); } -userApp.post('/', (req, res) => { +userApp.post('/', (req, res, next) => { if (!req.body.email || !req.body.password) { res.json({success: false, msg: 'Please pass email and password.'}); + } else { + next(); } - else { - if(!emailSyntax(req.body.email)){ - res.json({success: false, msg: 'Invalid email syntax.'}); - } - var newUser = new User({ - email: req.body.email, - password: req.body.password - });const jwt = require('jwt-simple'); - - // save the user - newUser.save((err) => { - if (err) { - return res.json({success: false, msg: 'Email already in use.'}); - } - res.json({success: true, msg: 'Successful created new user.'}); - }); +}, (req, res, next) => { + if(!emailSyntax(req.body.email)){ + res.json({success: false, msg: 'Invalid email syntax.'}); + } else { + next(); } + +}, (req, res, next) => { + var newUser = new User({ + email: req.body.email, + password: req.body.password + }); + + // save the user + newUser.save((err) => { + if (err) { + res.json({success: false, msg: 'Email already in use.'}); + } else { + res.json({success: true, msg: 'Successful created new user.'}); + } + }); }); userApp.post('/authenticate', (req, res) => { @@ -63,7 +69,7 @@ userApp.post('/authenticate', (req, res) => { res.json({success: true, token: 'JWT ' + token}); } else { - res.send({success: false, msg: 'Authentication failed. Wrong password'}); + res.json({success: false, msg: 'Authentication failed. Wrong password'}); } }) }