diff --git a/api/controllers/items.js b/api/controllers/items.js index 066b4b1..46aa4f8 100644 --- a/api/controllers/items.js +++ b/api/controllers/items.js @@ -43,8 +43,7 @@ exports.getAllItems = (req, res, next) => { //Handle post request to create a new item in the database exports.newItem = (req, res, next) => { //The poster's id is obtained from the token and stored in the databse along with the item - const decoded = jwt.decode(req.headers.authorization.split(' ')[1]); - const userId = decoded.username; + const userId = req.userData._id; const item = new Item({ _id: new mongoose.Types.ObjectId(), name: req.body.name, diff --git a/api/controllers/messages.js b/api/controllers/messages.js index 1e9c1c9..ce0f06b 100644 --- a/api/controllers/messages.js +++ b/api/controllers/messages.js @@ -7,8 +7,7 @@ const mongoose = require('mongoose'); //Handles get request to show all messages in the database targetted at the user exports.getInbox = (req, res, next) => { //By obtaning the username from the token, finds the messages within the database that are specifically targetted at the user - const decoded = jwt.decode(req.headers.authorization.split(' ')[1]); - const userId = decoded.username; + const userId = req.userData._id; Message.find({ destination: userId }).select('-__v').exec() .then(docs => { const response = { @@ -72,8 +71,7 @@ exports.sendMessage = (req, res, next) => { //Handles get requests where for messages sent by, rather than to, the user exports.getSent = (req, res, next) => { - const decoded = jwt.decode(req.headers.authorization.split(' ')[1]); - const userId = decoded.username; + const userId = req.userData._id; Message.find({ sender: userId }).select('-__v').exec() .then(docs => { const response = {