diff --git a/api/src/guards/request/session_auth.rs b/api/src/guards/request/session_auth.rs deleted file mode 100644 index 44063d4..0000000 --- a/api/src/guards/request/session_auth.rs +++ /dev/null @@ -1,71 +0,0 @@ -use entity::candidate::Model as Candidate; -use portfolio_core::sea_orm::prelude::Uuid; -use portfolio_core::services::admin_service::AdminService; -use portfolio_core::services::candidate_service::CandidateService; -use rocket::http::Status; -use rocket::outcome::Outcome; -use rocket::request::{FromRequest, Request}; - -use crate::pool::Db; - -pub struct CandidateAuth(Candidate); - -impl Into for CandidateAuth { - fn into(self) -> Candidate { - self.0 - } -} - -#[rocket::async_trait] -impl<'r> FromRequest<'r> for CandidateAuth { - type Error = Option; - async fn from_request(req: &'r Request<'_>) -> Outcome { - let session_id = req.cookies().get("id").unwrap().name_value().1; - let conn = &req.rocket().state::().unwrap().conn; - - let uuid = match Uuid::parse_str(&session_id) { - Ok(uuid) => uuid, - Err(_) => return Outcome::Failure((Status::BadRequest, None)), - }; - - let session = CandidateService::auth(conn, uuid).await; - - match session { - Ok(model) => Outcome::Success(CandidateAuth(model)), - Err(_) => Outcome::Failure((Status::Unauthorized, None)), - } - - } -} - -pub struct AdminAuth(Candidate); - -impl Into for AdminAuth { - fn into(self) -> Candidate { - self.0 - } -} - -#[rocket::async_trait] -impl<'r> FromRequest<'r> for AdminAuth { - type Error = Option; - async fn from_request(req: &'r Request<'_>) -> Outcome { - let session_id = req.cookies().get("id").unwrap().name_value().1; - let conn = &req.rocket().state::().unwrap().conn; - - let uuid = match Uuid::parse_str(&session_id) { - Ok(uuid) => uuid, - Err(_) => return Outcome::Failure((Status::BadRequest, None)), - }; - - let session = AdminService::auth(conn, uuid).await; - - match session { - Ok(model) => Outcome::Success(AdminAuth(model)), - Err(e) => Outcome::Failure( - (Status::from_code(e.code()).unwrap_or(Status::InternalServerError), None) - ), - } - - } -} \ No newline at end of file