mirror of
https://github.com/danbulant/Portfolio
synced 2026-07-04 02:20:50 +00:00
refactor: SessionAuth
This commit is contained in:
parent
3b9bf7cdda
commit
c37d6b970e
2 changed files with 7 additions and 7 deletions
|
|
@ -7,18 +7,18 @@ use rocket::request::{FromRequest, Request};
|
||||||
|
|
||||||
use crate::pool::Db;
|
use crate::pool::Db;
|
||||||
|
|
||||||
pub struct SessionAuth(Candidate);
|
pub struct CandidateAuth(Candidate);
|
||||||
|
|
||||||
impl Into<Candidate> for SessionAuth {
|
impl Into<Candidate> for CandidateAuth {
|
||||||
fn into(self) -> Candidate {
|
fn into(self) -> Candidate {
|
||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::async_trait]
|
#[rocket::async_trait]
|
||||||
impl<'r> FromRequest<'r> for SessionAuth {
|
impl<'r> FromRequest<'r> for CandidateAuth {
|
||||||
type Error = Option<String>;
|
type Error = Option<String>;
|
||||||
async fn from_request(req: &'r Request<'_>) -> Outcome<SessionAuth, (Status, Self::Error), ()> {
|
async fn from_request(req: &'r Request<'_>) -> Outcome<CandidateAuth, (Status, Self::Error), ()> {
|
||||||
let session_id = req.cookies().get("id").unwrap().name_value().1;
|
let session_id = req.cookies().get("id").unwrap().name_value().1;
|
||||||
let conn = &req.rocket().state::<Db>().unwrap().conn;
|
let conn = &req.rocket().state::<Db>().unwrap().conn;
|
||||||
|
|
||||||
|
|
@ -30,7 +30,7 @@ impl<'r> FromRequest<'r> for SessionAuth {
|
||||||
let session = CandidateService::auth(conn, uuid).await;
|
let session = CandidateService::auth(conn, uuid).await;
|
||||||
|
|
||||||
match session {
|
match session {
|
||||||
Ok(model) => Outcome::Success(SessionAuth(model)),
|
Ok(model) => Outcome::Success(CandidateAuth(model)),
|
||||||
Err(_) => Outcome::Failure((Status::Unauthorized, None)),
|
Err(_) => Outcome::Failure((Status::Unauthorized, None)),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ extern crate rocket;
|
||||||
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
use guards::request::session_auth::SessionAuth;
|
use guards::request::session_auth::CandidateAuth;
|
||||||
use portfolio_core::services::candidate_service::CandidateService;
|
use portfolio_core::services::candidate_service::CandidateService;
|
||||||
use requests::{LoginRequest, RegisterRequest};
|
use requests::{LoginRequest, RegisterRequest};
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
|
|
@ -47,7 +47,7 @@ async fn create(conn: Connection<'_, Db>, post_form: Json<RegisterRequest>) -> R
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/whoami")]
|
#[get("/whoami")]
|
||||||
async fn validate(session: SessionAuth) -> Result<String, Custom<String>> {
|
async fn validate(session: CandidateAuth) -> Result<String, Custom<String>> {
|
||||||
let candidate: entity::candidate::Model = session.into();
|
let candidate: entity::candidate::Model = session.into();
|
||||||
Ok(candidate.application.to_string())
|
Ok(candidate.application.to_string())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue