From bf1bbcc3d4cad41be470b7816d7c695074b0030b Mon Sep 17 00:00:00 2001 From: Foad Ardalan Date: Wed, 21 Mar 2018 13:03:47 +0100 Subject: [PATCH 1/5] retrieve user s role from login and register path --- .../users.authentication.server.controller.js | 14 ++++++++------ .../modules/users/server/services/user.service.js | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/server/modules/users/server/controllers/users/users.authentication.server.controller.js b/server/modules/users/server/controllers/users/users.authentication.server.controller.js index eb0d7c056..b4f2b50b9 100644 --- a/server/modules/users/server/controllers/users/users.authentication.server.controller.js +++ b/server/modules/users/server/controllers/users/users.authentication.server.controller.js @@ -27,12 +27,12 @@ var noReturnUrls = [ exports.signup = async function (req, res, next) { try { const user = await UserService.signUp(req.body) - const { _id, firstName, lastName, email, username } = user - const payload = { id: _id, firstName, lastName, email, username } + const { _id, firstName, lastName, email, username, roles } = user; + const payload = { id: _id, firstName, lastName, email, username, roles } const token = jwt.sign(payload, config.jwt.secret) return res.status(200) .cookie('TOKEN', token, { maxAge: 900000, httpOnly: true }) - .json({ user: payload, tokenExpiresIn: 101010010101 }) + .json({ user: payload, tokenExpiresIn: (new Date(Date.now() + 60 * 60 * 24 * 1000)).getTime() }) } catch(err) { return next(new ApiError(err.message)) } @@ -42,12 +42,14 @@ exports.signup = async function (req, res, next) { * Signin after passport authentication */ exports.signin = async function (req, res) { - const { _id, firstName, lastName, email, username } = req.user; - const payload = { id: _id, firstName, lastName, email, username }; + console.log('??????????????', req.user) + + const { _id, firstName, lastName, email, username, roles } = req.user; + const payload = { id: _id, firstName, lastName, email, username, roles }; const token = jwt.sign(payload, configuration.jwt.secret); return res.status(200) .cookie('TOKEN', token) - .json({ user: payload, tokenExpiresIn: 10101010101 }); + .json({ user: payload, tokenExpiresIn: (new Date(Date.now() + 60 * 60 * 24 * 1000)).getTime() }); }; /** diff --git a/server/modules/users/server/services/user.service.js b/server/modules/users/server/services/user.service.js index 9211949c0..d13cf6645 100644 --- a/server/modules/users/server/services/user.service.js +++ b/server/modules/users/server/services/user.service.js @@ -27,6 +27,7 @@ class UserService { username: user.username, email: user.email, provider: user.provider, + roles: user.roles, created: user.created, } } From d44fd0fc1c3881c9fddbadfa44fafe171ea7bdc1 Mon Sep 17 00:00:00 2001 From: Foad Ardalan Date: Wed, 21 Mar 2018 13:07:05 +0100 Subject: [PATCH 2/5] console.log --- .../controllers/users/users.authentication.server.controller.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/server/modules/users/server/controllers/users/users.authentication.server.controller.js b/server/modules/users/server/controllers/users/users.authentication.server.controller.js index b4f2b50b9..ab96ae161 100644 --- a/server/modules/users/server/controllers/users/users.authentication.server.controller.js +++ b/server/modules/users/server/controllers/users/users.authentication.server.controller.js @@ -42,8 +42,6 @@ exports.signup = async function (req, res, next) { * Signin after passport authentication */ exports.signin = async function (req, res) { - console.log('??????????????', req.user) - const { _id, firstName, lastName, email, username, roles } = req.user; const payload = { id: _id, firstName, lastName, email, username, roles }; const token = jwt.sign(payload, configuration.jwt.secret); From 72128268b5d8ad6e1cb2a03ed8243314cf2f0186 Mon Sep 17 00:00:00 2001 From: Foad Ardalan Date: Wed, 21 Mar 2018 19:05:04 +0100 Subject: [PATCH 3/5] fixing the profile modifiation endpoint --- .../controllers/users/users.profile.server.controller.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/modules/users/server/controllers/users/users.profile.server.controller.js b/server/modules/users/server/controllers/users/users.profile.server.controller.js index e1978c3c8..77fce259d 100644 --- a/server/modules/users/server/controllers/users/users.profile.server.controller.js +++ b/server/modules/users/server/controllers/users/users.profile.server.controller.js @@ -28,8 +28,7 @@ exports.update = function (req, res) { user.updated = Date.now(); user.displayName = user.firstName + ' ' + user.lastName; - - user.save(function (err) { + User.findByIdAndUpdate(user.id, user, function (err) { if (err) { return res.status(422).send({ message: errorHandler.getErrorMessage(err) From 69c5e08c639345939bf58b73b8613f71ecab234a Mon Sep 17 00:00:00 2001 From: Foad Ardalan Date: Fri, 23 Mar 2018 12:25:02 +0100 Subject: [PATCH 4/5] adapting to front --- .../controllers/tasks.server.controller.js | 21 +++++++++++-------- .../sequelize/task.server.model.sequelize.js | 5 +++++ .../server/routes/tasks.server.routes.js | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/server/modules/tasks/server/controllers/tasks.server.controller.js b/server/modules/tasks/server/controllers/tasks.server.controller.js index d2940fb63..0765a0fac 100644 --- a/server/modules/tasks/server/controllers/tasks.server.controller.js +++ b/server/modules/tasks/server/controllers/tasks.server.controller.js @@ -11,6 +11,7 @@ exports.getAllTasks = function (req, res) { orm.Task.findAll().then(function (tasks) { res.status(200).send(tasks); }).catch(function (error) { + console.log(error) res.status(500).send(error); }); @@ -42,14 +43,14 @@ exports.addTask = function (req, res) { }; exports.updateTask = function (req, res) { - - orm.Task.update(req.body, { - where: { - id: req.body.id - } - }).then(function (tasks) { - res.status(200).send(tasks); + console.log('req.body', req.body); + orm.Task.update( + { description: req.body.description, title: req.body.title }, + { where: { id: req.body.id } + }).then(function (task) { + res.status(200).send(req.body); }).catch(function (error) { + console.log(error) res.status(500).send(error); }); @@ -57,12 +58,14 @@ exports.updateTask = function (req, res) { exports.deleteTask = function (req, res) { + const taskId = req.params.id + orm.Task.destroy({ where: { - id: req.body.id + id: taskId } }).then(function (tasks) { - res.status(200).send(tasks); + res.status(200).send({ taskId }); }).catch(function (error) { res.status(500).send(error); }); diff --git a/server/modules/tasks/server/models/sequelize/task.server.model.sequelize.js b/server/modules/tasks/server/models/sequelize/task.server.model.sequelize.js index d67f2b0fb..296987df7 100644 --- a/server/modules/tasks/server/models/sequelize/task.server.model.sequelize.js +++ b/server/modules/tasks/server/models/sequelize/task.server.model.sequelize.js @@ -12,6 +12,11 @@ module.exports = function(sequelize, DataTypes) { allowNull: false, comment: 'The user who created and owns this task' }, + description: { + type: DataTypes.STRING, + allowNull: true, + comment: 'Some descriptions' + }, }, { classMethods: { associate: function (models) { diff --git a/server/modules/tasks/server/routes/tasks.server.routes.js b/server/modules/tasks/server/routes/tasks.server.routes.js index b430d852b..e5b6214ea 100644 --- a/server/modules/tasks/server/routes/tasks.server.routes.js +++ b/server/modules/tasks/server/routes/tasks.server.routes.js @@ -12,6 +12,6 @@ module.exports = function (app) { // CRUD actions app.route('/api/tasks').post(tasks.validateSessionUser, tasks.addTask); app.route('/api/tasks').put(tasks.validateSessionUser, tasks.updateTask); - app.route('/api/tasks').delete(tasks.validateSessionUser, tasks.deleteTask); + app.route('/api/tasks/:id').delete(tasks.validateSessionUser, tasks.deleteTask); }; From 221813d19e4c84c020c0e0b99c8339b290925727 Mon Sep 17 00:00:00 2001 From: Foad Ardalan Date: Fri, 23 Mar 2018 15:29:03 +0100 Subject: [PATCH 5/5] console.log --- .../modules/tasks/server/controllers/tasks.server.controller.js | 1 - 1 file changed, 1 deletion(-) diff --git a/server/modules/tasks/server/controllers/tasks.server.controller.js b/server/modules/tasks/server/controllers/tasks.server.controller.js index 0765a0fac..8031771cc 100644 --- a/server/modules/tasks/server/controllers/tasks.server.controller.js +++ b/server/modules/tasks/server/controllers/tasks.server.controller.js @@ -43,7 +43,6 @@ exports.addTask = function (req, res) { }; exports.updateTask = function (req, res) { - console.log('req.body', req.body); orm.Task.update( { description: req.body.description, title: req.body.title }, { where: { id: req.body.id }