Create function to build auth layer
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
use axum_login::{AuthUser, AuthnBackend, UserId};
|
||||
use axum_login::{AuthManagerLayer, AuthUser, AuthnBackend, UserId};
|
||||
use diesel::prelude::*;
|
||||
use diesel_async::RunQueryDsl;
|
||||
use tower_sessions_redis_store::RedisStore;
|
||||
|
||||
use crate::models::user::{DbUser, UserCredentials};
|
||||
use crate::server::database::{DbConn, DbPool};
|
||||
use crate::server::{
|
||||
database::{DbConn, DbPool},
|
||||
key_val_store::KeyValPool,
|
||||
};
|
||||
use crate::util::error::{Contextualize, Error, Result};
|
||||
|
||||
pub type AuthLayer = AuthManagerLayer<AuthBackend, RedisStore<KeyValPool>>;
|
||||
pub type AuthSession = axum_login::AuthSession<AuthBackend>;
|
||||
|
||||
impl AuthUser for DbUser {
|
||||
type Id = i32;
|
||||
|
||||
@@ -89,3 +96,16 @@ pub async fn get_user_by_username(
|
||||
.optional()
|
||||
.err_context("Error fetching user from database by username")
|
||||
}
|
||||
|
||||
/// Create the authentication middleware layer
|
||||
pub fn build_auth_layer(db_pool: DbPool, key_val_pool: KeyValPool) -> AuthLayer {
|
||||
use axum_login::{AuthManagerLayerBuilder, tower_sessions::SessionManagerLayer};
|
||||
use tower_sessions_redis_store::RedisStore;
|
||||
|
||||
let auth_session_store = RedisStore::new(key_val_pool);
|
||||
let session_layer = SessionManagerLayer::new(auth_session_store);
|
||||
|
||||
let auth_backend = AuthBackend { db_pool };
|
||||
|
||||
AuthManagerLayerBuilder::new(auth_backend, session_layer).build()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user