backend/app/routing/middleware/auth/ApiRoute.middleware.js
garrettmills 93d192d69c
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing
Add API endpoint for moving page nodes in tree
2021-01-02 21:50:28 -06:00

25 lines
704 B
JavaScript

const { Middleware } = require('libflitter')
class ApiRoute extends Middleware {
static get services() {
return [...super.services, 'models']
}
async test(req, res, next, { allow_public = false }) {
// 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