action event in StudentController refactorized (new event added in events.js)

parent ba9342c1
...@@ -41,17 +41,13 @@ module.exports = { ...@@ -41,17 +41,13 @@ module.exports = {
*/ */
login: function (req, res) { login: function (req, res) {
var bcrypt = require('bcrypt-nodejs'); var bcrypt = require('bcrypt-nodejs');
console.log('1');
Student.findOne({ Student.findOne({
username: req.body.username username: req.body.username
}) })
.then(function (student) { .then(function (student) {
console.log('2');
if (student) { if (student) {
console.log('3');
if (bcrypt.compareSync(req.body.password, student.password)) { if (bcrypt.compareSync(req.body.password, student.password)) {
student.isStudent = true; student.isStudent = true;
console.log('4');
res.ok({ res.ok({
user: student, user: student,
token: sailsTokenAuth.issueToken(student, sails.config.jwt.expiresInMinutes), token: sailsTokenAuth.issueToken(student, sails.config.jwt.expiresInMinutes),
...@@ -59,7 +55,6 @@ module.exports = { ...@@ -59,7 +55,6 @@ module.exports = {
.getTime() .getTime()
}); });
} else { } else {
console.log('5');
sails.log.error(`Invalid student login: user ${student.username}, password\ sails.log.error(`Invalid student login: user ${student.username}, password\
"${req.body.password}"`); "${req.body.password}"`);
res.badRequest(); res.badRequest();
...@@ -1020,13 +1015,7 @@ module.exports = { ...@@ -1020,13 +1015,7 @@ module.exports = {
const socketToOmit = (req.isSocket) ? req.socket : undefined; const socketToOmit = (req.isSocket) ? req.socket : undefined;
sails.hooks.events.broadcastEvent( sails.hooks.events.broadcastEvent(
sails.hooks.rooms.student(attributes.id_stu), sails.hooks.rooms.student(attributes.id_stu),
{ sails.hooks.events.actionPerformed(action, attributes),
name: 'action',
data: {
"action": action,
"attributes": attributes
}
},
socketToOmit socketToOmit
); );
......
...@@ -100,6 +100,23 @@ module.exports = function eventsHook(sails) { ...@@ -100,6 +100,23 @@ module.exports = function eventsHook(sails) {
count: subscriberCount count: subscriberCount
} }
}; };
},
/**
* A new action is performed by the user in the tablet
* @param {action} type of the action
* @param {attributes} attributes of the action (e.g. picto attributes when
* clicking on it)
* @return {Object} {name, data}
*/
actionPerformed: function (action, attributes) {
return {
name: 'action',
data: {
action: action,
attributes: attributes
}
};
} }
}; };
}; };
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