eecs448-project-4/app/routing/middleware/InjectUserTeam.middleware.js

34 lines
848 B
JavaScript
Raw Normal View History

2020-11-05 03:18:47 +00:00
const { Middleware } = require('libflitter')
2020-11-08 18:34:50 +00:00
/**
2020-11-05 03:18:47 +00:00
* InjectUserTeam Middleware
* -------------------------------------------------------------
* For the authenticated user, looks up the associated Team instance
2020-11-08 18:34:50 +00:00
* and injects it as request.user_team.
*
* @extends Middleware
2020-11-05 03:18:47 +00:00
*/
class InjectUserTeam extends Middleware {
static get services() {
return [...super.services, 'models']
}
2020-11-08 18:34:50 +00:00
/**
* Inject the user's team into the request, or redirect to a login page.
* @param req
* @param res
* @param next
* @param [args = {}]
2020-11-05 03:18:47 +00:00
*/
async test(req, res, next, args = {}){
if ( !req.user ) return res.redirect('/auth/login')
const Team = this.models.get('Team')
req.user_team = await Team.getForUser(req.user)
return next()
}
}
module.exports = InjectUserTeam