Nested Async/Await in guards #2202
-
use rocket::http::Status;
use rocket::outcome::Outcome;
use rocket::request::{self, FromRequest, Request};
use rocket::State;
use crate::crypto;
use crate::prisma::{user, PrismaClient};
struct Item<'r>(&'r str);
#[rocket::async_trait]
impl<'r> FromRequest<'r> for Item<'r> {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
let cookie = request.cookies().get("refresh_token");
match cookie {
Some(cookie) => {
let user = crypto::verify_refresh_token(cookie.value());
match user {
Ok(user) => request.guard::<&State<PrismaClient>>().await.map(|db| {
let user = db
.inner()
.user()
.find_unique(user::id::equals(user))
.exec()
.await; // <-- await doesn't work
Item("hi")
}),
Err(_) => Outcome::Failure((Status::Unauthorized, ())),
}
}
None => Outcome::Failure((Status::Forbidden, ())),
}
}
}
How would I get this example working? Also how would I return a failure outcome from that |
Beta Was this translation helpful? Give feedback.
Answered by
SergioBenitez
May 23, 2022
Replies: 1 comment
-
For this to work, Rust would need to support #[rocket::async_trait]
impl<'r> FromRequest<'r> for Item<'r> {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
let cookie = request.cookies().get("refresh_token");
match cookie {
Some(cookie) => {
let user = crypto::verify_refresh_token(cookie.value());
match user {
Ok(user) => {
let db = try_outcome!(request.guard::<&State<PrismaClient>>().await);
let user = db.inner()
.user()
.find_unique(user::id::equals(user))
.exec()
.await;
Outcome::Success(Item("hi"))
},
Err(_) => Outcome::Failure((Status::Unauthorized, ())),
}
}
None => Outcome::Failure((Status::Forbidden, ())),
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
samchouse
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For this to work, Rust would need to support
async
closures, which it currently does not. So, don't use a closure: