How to redirect users without a callback

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?

Hello @Matan,

You can redirect with the flask server. Other than that, I dont think there is a way to redirect without using the layout to access the url.

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

from flask import redirect

redirect(URL)

Any idea how to handle this?

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.

https://flask.palletsprojects.com/en/2.2.x/api/

and it works with dash pages?

It should, dash uses flask as its base server.

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. :stuck_out_tongue_winking_eye:

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.

@Matan,

Did you get this to work?

Hi man! I actually been on a break from work the past few days I will check it out this Saturday! didnā€™t forget :smile:

1 Like

Hey @Matan,

Following up again. :wink:

1 Like

@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 :smile: ) 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!)

1 Like

Glad you got some time off. :tada:

Also, glad you got it to work.

Hi @jinnyzor do you have a working example of redirect with after_request? Somehow, the redirect is ignored by my dash application.

I think after_request doesnā€™t work, it needs to be done before_request. :grin:

@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 :confused: 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 :smiley: (thanks to @echeung, source: How to handle with redirect in Dash app? - #8 by echeung)

1 Like