backend/app/routing/middleware/api/UserRoute.middleware.js

36 lines
1.1 KiB
JavaScript

const Middleware = require('libflitter/middleware/Middleware')
/*
* UserRoute Middleware
* -------------------------------------------------------------
* Put some description here!
*/
class UserRoute extends Middleware {
static get services() {
return [...super.services, 'models']
}
/*
* Run the middleware test.
* This method is required by all Flitter middleware.
* It should either call the next function in the stack,
* or it should handle the response accordingly.
*/
async test(req, res, next, {allow_public_user = false}){
const User = this.models.get('auth:User')
const user_id = req.form.user_id ? req.form.user_id : req.params.user_id
if ( !user_id ) return res.status(400).message('Midding user_id.').api({})
const user = await User.findById(user_id)
if ( !user && !allow_public_user ) return res.status(404).message('Unable to find user with that ID.').api({})
if ( !req.form ) req.form = {}
req.form.user = user
next()
}
}
module.exports = exports = UserRoute