const { Middleware } = require('libflitter') class ApiRoute extends Middleware { static get services() { return [...super.services, 'models'] } async test(req, res, next, { allow_public = true }) { // If we have an authenticated session, just continue if ( req.is_auth ) { return next() } else if ( allow_public ) { const PublicUser = this.models.get('auth:PublicUser') req.user = await PublicUser.get_for_request(req) return next() } else { return res.status(401) .message('API authentication required') .api() } } } module.exports = ApiRoute