mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-20 06:51:10 +00:00
refactor: split request guards into seperate files
This commit is contained in:
parent
1a4e471164
commit
8264187198
5 changed files with 85 additions and 2 deletions
40
api/src/guards/request/auth/admin.rs
Normal file
40
api/src/guards/request/auth/admin.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use entity::candidate::Model as Admin;
|
||||
use portfolio_core::sea_orm::prelude::Uuid;
|
||||
use portfolio_core::services::admin_service::AdminService;
|
||||
use rocket::http::Status;
|
||||
use rocket::outcome::Outcome;
|
||||
use rocket::request::{FromRequest, Request};
|
||||
|
||||
use crate::pool::Db;
|
||||
|
||||
pub struct AdminAuth(Admin);
|
||||
|
||||
impl Into<Admin> for AdminAuth {
|
||||
fn into(self) -> Admin {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for AdminAuth {
|
||||
type Error = Option<String>;
|
||||
async fn from_request(req: &'r Request<'_>) -> Outcome<AdminAuth, (Status, Self::Error), ()> {
|
||||
let session_id = req.cookies().get("id").unwrap().name_value().1;
|
||||
let conn = &req.rocket().state::<Db>().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)
|
||||
),
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
38
api/src/guards/request/auth/candidate.rs
Normal file
38
api/src/guards/request/auth/candidate.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
use entity::candidate::Model as Candidate;
|
||||
use portfolio_core::sea_orm::prelude::Uuid;
|
||||
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<Candidate> for CandidateAuth {
|
||||
fn into(self) -> Candidate {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for CandidateAuth {
|
||||
type Error = Option<String>;
|
||||
async fn from_request(req: &'r Request<'_>) -> Outcome<CandidateAuth, (Status, Self::Error), ()> {
|
||||
let session_id = req.cookies().get("id").unwrap().name_value().1;
|
||||
let conn = &req.rocket().state::<Db>().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)),
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
5
api/src/guards/request/auth/mod.rs
Normal file
5
api/src/guards/request/auth/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
pub mod admin;
|
||||
pub mod candidate;
|
||||
|
||||
pub use admin::*;
|
||||
pub use candidate::*;
|
||||
|
|
@ -1 +1 @@
|
|||
pub mod session_auth;
|
||||
pub mod auth;
|
||||
|
|
@ -3,7 +3,7 @@ extern crate rocket;
|
|||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use guards::request::session_auth::{CandidateAuth, AdminAuth};
|
||||
use guards::request::auth::{CandidateAuth, AdminAuth};
|
||||
use portfolio_core::services::candidate_service::CandidateService;
|
||||
use requests::{LoginRequest, RegisterRequest};
|
||||
use rocket::http::Status;
|
||||
|
|
|
|||
Loading…
Reference in a new issue