Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to get request headers in the frontend #2704

Open
ericfeunekes opened this issue Feb 23, 2024 · 2 comments
Open

Add the ability to get request headers in the frontend #2704

ericfeunekes opened this issue Feb 23, 2024 · 2 comments

Comments

@ericfeunekes
Copy link

When hosting reflex in Azure Web Apps you can use built in authentication providers. This also gives you access to the relevant tokens for any of those services, but to access them you need access to the request headers (see here).

It would be great to be able to access those request headers for example in a rx.State variable.

@ericfeunekes
Copy link
Author

Just found this: https://reflex.dev/docs/utility_methods/router_attributes/

It looks like all request headers should be accessible there. However, I'm still having trouble with passing request headers that come from the front end to the backend.

As background, I am hosting Reflex in Azure using web apps. I created two webapps: one for the frontend, one for the backend. Then I added Authentication. The Web App makes the authenticated user information available as injected headers.

The problem is that the authentication happens in the frontend web app so that's where the request headers are. I tried adding the auth to the backend, but it doesn't redirect traffic properly so the connection just fails.

So I need a way to pass the request headers that the front end receives to the backend. But because the router approaches all work with State variables, they happen in the backend. Looking for any suggestions!

@ericfeunekes ericfeunekes changed the title Add the ability to get request headers Add the ability to get request headers in the frontend Feb 26, 2024
@jq6l43d1
Copy link

I've been using MSAL to authenticate users of my reflex site for a while now. Here is the code I use.

State code:

    import msal

    logged_in: bool = False
    token_response: dict = {}

    def logged_in_check(self):
        """Checks if the user is logged in and redirects accordingly."""
        print("logged_in_check")
        print(self.router.page.path)
        if self.logged_in == False:
            auth_url = self.create_ms_app().get_authorization_request_url(
                ["User.Read"], 
                REDIRECT_URI=REDIRECT_URI
            )
            return rx.redirect(auth_url)

    def create_ms_app(self):
        """Creates a ConfidentialClientApplication for Microsoft authentication."""
        return msal.ConfidentialClientApplication(
            CLIENT_ID, 
            authority=AUTHORITY, 
            client_credential=CLIENT_SECRET
        )

    def ms_login(self):
        """Logs in the user using Microsoft authentication and updates the app state."""

        print('ms_login')

        auth_dict = self.get_query_params()
        auth_code = auth_dict['code']

        self.token_response = self.create_ms_app().acquire_token_by_authorization_code(
            auth_code, 
            ["User.Read"], 
            redirect_uri=REDIRECT_URI
        )

        if 'error' in self.token_response:
            print('error in token response')
            self.reset()
            return rx.redirect("/")
        
        self.account = self.token_response['id_token_claims']['preferred_username'].lower()
        self.username = self.token_response['id_token_claims']['name']
        self.first_name = self.username.split()[0]

        print(self.username)

        self.logged_in = True

        return rx.redirect("/")

page code:

app.add_page(
    index,
    on_load=State.logged_in_check,
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants