Accessing Middleware Values in a Marimo Cell
I'm running a FastAPI server with Marimo and using SessionMiddleware to manage authentication via a cookie containing the access token. Here's the FastAPI setup:
In the Marimo app code, I can access the access_token from the session middleware, but I can't figure out how to access the same value within a Marimo cell. Here's the cell code:
1. How can I properly access the values outside the cell (like access_token) in a Marimo cell?
2. Is there a recommended approach for passing session values from FastAPI's middleware into Marimo's context or cells?
Any guidance or examples would be appreciated!
10 Replies
Someone will reply to you shortly. In the meantime, this might help:
Hi, can you let me know whether this is useful for you: https://docs.marimo.io/guides/deploying/authentication/?h=middle#authentication
Authentication - marimo
The next generation of Python notebooks
Yes, this is essentially what I did and works great for authing access to the app itself.
However, what it doesn't solve is my use case where I want to be able to make api requests to our other api routes with something like requests.get() but need to attach the auth token to it and haven't found way to get that access token available within the cell itself.
Haven't yet had a use-case like that personally. Will let you know if I can figure out a way for this.
@coltone24 , it is not possible today to get the access token or user session. we've though about adding it to
mo.app_meta().user
, when running programatically@Myles Scolnick is there a reason the request headers can’t easily be exposed from within the Marimo cell, via mo.app_meta() or other method? I am realizing this is a hard requirement for an app I’m on the hook to deploy next week, and I’d like to hack a way around it. If there is any tips you may be able to provide, it’d v appreciated!
it might be hard to plumb it through, but not real reason. maybe security, but we can make that opt-in.
what do you need from the headers? if its just login/user-info, it might be easier than pulubming all the headers
Specifically in my case, to parse the keycloak auth token for the user info. Thinking that exposing headers, or at least as an opt-in, would afford the developer full flexibility when mounting marimo in a FastAPI app. Is there a specific format or set of formats you’d be looking to support for parsing user info in the headers?
I think this makes total sense and I am sure folks would be interested. Maybe a dict of the headers is easiest. I was thinking you could pass the user info too in a structured formatted of email, name, and get_token(). Does that answer your question?
It does, thanks - dict of headers would work great. Also like the structured user info and get_token() to streamline the more common use cases