Skip to content
Snippets Groups Projects
Commit c0e9561c authored by Lucas Gabriel Lima's avatar Lucas Gabriel Lima
Browse files

refactor user route

parent 1a5d1440
No related branches found
No related tags found
1 merge request!20Auth
Pipeline #
...@@ -14,32 +14,38 @@ const jwt = require('jwt-simple'); ...@@ -14,32 +14,38 @@ const jwt = require('jwt-simple');
function emailSyntax(email) { function emailSyntax(email) {
regex = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; const regex = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
return regex.test(email); return regex.test(email);
} }
userApp.post('/', (req, res) => { userApp.post('/', (req, res, next) => {
if (!req.body.email || !req.body.password) { if (!req.body.email || !req.body.password) {
res.json({success: false, msg: 'Please pass email and 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({ }, (req, res, next) => {
email: req.body.email, if(!emailSyntax(req.body.email)){
password: req.body.password res.json({success: false, msg: 'Invalid email syntax.'});
});const jwt = require('jwt-simple'); } else {
next();
// 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) => {
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) => { userApp.post('/authenticate', (req, res) => {
...@@ -63,7 +69,7 @@ userApp.post('/authenticate', (req, res) => { ...@@ -63,7 +69,7 @@ userApp.post('/authenticate', (req, res) => {
res.json({success: true, token: 'JWT ' + token}); res.json({success: true, token: 'JWT ' + token});
} }
else { else {
res.send({success: false, msg: 'Authentication failed. Wrong password'}); res.json({success: false, msg: 'Authentication failed. Wrong password'});
} }
}) })
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment