Skip to content
Snippets Groups Projects
Commit eed77e36 authored by Rudolf Copi Eckelberg's avatar Rudolf Copi Eckelberg
Browse files

Merge branch 'auth' into simobject_20160927

parents 183a5b78 73edd413
No related branches found
No related tags found
2 merge requests!116Release v1.0.0,!23Simobject 20160927
Pipeline #
......@@ -13,7 +13,8 @@
"nrConnections": "16"
},
"mongodb" : {
"uri": "mongodb://localhost/users"
"uri": "mongodb://localhost/users",
"secret": "SimCAQC3SL"
},
"default": {
"api": {
......@@ -35,7 +36,8 @@
"nrConnections": "16"
},
"mongodb" : {
"uri": "mongodb://localhost/test_users"
"uri": "mongodb://localhost/test_users",
"secret": "SimCAQC3SL"
},
"default": {
"api": {
......@@ -57,7 +59,8 @@
"nrConnections": "16"
},
"mongodb" : {
"uri": "mongodb://localhost/users"
"uri": "mongodb://localhost/users",
"secret": "SimCAQC3SL"
},
"default": {
"api": {
......
......@@ -11,7 +11,7 @@ mongoose.Promise = global.Promise;
module.exports = () => {
// Get mongodb URI (ip and port) in config file
const mongoUri = process.env.MONGO_URI || config.mongodb.uri;
log.debug(`Connecting to MongDB on URI ${mongoUri}`);
log.debug(`Connecting to MongoDB on URI ${mongoUri}`);
// Connection singleton
const db = mongoose.connect(mongoUri);
......
......@@ -18,10 +18,6 @@ module.exports = function(passport){
return done(null, false, {message: 'Unknown user'});
}
if (!user.comparePassword(password)) {
return done(null, false, {message: 'Invalid password'});
}
return done(null, user);
});
}));
......
......@@ -22,33 +22,107 @@ function emailSyntax(email) {
userApp.post('/', (req, res, next) => {
if (!req.body.email) {
res.json({success: false, msg: 'Please pass 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: 'Please pass 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: 'Invalid email syntax.'});
res.json({success: false, msg: 'O email informado é inválido.'});
} else {
next();
}
}, (req, res, next) => {
for (let property of required_fields){
// if(!Object.prototype.hasOwnProperty.call(req.body, property)){
// res.json({success: false, msg: 'Please fill out all mandatory fields.'});
// return;
// }
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.'});
} 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) => {
User.count({'email': req.body.email}, function(err, count){
if (err){
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){
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,
......@@ -68,16 +142,23 @@ userApp.post('/', (req, res, next) => {
// save the user
newUser.save((err) => {
if (err) {
res.json({success: false, msg: 'Email already in use.'});
res.json({success: false, msg: 'Um erro ocorreu no banco de dados.'});
} else {
res.json({success: true, msg: 'Successful created new user.'});
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.'});
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();
}
......@@ -89,21 +170,21 @@ userApp.post('/authenticate', (req, res, next) => {
if (err) throw err;
if(!user){
res.json({success: false, msg: 'Authentication failed. User not found.'});
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.get('mongodb:secret');
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});
res.json({success: true, token: 'JWT ' + token, msg: 'Usuário autenticado com sucesso'});
}
else {
res.json({success: false, msg: 'Authentication failed. Wrong password'});
res.json({success: false, msg: 'A Senha informada é inválida.'});
}
});
}
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment