wip: work on auth

This commit is contained in:
2024-07-14 07:58:39 +02:00
parent b029b4b975
commit c78e386645
7 changed files with 102 additions and 27 deletions

View File

@@ -183,16 +183,16 @@ impl AdminRepository for UserRepository {
}
}
pub fn register(registry: &mut AdminRegistry, db: DatabaseConnection) {
pub fn register(registry: &mut AdminRegistry, db: DatabaseConnection) -> UserRepository {
let app_key = registry.register_app("Auth");
let repo = UserRepository::new(db);
let model_config = AdminModelConfig {
app_key: app_key,
name: "User".to_owned(),
};
let model_result = registry.register_model(model_config, repo);
let model_result = registry.register_model(model_config, repo.clone());
match model_result {
Err(err) => panic!("{}", err),
_ => (),
_ => repo,
}
}

View File

@@ -6,7 +6,6 @@ use axum::{
Form, Router,
};
use axum_messages::{Message, Messages};
use rear::service::templates::Templates;
use serde::{Deserialize, Serialize};
use crate::models::{AuthSession, Credentials};
@@ -24,11 +23,12 @@ pub struct NextUrl {
next: Option<String>,
}
pub fn router() -> Router<Templates> {
pub fn routes<S: rear::admin::state::AdminState + Clone + Send + Sync + 'static>() -> Router<S>
where {
Router::new()
.route("/login", post(self::post::login))
.route("/login", get(self::get::login))
.route("/logout", get(self::get::logout))
.route("/login", get(self::get::login::<S>))
.route("/logout", post(self::post::logout))
}
mod post {
@@ -44,7 +44,7 @@ mod post {
Ok(None) => {
messages.error("Invalid credentials");
let mut login_url = "/login".to_string();
let mut login_url = "/admin/login".to_string();
if let Some(next) = creds.next {
login_url = format!("{}?next={}", login_url, next);
};
@@ -67,23 +67,6 @@ mod post {
}
.into_response()
}
}
mod get {
use super::*;
use axum::extract::State;
pub async fn login(
messages: Messages,
templates: State<Templates>,
Query(NextUrl { next }): Query<NextUrl>,
) -> impl IntoResponse {
let context = LoginTemplate {
messages: messages.into_iter().collect(),
next,
};
templates.render_html("login.html", context)
}
pub async fn logout(mut auth_session: AuthSession) -> impl IntoResponse {
match auth_session.logout().await {
@@ -93,4 +76,22 @@ mod get {
}
}
mod get {
use super::*;
use axum::extract::State;
pub async fn login<S: rear::admin::state::AdminState + Clone + Send + Sync + 'static>(
messages: Messages,
admin: State<S>,
Query(NextUrl { next }): Query<NextUrl>,
) -> impl IntoResponse {
let templates = admin.get_templates();
let context = LoginTemplate {
messages: messages.into_iter().collect(),
next,
};
templates.render_html("admin/login.html", context)
}
}
// this was taken from the axum_login examples and modified. Redirection might need reverse-routing support.