working on issue #269

parent f596a9a9
...@@ -798,25 +798,28 @@ module.exports = { ...@@ -798,25 +798,28 @@ module.exports = {
sails.log.debug("Inside actions_batch"); sails.log.debug("Inside actions_batch");
if (!params.actions)
return res.json(400, {'error': "no actions"});
// We loop through the actions and store them in the database // We loop through the actions and store them in the database
async.forEach(params.actions, function(action, cb){ async.forEach(params.actions, function(action, cb){
var id_dev = null; var id_dev = null;
if (action.attributes.id_dev) if (action.attributes.id_dev)
var id_dev = action.attributes.id_dev; id_dev = action.attributes.id_dev;
var id_sup = null; var id_sup = null;
if(action.attributes.id_sup) if(action.attributes.id_sup)
id_sup = action.attributes.sup; id_sup = action.attributes.sup;
var id_stu = null; var id_stu = null;
if(action.attributes.id_stu) if (action.attributes.id_stu)
id_stu = action.attributes.id_stu; id_stu = action.attributes.id_stu;
var desc = null; var desc = null;
if(action.attributes.picto) if(action.attributes.picto)
desc = action.attributes.picto; // select, add and delete actions data desc = action.attributes.picto; // select, add and delete actions data
if(action.attributes.pictos) if(action.attributes.pictos)
desc = action.attributes.pictos; // show action data desc = action.attributes.pictos; // show action data
Action.create({ Action.create({
...@@ -827,19 +830,23 @@ module.exports = { ...@@ -827,19 +830,23 @@ module.exports = {
device: id_dev, device: id_dev,
description: desc description: desc
}).exec(function (err, created) { }).exec(function (err, created) {
if (err) if (err) {
sails.log.error(err.details); console.log(err.details);
sails.log.error(err.details);
return cb(err);
}
else if (created) else if (created)
count++; count++;
cb(); cb();
}); });
}, },
function(err) { // function called when loop is done function(err) { // function called when loop is done
if (err) { if (err) {
sails.log.error(err.details); console.log(err.details);
res.json({'error': err.details}); sails.log.error(err.details);
return res.json({'error': err.details});
} else } else
res.json({'result': 'Ok', 'total': count}); return res.json({'result': 'Ok', 'total': count});
}); });
}, },
......
module.exports = function(req, res, next) { module.exports = function(req, res, next) {
var token; var token = null;
// //
// Token comes in the header // Token comes in the header
...@@ -8,11 +8,10 @@ module.exports = function(req, res, next) { ...@@ -8,11 +8,10 @@ module.exports = function(req, res, next) {
var parts = req.headers.authorization.split(' '); var parts = req.headers.authorization.split(' ');
if (parts.length == 2) { if (parts.length == 2) {
var scheme = parts[0], var scheme = parts[0],
credentials = parts[1]; credentials = parts[1];
if (/^Bearer$/i.test(scheme)) { if (/^Bearer$/i.test(scheme))
token = credentials; token = credentials;
}
} else { } else {
return res.json(401, {err: 'Format is Authorization: Bearer [token]'}); return res.json(401, {err: 'Format is Authorization: Bearer [token]'});
} }
...@@ -24,7 +23,7 @@ module.exports = function(req, res, next) { ...@@ -24,7 +23,7 @@ module.exports = function(req, res, next) {
token = req.param('token'); token = req.param('token');
// We delete the token from param to not mess with blueprints // We delete the token from param to not mess with blueprints
delete req.query.token; delete req.query.token;
} else { } else { // No token provided
return res.json(401, {err: 'No Authorization header was found'}); return res.json(401, {err: 'No Authorization header was found'});
} }
...@@ -32,7 +31,7 @@ module.exports = function(req, res, next) { ...@@ -32,7 +31,7 @@ module.exports = function(req, res, next) {
// We have a token, let's verify it // We have a token, let's verify it
// //
sailsTokenAuth.verifyToken(token, function(err, token) { sailsTokenAuth.verifyToken(token, function(err, token) {
if (err) return res.json(401, {err: 'Invalid token'}); if (err) return res.json(401, {err: 'Invalid token ' + token});
req.token = token; req.token = token;
next(); next();
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment