/**
 * @module flitter-auth/deploy/routing/middleware/RequireGuest
 */

/**
 * This middleware is provided by Flitter-auth. It will redirect the user
 * back to their previous location if the session contains the user object.
 * 
 * @class
 */
class RequireGuest {

    /**
     * Run the middleware test. If an authenticated session exists, redirect the user to an error page.
     * Otherwise, allow the request to continue.
     * @param {Express/Request} req - the incoming Express request
     * @param {Express/Response} res - the corresponding Express response
     * @param {Function} next - The callback to continue the Express request handling stack. This is called if the middleware check passes.
     */
    test(req, res, next){
        if ( req.session && req.session.auth && (req.session.auth.authenticated === true || req.session.auth.user) ){
            return _flitter.view(res, 'errors/requires_guest')
        }

        /*
         * Call the next function in the stack.
         */
        next()
    }
}

module.exports = RequireGuest