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:

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.