-
SummaryHi, so I'm trying to add some functionality to my authentication middleware. This is the code I had before: // Middleware to extract user from session token
pub async fn auth_middleware(
cookies: CookieJar,
State(state): State<Arc<AppState>>,
mut request: Request,
next: Next,
) -> Result<Response, StatusCode> {
log::info!("auth middleware");
let token = cookies.get("auth_token").and_then(|cookie| Some(cookie.value().to_string()));
if token.is_none() {
return Err(StatusCode::UNAUTHORIZED);
}
let mgr_write = state.user_manager.write().await;
let token = token.unwrap();
log::info!("token: {}", &token);
let user = match mgr_write.validate_session(&token).await {
Ok(u) => u,
Err(_) => {
return Err(StatusCode::UNAUTHORIZED);
},
};
log::info!("user: {:#?}", user);
request.extensions_mut().insert(user);
Ok(next.run(request).await)
} This works fine for me, but when I try to add some nested logic within the match statement: Err(_) => {
// Attempt auto-migration of session token
let new_token = mgr_write.migrate_token(&token).await;
match new_token {
Ok(tok) => mgr_write.validate_session(&tok).await.unwrap(),
Err(_) => return Err(StatusCode::UNAUTHORIZED),
}
}, I get an error saying:
I assume this is due to how RwLocks work. I have tried different configurations such as limiting nesting and such, but the error seems persistent no matter what I do. The underlying datatype is as follows: pub user_manager: Arc<RwLock<UserManager>> And the concrete type isn't anything fancy either: pub struct UserManager {
db: Pool<Postgres>,
data_dir: PathBuf,
} Sorry if I'm missing anything obvious, but I've been at this for 3 hours at this point xd axum version0.8.3 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Try using |
Beta Was this translation helpful? Give feedback.
Try using
debug_middleware
, it might provide a better error.