So I have a sso app that handles all the permissions of my microservices.
now whenever I build a page I have to run a function that checks if he has an authorization cookie if yes go ahead if not I need to redirect him depends on to a specific website depending on what the problem is.
now the function runs in the layout function of dash pages.
Any idea how I can redirect the user to another website like you can do in Django for example with HttpResponseRedirect?
Hi @jinnyzor But how do I do that?
I tried using flask redirect but got a dash.exception.InvalidCallbackReturnValue
returned a value having type āResponseā which is not JSON serializable
To perform redirects, it needs to be within a request. To do this, youād need something like:
from flask import Flask, redirect, request
server = Flask(__name__)
@server.before_request
def requestCheck():
if request.method == āGETā:
if not criteria:
redirect(target_url)
app = Dash(__name__, server = server)
You can find more server functions here for flask.
I worked with after_request and before_request with some of my stuff. Sometimes Iāll add things to the session cookie with before_request and remove it after_request because it makes it easier to refer to if in the session cookie. But it is too heavy and private to send back to the client browser.
Also, when you open up the server, it allows for really light weight use of post requests, instead of relying on get requests and post requests only going to _dash-update-component.
@jinnyzor
hi man, thanks for waiting, I was on a break from work till today (didnāt return on Saturday and took 2 more days off ) so didnāt have the chance to check your solution till now.
It works! thanks for helping (its the same situation for the CSS post you answered me I am not sure I will be checking it today hopefully by tomorrow I will confirm if it answers my needs but thanks for always helping!)
@jinnyzor ah too bad, because I wanted to redirect to the login page when the token expired. To detect the token expired, I wanted to use the after_request decorator to detect an 403 response from the backend Do you know an alternative to this?
At this point, it is a response, technically I think you can create your own response.
If this is within a dash callback, it needs to be handled differently, and there is actually a topic about this already on the forums. Look for āredirectā, it uses the before_request as well.
@jinnyzor Thank you so much for the hint! The redirect() function did not work with after_request but returning a response such as āreturn jsonify({āmultiā: True, āresponseā: {āurlā: {āpathnameā: ā/loginā}}})ā did work (thanks to @echeung, source: How to handle with redirect in Dash app? - #8 by echeung)