-
Lucas Gabriel Lima authoredLucas Gabriel Lima authored
user.js 4.36 KiB
const express = require('express');
const userApp = express();
const libs = `${process.cwd()}/libs`;
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);
}
userApp.post('/', (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) => {
if(!emailSyntax(req.body.email)){
res.json({success: false, msg: 'O email Informado é inválido.'});
} else {
next();
}
}, (req, res, next) => {
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 Intituição em que trabalha é obrigatório.'});
} else {
next();
}
}, (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) => {
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) {
//no momento retorna essa mensagem se o email OU CPF já estiver cadastrado
res.json({success: false, msg: 'O email informado já está cadastrado.'});
} else {
res.json({success: true, msg: 'Usuário cadastrado com sucesso!'});
}
});
});
userApp.post('/authenticate', (req, res, next) => {
if (!req.body.email || !req.body.password) {
res.json({success: false, msg: 'Please pass email and password.'});
} else {
next();
}
}, (req, res, next) => {
User.findOne({
email: req.body.email
}, (err, user) => {
if (err) throw err;
if(!user){
res.json({success: false, msg: 'Authentication failed. User not found.'});
}
else {
user.comparePassword(req.body.password, (err, isMatch) => {
if (isMatch && !err) {
var secret = config.get('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});
}
else {
res.json({success: false, msg: 'Authentication failed. Wrong password'});
}
});
}
});
});
module.exports = userApp;