Newer
Older
const config = require(`${libs}/config`);
const log = require(`${libs}/log`)(module);
const User = require(`${libs}/models/user`);
const jwt = require('jwt-simple');
const required_fields = ["email", "password", "name", "cpf", "schooling", "segment", "role", "institution_name", "state", "city"];
function emailSyntax(email) {
const regex = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
return regex.test(email);
if (!req.body.email) {
res.json({success: false, msg: 'O campo Email é obrigatório.'});
} else {
next();
}
}, (req, res, next) => {
if (!req.body.password) {
res.json({success: false, msg: 'O campo Senha é obrigatório.'});
}, (req, res, next) => {
if(!emailSyntax(req.body.email)){
res.json({success: false, msg: 'O email informado é inválido.'});
}, (req, res, next) => {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
if (!req.body.name) {
res.json({success: false, msg: 'O campo Nome é obrigatório.'});
} else {
next();
}
}, (req, res, next) => {
if (!req.body.cpf) {
res.json({success: false, msg: 'O campo CPF é obrigatório.'});
} else {
next();
}
}, (req, res, next) => {
if (!req.body.schooling) {
res.json({success: false, msg: 'O campo Escolaridade é obrigatório.'});
} else {
next();
}
}, (req, res, next) => {
if (!req.body.segment) {
res.json({success: false, msg: 'O campo Segmento é obrigatório.'});
} else {
next();
}
}, (req, res, next) => {
if (!req.body.role) {
res.json({success: false, msg: 'O campo Função é obrigatório.'});
} else {
next();
}
}, (req, res, next) => {
if (!req.body.institution_name) {
res.json({success: false, msg: 'O campo Instituição em que trabalha é obrigatório.'});
}, (req, res, next) => {
if (!req.body.city) {
res.json({success: false, msg: 'O campo Cidade é obrigatório.'});
} else {
next();
}
}, (req, res, next) => {
if (!req.body.state) {
res.json({success: false, msg: 'O campo Estado é obrigatório.'});
} else {
next();
}
}, (req, res, next) => {
User.count({'email': req.body.email}, function(err, count){
if (err){

Lucas Gabriel Lima
committed
log.error('MongoDB error: ' + err);
res.json({success: false, msg: 'Um erro ocorreu no banco de dados.'});
}
if(count){
res.json({success: false, msg: 'O email informado já está cadastrado.'});
} else {
next();
}
});
}, (req, res, next) => {
User.count({'cpf': req.body.cpf}, function(err, count){
if (err){

Lucas Gabriel Lima
committed
log.error('MongoDB error: ' + err);
res.json({success: false, msg: 'Um erro ocorreu no banco de dados.'});
}
if(count){
res.json({success: false, msg: 'O CPF informado já está cadastrado.'});
} else {
next();
}
});
}, (req, res, next) => {
var newUser = new User({
email: req.body.email,
password: req.body.password,
name: req.body.name,
cpf: req.body.cpf,
schooling: req.body.schooling,
course: req.body.course,
segment: req.body.segment,
role: req.body.role,
institution_name: req.body.institution_name,
state: req.body.state,
city: req.body.city,
receive_emails: req.body.receive_emails
});
// save the user
newUser.save((err) => {
if (err) {
res.json({success: false, msg: 'Um erro ocorreu no banco de dados.'});
res.json({success: true, msg: 'Usuário cadastrado com sucesso!'});
userApp.post('/authenticate', (req, res, next) => {
if (!req.body.email) {
res.json({success: false, msg: 'O campo Email é obrigatório.'});
} else {
next();
}
}, (req, res, next) => {
if (!req.body.password) {
res.json({success: false, msg: 'O campo Senha é obrigatório.'});
} else {
next();
}
}, (req, res, next) => {
User.findOne({
email: req.body.email
}, (err, user) => {
if (err) throw err;
if(!user){
res.json({success: false, msg: 'O Email informado não está cadastrado.'});
}
else {
user.comparePassword(req.body.password, (err, isMatch) => {
if (isMatch && !err) {
var secret = config.mongodb.secret;
// if user is found and password is right create a token
var token = jwt.encode(user, secret);
//returns user info including token as json
res.json({success: true, token: 'JWT ' + token, msg: 'Usuário autenticado com sucesso'});
}
else {
res.json({success: false, msg: 'A Senha informada é inválida.'});