2020-11-11 02:57:43 +00:00
|
|
|
const { Middleware } = require('libflitter')
|
|
|
|
|
|
|
|
class ApiRoute extends Middleware {
|
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models']
|
|
|
|
}
|
|
|
|
|
2021-03-04 17:26:14 +00:00
|
|
|
async test(req, res, next, { allow_public = true }) {
|
2020-11-11 02:57:43 +00:00
|
|
|
// 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 {
|
2020-11-11 18:23:31 +00:00
|
|
|
return res.status(401)
|
|
|
|
.message('API authentication required')
|
|
|
|
.api()
|
2020-11-11 02:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ApiRoute
|