SZIYDOFDBT2H224T5Y6WGL6TFIKOMKGPS7T3NCGVFN7EQOLIHA6AC
}
}
static COOKIE_USER_ID_KEY: &str = "user_id";
use rocket::http::{CookieJar, Status};
use rocket::request::{FromRequest, Outcome};
use rocket::Request;
#[rocket::async_trait]
impl<'r> FromRequest<'r> for User {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> Outcome<User, ()> {
let db: &State<Database> = match request.guard().await {
Outcome::Success(db) => db,
_ => return Outcome::Failure((Status::Unauthorized, ())),
};
let cookies = request.cookies();
let some_id = match get_user_id_from_cookie(cookies) {
Some(id) => id,
None => return Outcome::Forward(()),
};
match User::find(db, some_id).await {
Ok(u) => Outcome::Success(u),
Err(_e) => Outcome::Forward(()), // TODO this is a failure
}
fn get_user_id_from_cookie(jar: &CookieJar) -> Option<i32> {
match jar.get_private(COOKIE_USER_ID_KEY) {
Some(id) => Some(id.value().parse::<i32>().ok()?),
None => None,
}
}
// TODO decide if this controller file is the place for this guard to be at?
// Feels like a constructor for a model, thus needs moving
#[rocket::async_trait]
impl<'r> FromRequest<'r> for User {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> request::Outcome<User, ()> {
let db: &State<Database> = match request.guard().await {
Outcome::Success(db) => db,
_ => return Outcome::Failure((Status::Unauthorized, ())),
};
let cookies = request.cookies();
let some_id = match get_user_id_from_cookie(cookies) {
Some(id) => id,
None => return Outcome::Forward(()),
};