mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-18 22:11:14 +00:00
refactor: session service
This commit is contained in:
parent
eb04334081
commit
b582d2e8e1
4 changed files with 13 additions and 12 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use entity::candidate::Model as Candidate;
|
||||
use portfolio_core::sea_orm::prelude::Uuid;
|
||||
use portfolio_core::services::candidate_service::CandidateService;
|
||||
use portfolio_core::services::session_service::SessionService;
|
||||
use rocket::http::Status;
|
||||
use rocket::outcome::Outcome;
|
||||
use rocket::request::{FromRequest, Request};
|
||||
|
|
@ -27,7 +27,7 @@ impl<'r> FromRequest<'r> for SessionAuth {
|
|||
Err(_) => return Outcome::Failure((Status::BadRequest, None)),
|
||||
};
|
||||
|
||||
let session = CandidateService::auth_user_session(conn, uuid).await;
|
||||
let session = SessionService::auth_user_session(conn, uuid).await;
|
||||
|
||||
match session {
|
||||
Ok(model) => Outcome::Success(SessionAuth(model)),
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use std::net::SocketAddr;
|
|||
|
||||
use guards::request::session_auth::SessionAuth;
|
||||
use portfolio_core::error::ServiceError;
|
||||
use portfolio_core::services::candidate_service::CandidateService;
|
||||
use portfolio_core::services::session_service::SessionService;
|
||||
use requests::{LoginRequest, RegisterRequest};
|
||||
use rocket::http::Status;
|
||||
use rocket::{Rocket, Build};
|
||||
|
|
@ -59,7 +59,7 @@ async fn login(conn: Connection<'_, Db>, login_form: Json<LoginRequest>, ip_addr
|
|||
let db = conn.into_inner();
|
||||
println!("{} {}", login_form.application_id, login_form.password);
|
||||
|
||||
let session_token = CandidateService::new_session(db,
|
||||
let session_token = SessionService::new_session(db,
|
||||
login_form.application_id,
|
||||
login_form.password.to_string(),
|
||||
ip_addr.ip().to_string()
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
pub mod candidate_service;
|
||||
pub mod session_service;
|
||||
|
|
@ -5,9 +5,10 @@ use sea_orm::{DatabaseConnection, prelude::Uuid, ModelTrait};
|
|||
|
||||
use crate::{crypto::{self}, Query, error::{ServiceError, USER_NOT_FOUND_ERROR, INVALID_CREDENTIALS_ERROR, DB_ERROR, USER_NOT_FOUND_BY_JWT_ID, USER_NOT_FOUND_BY_SESSION_ID, EXPIRED_SESSION_ERROR}, Mutation};
|
||||
|
||||
pub struct CandidateService;
|
||||
// TODO: generics
|
||||
pub struct SessionService;
|
||||
|
||||
impl CandidateService {
|
||||
impl SessionService {
|
||||
/// Delete n old sessions for user
|
||||
async fn delete_old_sessions(db: &DatabaseConnection, user_id: i32, keep_n_recent: usize) -> Result<(), ServiceError> {
|
||||
let mut sessions = Query::find_sessions_by_user_id(db, user_id).await.unwrap();
|
||||
|
|
@ -52,7 +53,7 @@ impl CandidateService {
|
|||
};
|
||||
|
||||
// delete old sessions
|
||||
CandidateService::delete_old_sessions(db, candidate.application, 3).await.ok(); // TODO move to dotenv
|
||||
SessionService::delete_old_sessions(db, candidate.application, 3).await.ok(); // TODO move to dotenv
|
||||
|
||||
Ok(session.id.to_string())
|
||||
}
|
||||
|
|
@ -95,7 +96,7 @@ mod tests {
|
|||
use entity::candidate;
|
||||
use sea_orm::{DbConn, Database, sea_query::TableCreateStatement, DbBackend, Schema, ConnectionTrait, prelude::Uuid};
|
||||
|
||||
use crate::{crypto, Mutation, services::candidate_service::CandidateService};
|
||||
use crate::{crypto, Mutation, services::session_service::SessionService};
|
||||
|
||||
#[cfg(test)]
|
||||
async fn get_memory_sqlite_connection() -> DbConn {
|
||||
|
|
@ -132,7 +133,7 @@ mod tests {
|
|||
Mutation::create_candidate(&db, 5555555, &"Tajny_kod".to_string(), "".to_string()).await.unwrap();
|
||||
|
||||
// correct password
|
||||
let session = CandidateService::new_session(
|
||||
let session = SessionService::new_session(
|
||||
db,
|
||||
5555555,
|
||||
"Tajny_kod".to_string(),
|
||||
|
|
@ -142,7 +143,7 @@ mod tests {
|
|||
// println!("{}", session.err().unwrap().1);
|
||||
|
||||
assert!(
|
||||
CandidateService::auth_user_session(db, Uuid::parse_str(&session).unwrap())
|
||||
SessionService::auth_user_session(db, Uuid::parse_str(&session).unwrap())
|
||||
.await
|
||||
.is_ok()
|
||||
);
|
||||
|
|
@ -156,7 +157,7 @@ mod tests {
|
|||
|
||||
// incorrect password
|
||||
assert!(
|
||||
CandidateService::new_session(db, candidate_form.application, "Spatny_kod".to_string(), "127.0.0.1".to_string()).await.is_err()
|
||||
SessionService::new_session(db, candidate_form.application, "Spatny_kod".to_string(), "127.0.0.1".to_string()).await.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue