summary refs log tree commit diff
path: root/src/api/guards/session.rs
blob: ccd40a2b19d39ae345e6f4a9c8193251443b614e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use std::str::FromStr;

use rocket::{http::Status, outcome::Outcome, request::{self, Request, FromRequest}};
use sqlx::PgPool;
use uuid::Uuid;
use crate::db::models::auth::LoginSession;


#[rocket::async_trait]
impl<'r> FromRequest<'r> for LoginSession {
    type Error = ();

    async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
        // At the moment just always look up this one for initial testing
        let hardcoded_uuid = "557621dd-b98e-409a-9ce7-25b5004caa42";

        if let Some(db) = req.rocket().state::<PgPool>() {
            if let Some(session) = LoginSession::lookup(&db, Uuid::from_str(hardcoded_uuid).unwrap()).await {
                return Outcome::Success(session);
            }
        }

        Outcome::Error((Status::InternalServerError, ()))
    }
}