Im trying to write req guard that will either return user_id: String
or just terminate request with Status::Unauthorized
use rocket::request::{Request, FromRequest, Outcome};
use rocket::outcome::IntoOutcome;
use rocket::{async_trait};
use rocket::http::Status;
use mongodb::bson::doc;
// modules
use crate::DB;
use crate::user_module::user_model::UserModel;
struct Auth {
}
#[async_trait]
impl<'r> FromRequest<'r> for Auth {
type Error = ();
async fn from_request(req: &'r Request<'_>) -> Outcome<String, ()> {
let db = DB.get().unwrap();
let users_collection = db.collection::<UserModel>("users");
req.cookies()
.get_private("user_id")
.and_then(|cookie| cookie.value().parse::<String>().ok())
.or_forward(Status::Unauthorized)
}
}
The problem is… it always gives me an error, doesn’t matter what i’m changing
Right now, problem is
method `from_request` has an incompatible type for trait
expected signature `fn(&'r rocket::Request<'_>) -> Pin<Box<(dyn std::future::Future<Output = Outcome<guards::Auth::Auth, (Status, ()), Status>> + std::marker::Send + 'async_trait)>>`
found signature `fn(&'r rocket::Request<'_>) -> Pin<Box<(dyn std::future::Future<Output = Outcome<std::string::String, (Status, ()), Status>> + std::marker::Send + 'async_trait)>>`
and i have no idea what this is about
The guide i found is… idk incomplete? Where is struct
?
New contributor
Kristiano Odadu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.