You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When updating an 0.4 application to 0.5, I've discovered a hiccup that I've not yet found a workaround for. We have a custom Responder impl on our error type that uses a request guard to determine how to respond: authenticated users get redirected to an error page with some details, but unauthenticated users get redirected to /login with a bare-bones message.
Unfortunately, in 0.5 Request::guard is async, but Responder::respond_to is sync. In #1065 (comment) it was clarified that Responder is intentionally sync, which makes sense.
The only solution I can see here is to front-load the request guard into each and every handler and explicitly pass it along to the error type. That would work, but seems to be a big cudgel for what should be a pretty small problem. Any other ideas?
To Reproduce
pub struct SessionData;
#[rocket::async_trait]
impl<'r> FromRequest<'r> for SessionData {
// ...
}
pub struct ErrorResponse;
impl<'r, 'o: 'r> response::Responder<'r, 'o> for ErrorResponse {
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> {
// UNABLE TO AWAIT HERE
match req.guard::<SessionData>() {
// ...
}
}
}
Expected Behavior
Some way to get the value of a request guard in a responder.
The text was updated successfully, but these errors were encountered:
After further reflection, I think the best answer is to cache the guard in request-local storage, and rely on that in the responder. This is good enough for my purposes, though it is incomplete: on unauthenticated routes the session guard will never be invoked, so the request-local storage will be empty.
I've been working on a library for rocket and I've found myself in a similar situation. In my case, I'm trying to write a configurable LangCode request guard. This type would be used to get the preferred language for a client request and can hopefully be consumed by other values. This way users can do things like this:
use authentication_lib::{Error,Auth,LoginForm};// here the handler returns an Error from the library which knows in what language // to display the error message because it can retrieve the `LangCode` guard in // the responder// the user doesn't need to worry about that stuff because it is already handled by the library. // they just try `?` the error and move on. #[get("/foo/", data="<form>")]fnlogin(form:LoginForm,auth:Auth<'_>) -> Result<Template,Error>{
auth.login(form).await?;/* .. */}
Description
When updating an 0.4 application to 0.5, I've discovered a hiccup that I've not yet found a workaround for. We have a custom
Responder
impl on our error type that uses a request guard to determine how to respond: authenticated users get redirected to an error page with some details, but unauthenticated users get redirected to/login
with a bare-bones message.Unfortunately, in 0.5
Request::guard
is async, butResponder::respond_to
is sync. In #1065 (comment) it was clarified thatResponder
is intentionally sync, which makes sense.The only solution I can see here is to front-load the request guard into each and every handler and explicitly pass it along to the error type. That would work, but seems to be a big cudgel for what should be a pretty small problem. Any other ideas?
To Reproduce
Expected Behavior
Some way to get the value of a request guard in a responder.
The text was updated successfully, but these errors were encountered: