Hi everyone,
My user’s token expires after some time, which means that my backend will return a ‘403 Unauthorized’ status code to the dash app after expiration. The user faces then a dash app that is ‘stalling’ without any message.
I need to redirect the user to the login page when he tries to use the dash app with an expired token. I tried to use a flask.redirect after a request encountered a 403 error:
import dash
import flask
port = 8123
host = "localhost"
app = dash.Dash(__name__)
server = app.server
@server.errorhandler(Exception)
def exception_handler(err):
return err.description, err.status_code
@server.after_request
def redirect_to_login(response):
if flask.request.path == "/_dash-update-component":
if response.status_code == 403:
redirect_req = flask.redirect(f"http://{host}:{port}/login", 303)
return redirect_req
return response
def main():
app.run(host=host, port=port, debug=True)
if __name__ == "__main__":
main()
Two problems show up:
- The following error in the browser’s console shows up upon redirect:
Uncaught (in promise) SyntaxError: Unexpected token ‘<’, "<!DOCTYPE "… is not valid JSON
because the redirect response is of html type.
- the redirect requests to /login shows up in the console’s network with a status 200, but the browser’s URL is not changed and the page not rendered.
Am I missing something with a flask redirect with the ‘after_request’ decorator or should I use something else?