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');
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'});
}
})
}
......
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