Is it ok to still use Extension<CurrentUser>
with 0.6?
#1659
Answered
by
davidpdrsn
frederikhors
asked this question in
Q&A
-
I upgraded to 0.6 from 0.5 and I removed Now a doubt: should I remove pub async fn middleware_check_authenticated_user<B>(
State(app_state): State<Arc<AppState>>,
mut req: Request<B>,
next: Next<B>,
) -> Result<Response, StatusCode> {
let cookie = req
.headers()
.get("Cookie")
.and_then(|c| c.to_str().ok())
.unwrap_or("");
let is_authenticated = app_state.check_is_authenticated(cookie);
if is_authenticated {
match serde_json::from_slice::<CurrentUser>(
&hyper::body::to_bytes(is_authenticated.into_body()).await.unwrap(),
) {
Ok(current_user) => {
req.extensions_mut().insert(current_user);
return Ok(next.run(req).await);
}
Err(err) => {
eprintln!("{err}");
}
}
}
Err(StatusCode::UNAUTHORIZED)
} Using it like: pub async fn api_handler(
State(app_state): State<Arc<AppState>>,
current_user: Extension<CurrentUser>,
req: Request,
) -> Response {
app_state
.api
.execute(req.into_inner().data(current_user))
.await
.into()
} Is it ok to still use Or should I use a new (compile-time-safe) |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Dec 23, 2022
Replies: 1 comment
-
You still need to use Extension for request specific things. State is shared with all requests. So yes it's fine. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
frederikhors
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You still need to use Extension for request specific things. State is shared with all requests. So yes it's fine.