/**
* @module flitter-auth/middleware/GuestOnly
*/
const Middleware = require('libflitter/middleware/Middleware')
/**
* Middleware that allows only unauthenticated sessions to continue.
* @extends module:libflitter/middleware/Middleware~Middleware
*/
class GuestOnly extends Middleware {
/**
* Defines the services required by this middleware.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'configs']
}
/**
* Redirects the user via auth flow or default route if they are logged in.
* @param {express/request} req
* @param {express/response} res
* @param {function} next
* @param {object} [args = {}]
* @return {Promise<*>}
*/
async test(req, res, next, args = {}){
if ( req.is_auth ){
// If authenticated and there is a flow object,
// then the user just signed in and should be redirected
if ( req.session.auth.flow ){
const destination = req.session.auth.flow
req.session.auth.flow = false
return res.redirect(destination)
}
// Otherwise redirect away from this page
return res.redirect(this.configs.get('auth.default_login_route'))
}
else {
return next()
}
}
}
module.exports = GuestOnly