Mercurial > lbo > hg > analyrics
view src/guards.rs @ 80:d3368a7142ef default tip
Make analyrics build again and add Cargo.lock
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 27 Apr 2024 20:34:05 +0200 |
parents | fd0237049be0 |
children |
line wrap: on
line source
use anyhow::{self, Error}; use rocket::http::HeaderMap; use rocket::request::{FromRequest, Outcome, Request}; pub const USER_ID_COOKIE_KEY: &str = "user_id"; pub struct LoggedInGuard(pub String); #[rocket::async_trait] impl<'r> FromRequest<'r> for LoggedInGuard { type Error = Error; async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> { let cookies = req.cookies(); if let Some(uc) = cookies.get_private(USER_ID_COOKIE_KEY) { Outcome::Success(LoggedInGuard(uc.value().to_string())) } else { Outcome::Forward(rocket::http::Status { code: 401 }) } } } pub struct HeadersGuard<'h>(pub HeaderMap<'h>); #[rocket::async_trait] impl<'r> FromRequest<'r> for HeadersGuard<'r> { type Error = Error; async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> { Outcome::Success(HeadersGuard(req.headers().clone())) } }