diff --git a/src/auth/mod.rs b/src/auth/mod.rs index e8fa73de..e8217bc8 100644 --- a/src/auth/mod.rs +++ b/src/auth/mod.rs @@ -1,5 +1,4 @@ pub mod routes; pub mod utils; -pub mod redirect; pub use routes::init; diff --git a/src/main.rs b/src/main.rs index 74fd6733..026cb767 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,6 @@ use actix_web::{web, App, HttpServer}; use dotenv::dotenv; use sqlx::postgres::PgPool; -use crate::auth::redirect; use crate::auth::utils::generate_salt_and_hash_plain_password; use crate::models::User; use crate::postgres_session_store::SqlxPostgresqlSessionStore; @@ -19,9 +18,9 @@ mod auth; mod calendar; mod endpoints; mod models; +mod middleware; mod postgres_session_store; -mod load_current_user_from_db; pub enum Command { Migrate, @@ -120,8 +119,8 @@ async fn main() -> anyhow::Result<()> { .configure(auth::init) .configure(calendar::init) .configure(endpoints::init) - .wrap(redirect::CheckLogin) - .wrap(load_current_user_from_db::LoadUser) + .wrap(middleware::RedirectToLogin) + .wrap(middleware::LoadCurrentUser) .wrap( IdentityMiddleware::builder() .visit_deadline(Some(Duration::from_secs(300))) diff --git a/src/load_current_user_from_db.rs b/src/middleware/load_current_user_from_db.rs similarity index 84% rename from src/load_current_user_from_db.rs rename to src/middleware/load_current_user_from_db.rs index d11fb7be..0aaa44d6 100644 --- a/src/load_current_user_from_db.rs +++ b/src/middleware/load_current_user_from_db.rs @@ -13,30 +13,30 @@ use sqlx::PgPool; use crate::models::User; -pub struct LoadUser; +pub struct LoadCurrentUser; -impl Transform for LoadUser +impl Transform for LoadCurrentUser where S: Service, Error = Error> + 'static, { type Response = ServiceResponse; type Error = Error; type InitError = (); - type Transform = LoadUserMiddleware; + type Transform = LoadCurrentUserMiddleware; type Future = Ready>; fn new_transform(&self, service: S) -> Self::Future { - ready(Ok(LoadUserMiddleware { + ready(Ok(LoadCurrentUserMiddleware { service: Rc::new(service), })) } } -pub struct LoadUserMiddleware { +pub struct LoadCurrentUserMiddleware { service: Rc, } -impl Service for LoadUserMiddleware +impl Service for LoadCurrentUserMiddleware where S: Service, Error = Error> + 'static, { diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs new file mode 100644 index 00000000..1a8c931d --- /dev/null +++ b/src/middleware/mod.rs @@ -0,0 +1,5 @@ +mod redirect_to_login; +mod load_current_user_from_db; + +pub use redirect_to_login::RedirectToLogin; +pub use load_current_user_from_db::LoadCurrentUser; diff --git a/src/auth/redirect.rs b/src/middleware/redirect_to_login.rs similarity index 87% rename from src/auth/redirect.rs rename to src/middleware/redirect_to_login.rs index 8e5d6a13..db4efe48 100644 --- a/src/auth/redirect.rs +++ b/src/middleware/redirect_to_login.rs @@ -8,9 +8,9 @@ use actix_web::{ }; use futures_util::future::LocalBoxFuture; -pub struct CheckLogin; +pub struct RedirectToLogin; -impl Transform for CheckLogin +impl Transform for RedirectToLogin where S: Service, Error = Error>, S::Future: 'static, @@ -19,18 +19,18 @@ where type Response = ServiceResponse>; type Error = Error; type InitError = (); - type Transform = CheckLoginMiddleware; + type Transform = RedirectToLoginMiddleware; type Future = Ready>; fn new_transform(&self, service: S) -> Self::Future { - ready(Ok(CheckLoginMiddleware { service })) + ready(Ok(RedirectToLoginMiddleware { service })) } } -pub struct CheckLoginMiddleware { +pub struct RedirectToLoginMiddleware { service: S, } -impl Service for CheckLoginMiddleware +impl Service for RedirectToLoginMiddleware where S: Service, Error = Error>, S::Future: 'static,