Why axum middleware does not work with "/" route ? #2606
Answered
by
davidpdrsn
HosMercury
asked this question in
Q&A
-
SummaryI have this router pub fn router() -> Router<AppState> {
Router::new().route("/", get(dashboard)).route("/t", get(t))
} These is the combined routes let app = dashboard::router()
.layer(middleware::from_fn(middlewares::auth_middlware))
.merge(auth::router())
.merge(oauth::router())
.layer(session_layer)
.with_state(state)
.nest_service("/assets", ServeDir::new("assets")); while this middleware is not working with the pub async fn auth_middlware(session: Session, request: Request, next: Next) -> Response {
println!("middlware request");
let user = session.get::<User>(USER_SESSION_KEY).await.unwrap();
println!("user session {:#?}", user);
if user.is_none() {
println!("if user session {:?}", user);
return Redirect::permanent("/login").into_response();
}
let response = next.run(request).await;
response
} what could be the reason? axum versionlatest |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Feb 18, 2024
Replies: 1 comment 5 replies
-
Middleware only apply to routes already added. So add all your routes first, then add middleware.. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah sorry I misread your code. Regardless middleware does work on “/“ so must be something else the rejects the request.
Have you tried simplifying your code until works? Perhaps try removing the
Session
extractor.