Flask `RuntimeError: Working outside of application context.` from Middleware

Solution
app = Flask(__name__)
app.config.from_object('SETTINGS')
app.wsgi_app = Middleware(app.wsgi_app, app)
class Middleware:
    def __init__(self, wsgi, app):
        self.wsgi = wsgi
        self.app = app


    def __call__(self, environ, start_response):
        request = Request(environ)
        cookies = request.cookies
        path = request.path

        with self.app.app_context():
            environ['isAuthenticated'] = isAuthenticated(cookies)

        return self.wsgi(environ, start_response)