-
Notifications
You must be signed in to change notification settings - Fork 18
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
How to retrieve a readable session in middleware? #22
Comments
You might want to look at how axum-login does this: you'll want to pull the SessionHandle off the request and then can acquire either a readable or writable guard. |
https://github.com/maxcountryman/axum-login/blob/a27fe5e2b18cb1d3faf6590fdcc829d77824fdc3/src/auth.rs#L106 |
Yes that should be: it's the middleware itself which is hopefully helpful here.
|
Thanks, this is what I ended up with: pub async fn flash<S: Send + 'static>(
mut req: Request<S>,
next: Next<S>,
) -> Result<impl IntoResponse> {
let Extension(session_handle): Extension<SessionHandle> = req.extract_parts().await?;
let session = session_handle.read().await;
let payload = session.get::<bool>("signed_in").unwrap_or(false);
req.extensions_mut().insert(payload);
Ok(next.run(req).await)
} It seems like |
That's a good question: I don't know the answer. It might require looking at the implementation more closely (I don't recall if the docs mention this). |
Okay, I ran into another problem. This is what my router looks like: let hidden = Router::new()
.route("/login", get(handlers::get_login).post(handlers::login))
.route("/signup", get(handlers::get_signup).post(handlers::signup))
.route("/logout", get(handlers::logout));
Router::new()
.merge(SpaRouter::new("/static", "ui/static"))
.route("/", get(handlers::get_root))
.nest("/.api", hidden)
.layer(session_layer)
.route_layer(middleware::from_fn(flash))
.layer(TraceLayer::new_for_http())
.with_state(app) Whenever I visit |
I think your session layer should come before the flash middle, no? |
Yeah I managed to get that far. Now I figured out that my |
Ended up with a deadlock in my POST handler, trying to figure out what happened in |
Are you |
Sorry for the late reply. Skimmed through #13 before I realized I didn't drop my reader after spawning a writer in a function call where the reader was still in scope. |
The example on docs.rs cites the following code:
This is what I have:
However, I want to embed state in the
Request
so that the next handler knows whether the user is logged in or not. I'm using Askama for templating in case this is relevant.The text was updated successfully, but these errors were encountered: