JavaScript
x
16
16
1
class Middleware:
2
def __init__(self, app):
3
self.app = app
4
5
6
def __call__(self, environ, start_response):
7
request = Request(environ)
8
cookies = request.cookies
9
path = request.path
10
11
12
if not isAuthenticated(cookies):
13
#Redirect to /login
14
15
return self.app(environ, start_response)
16
So I have a Middleware
class that is supposed to get the cookies
from the request
and then send it to a function isAuthenticated
which returns either True
or False
now if the function returns False
I need to redirect to /login
page is that possible to do? even though I don’t have the request
object I only have the environ
?
Advertisement
Answer
Not sure. Because WSGI middleware is at a layer outside the app itself.
You can use other hooks to work inside app / request context. In this case you can use flask.request
, flask.redirect()
etc. By the way, you don’t need to initialize the request (request = Request (Environment)
).