Add public user support, break API into individual files

This commit is contained in:
2020-11-10 20:57:43 -06:00
parent 37f9c09fe2
commit 4636521d50
16 changed files with 371 additions and 134 deletions

View File

@@ -0,0 +1,25 @@
const { Middleware } = require('libflitter')
class ApiRoute extends Middleware {
static get services() {
return [...super.services, 'models']
}
async test(req, res, next, { allow_public = false }) {
console.log({allow_public})
// 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 {
// If not signed in, save the target url so we can redirect back here after auth
req.session.auth.flow = req.originalUrl
return res.redirect('/auth/login')
}
}
}
module.exports = ApiRoute