From 8201236c7de634fcd68105155b5e18006cdc64aa Mon Sep 17 00:00:00 2001 From: sundowndev Date: Thu, 15 Nov 2018 19:28:33 +0100 Subject: [PATCH] User routes --- app/routes/user/index.js | 20 +++++++++++++------- app/routes/user/notes.js | 15 +++++++++++++++ app/routes/user/profile.js | 21 ++++++++++++++++++--- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/app/routes/user/index.js b/app/routes/user/index.js index 0f5d429..42ffe60 100644 --- a/app/routes/user/index.js +++ b/app/routes/user/index.js @@ -3,34 +3,40 @@ const user = require('express').Router(); const profile = require('./profile'); const update = require('./update'); const remove = require('./delete'); -// const notes = require('./notes'); +const notes = require('./notes'); + +const Authentication = require.main.require('./app/validation/auth'); /** - * @api {get} /user/me Get user information + * @api {get} /user/me Get account information * @apiName GetUser * @apiGroup User * * @apiSuccess {String} firstname Firstname of the User. * @apiSuccess {String} lastname Lastname of the User. - * @apiSuccess {String} username Username of the User. + * @apiSuccess {String} email Email of the User. */ -user.get('/me', profile); +user.get('/me', Authentication, profile); /** * @api {put} /user/me Update account information * @apiName UpdateUser * @apiGroup User * + * @apiParam {String} Firstname new firstname. + * @apiParam {String} Lastname new lastname. + * @apiParam {String} Email new email address. + * * @apiSuccess {Object} user User object. */ -user.put('/me', update); +user.put('/me', Authentication, update); /** * @api {delete} /user/me Delete account * @apiName DeleteUser * @apiGroup User */ -user.delete('/me', remove); +user.delete('/me', Authentication, remove); /** * @api {get} /user/me/notes Get all notes @@ -39,6 +45,6 @@ user.delete('/me', remove); * * @apiSuccess {Array} Array Notes of the user. */ -user.get('/me/notes', profile); +user.get('/me/notes', Authentication, notes); module.exports = user; diff --git a/app/routes/user/notes.js b/app/routes/user/notes.js index e69de29..18140ce 100644 --- a/app/routes/user/notes.js +++ b/app/routes/user/notes.js @@ -0,0 +1,15 @@ +const mongoose = require('mongoose'); +const jwt = require('jsonwebtoken'); + +module.exports = (req, res) => { + const NoteModel = mongoose.model('Note'); + + const { user } = jwt.decode(req.headers.authorization); + + NoteModel.find({ user: user.id }) + .lean() + .exec() + .then((notes) => { + res.status(200).json(notes); + }); +}; diff --git a/app/routes/user/profile.js b/app/routes/user/profile.js index 0e97c8b..66c0e4b 100644 --- a/app/routes/user/profile.js +++ b/app/routes/user/profile.js @@ -1,5 +1,20 @@ -module.exports = (req, res) => { - const user = {}; +const mongoose = require('mongoose'); +const jwt = require('jsonwebtoken'); - res.status(200).json({ user }); +module.exports = (req, res, next) => { + const UserModel = mongoose.model('User'); + + const { user } = jwt.decode(req.headers.authorization); + + return UserModel.findById(user.id, 'id firstname lastname email') + .lean() + .exec() + .then((result) => { + if (result === null) { + return next({ status: 401, message: 'User does not exists.' }); + } + + return res.status(200).json(result); + }) + .catch(() => next({ status: 401, message: 'User does not exists.' })); };