From 325275d14c3b6dcf98dc88d3d1f27e40bda92773 Mon Sep 17 00:00:00 2001 From: Riccardo Sangiorgio Date: Wed, 24 Oct 2018 00:52:28 +0200 Subject: [PATCH] Admin partial search. Corretti alcuni errori. --- server/middleware/authenticate.js | 21 +++++- server/routes/api/v1/routes/admin.js | 86 ++++++++++++------------ server/routes/api/v1/routes/documents.js | 2 +- server/routes/api/v1/routes/subjects.js | 2 +- server/routes/api/v1/routes/users.js | 6 +- server/routes/login.js | 11 ++- server/server.js | 5 -- 7 files changed, 72 insertions(+), 61 deletions(-) diff --git a/server/middleware/authenticate.js b/server/middleware/authenticate.js index 7c3df34..e29d6a1 100644 --- a/server/middleware/authenticate.js +++ b/server/middleware/authenticate.js @@ -2,10 +2,12 @@ const passport = require('passport') // Config const { - bearer + bearer, + login } = require('../config/passport') passport.use('bearer', bearer) +passport.use('login', login) const authenticate = (req, res, next) => passport.authenticate('bearer', { session: false, @@ -25,6 +27,22 @@ const authenticate = (req, res, next) => passport.authenticate('bearer', { } })(req, res, next) +const signin = (req, res, next) => passport.authenticate('login', { + session: false, + failureFlash: true +}, (err, user, info) => { + if (err) { + next(new Error('Si è verificato un errore durante il login.')) + } else if (!user) { + return res.status(401).json({ + message: info + }) + } else { + req.user = user + return next() + } +})(req, res, next) + const authenticateAdmin = (req, res, next) => { if (req.user.privileges._id !== 'admin') { return res.status(401).json({ @@ -69,6 +87,7 @@ var authenticateAccesses = (req, res, next) => { module.exports = { authenticate, + signin, authenticateUser, authenticateAdmin, authenticateAccesses diff --git a/server/routes/api/v1/routes/admin.js b/server/routes/api/v1/routes/admin.js index a9eaf19..13b4806 100644 --- a/server/routes/api/v1/routes/admin.js +++ b/server/routes/api/v1/routes/admin.js @@ -93,53 +93,53 @@ router.get('/users/:id', authenticate, authenticateAdmin, asyncMiddleware(async * Utente admin */ router.get('/users/search/:key', authenticate, authenticateAdmin, asyncMiddleware(async (req, res) => { - // let regex = req.params.key.split(' ') - // regex = regex.join('|') - // - // let users = await User.find({ - // $and: [{ - // $or: [{ - // firstname: { - // $regex: regex, - // $options: 'i' - // } - // }, { - // lastname: { - // $regex: regex, - // $options: 'i' - // } - // }] - // }, { - // _id: { - // $ne: req.user._id - // } - // }, { - // state: { - // $ne: 'pending' - // } - // }] - // }) + let regex = req.params.key.split(' ') + regex = regex.join('|') let users = await User.find({ - $text: { - $search: req.params.key - }, - _id: { - $ne: req.user._id - }, - state: { - $ne: 'pending' - } - }, { - score: { - $meta: 'textScore' - } - }).sort({ - score: { - $meta: 'textScore' - } + $and: [{ + $or: [{ + firstname: { + $regex: regex, + $options: 'i' + } + }, { + lastname: { + $regex: regex, + $options: 'i' + } + }] + }, { + _id: { + $ne: req.user._id + } + }, { + state: { + $ne: 'pending' + } + }] }) + // let users = await User.find({ + // $text: { + // $search: req.params.key + // }, + // _id: { + // $ne: req.user._id + // }, + // state: { + // $ne: 'pending' + // } + // }, { + // score: { + // $meta: 'textScore' + // } + // }).sort({ + // score: { + // $meta: 'textScore' + // } + // }) + if (users.length && req.params.key.length !== 0) { res.status(200).json(users) } else { diff --git a/server/routes/api/v1/routes/documents.js b/server/routes/api/v1/routes/documents.js index 49c6a74..4d7c95f 100644 --- a/server/routes/api/v1/routes/documents.js +++ b/server/routes/api/v1/routes/documents.js @@ -50,7 +50,7 @@ const { const storage = multer.diskStorage({ destination: function (req, file, cb) { - cb(null, path.join(__dirname, '..', 'public', 'public', 'documents')) + cb(null, path.join(__dirname, '..', '..', '..', '..', 'public', 'public', 'documents')) }, filename: function (req, file, cb) { cb(null, new Date().toISOString() + path.extname(file.originalname)) diff --git a/server/routes/api/v1/routes/subjects.js b/server/routes/api/v1/routes/subjects.js index 3068293..bc0478c 100644 --- a/server/routes/api/v1/routes/subjects.js +++ b/server/routes/api/v1/routes/subjects.js @@ -19,7 +19,7 @@ router.get('/', asyncMiddleware(async (req, res) => { res.status(200).send(subjects) })) -router.post('/subjects/search/partial/', asyncMiddleware(async (req, res) => { +router.post('/search/partial/', asyncMiddleware(async (req, res) => { let query = req.body.query let regex = query.split(' ').join('|') diff --git a/server/routes/api/v1/routes/users.js b/server/routes/api/v1/routes/users.js index f292622..03e035f 100644 --- a/server/routes/api/v1/routes/users.js +++ b/server/routes/api/v1/routes/users.js @@ -64,7 +64,7 @@ const { router.get('/me/', authenticate, asyncMiddleware(async (req, res) => { let user = _.pick(req.user, ['_id', 'firstname', 'lastname', 'email', 'accesses', 'privileges']) - let documents = await Document.count({ + let documents = await Document.countDocuments({ author: user._id }) @@ -104,7 +104,7 @@ router.get('/me/documents/:visibility', authenticate, asyncMiddleware(async (req * Utente loggato */ router.get('/me/documents/count/:visibility', authenticate, asyncMiddleware(async (req, res) => { - let documents = await Document.count({ + let documents = await Document.countDocuments({ author: req.user._id, visibility: req.params.visibility }) @@ -239,7 +239,7 @@ router.post('/search/partial/', authenticate, asyncMiddleware(async (req, res) = /* Utente loggato */ -router.post('/me/logged', authenticate, (req, res) => { +router.post('/me/logged/', authenticate, (req, res) => { res.status(200).send() }) diff --git a/server/routes/login.js b/server/routes/login.js index daed007..7f09547 100644 --- a/server/routes/login.js +++ b/server/routes/login.js @@ -1,29 +1,26 @@ const express = require('express') -const passport = require('passport') const router = express.Router() // Config const { - login -} = require('../config/passport') + signin +} = require('../middleware/authenticate') // Middleware const { asyncMiddleware } = require('../middleware/async') -passport.use('login', login) - /* * Utente non loggato */ -router.post('/', passport.authenticate('login', { session: false }), asyncMiddleware(async (req, res) => { +router.post('/', signin, asyncMiddleware(async (req, res) => { const {user} = req let token try { token = await user.generateAuthToken() } catch (e) { - throw new Error('Si è verificato un errore durante la generazione del token.') + throw e } res.status(200).json({ token diff --git a/server/server.js b/server/server.js index 7c9cf0b..3791960 100644 --- a/server/server.js +++ b/server/server.js @@ -61,11 +61,6 @@ app.use((req, res, next) => { next() }) -// const { -// asyncMiddleware -// } = require('./middleware/async') -// app.use(asyncMiddleware) - // Routes app.use('/signup', signup) app.use('/login', login)