User routes

pull/1/head
sundowndev 2018-11-15 19:28:33 +01:00
parent cb092b7f91
commit 8201236c7d
3 changed files with 46 additions and 10 deletions

View File

@ -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;

View File

@ -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);
});
};

View File

@ -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.' }));
};