.env login is now working well

This commit is contained in:
Priec
2026-05-17 15:31:37 +02:00
parent eaccfada73
commit 046f7c04c8
5 changed files with 48 additions and 3 deletions

1
Cargo.lock generated
View File

@@ -5060,6 +5060,7 @@ dependencies = [
"axum", "axum",
"axum-extra", "axum-extra",
"chrono", "chrono",
"dotenvy",
"fluent-templates", "fluent-templates",
"include_dir", "include_dir",
"insta", "insta",

View File

@@ -33,6 +33,7 @@ sea-orm = { version = "1.1", features = [
] } ] }
chrono = { version = "0.4" } chrono = { version = "0.4" }
time = { version = "0.3" } time = { version = "0.3" }
dotenvy = { version = "0.15" }
validator = { version = "0.20" } validator = { version = "0.20" }
uuid = { version = "1.6", features = ["v4"] } uuid = { version = "1.6", features = ["v4"] }
include_dir = { version = "0.7" } include_dir = { version = "0.7" }

View File

@@ -43,10 +43,16 @@ impl Hooks for App {
create_app::<Self, Migrator>(mode, environment, config).await create_app::<Self, Migrator>(mode, environment, config).await
} }
async fn load_config(environment: &Environment) -> Result<Config> {
dotenvy::dotenv().ok();
environment.load()
}
async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> { async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> {
Ok(vec![Box::new( Ok(vec![
initializers::view_engine::ViewEngineInitializer, Box::new(initializers::view_engine::ViewEngineInitializer),
)]) Box::new(initializers::admin_seeder::AdminSeeder),
])
} }
fn routes(_ctx: &AppContext) -> AppRoutes { fn routes(_ctx: &AppContext) -> AppRoutes {

View File

@@ -0,0 +1,36 @@
use async_trait::async_trait;
use loco_rs::prelude::*;
use crate::models::users::{self, RegisterParams};
pub struct AdminSeeder;
#[async_trait]
impl Initializer for AdminSeeder {
fn name(&self) -> String {
"admin-seeder".to_string()
}
async fn before_run(&self, ctx: &AppContext) -> Result<()> {
let email = std::env::var("ADMIN_EMAIL").unwrap_or_default();
let password = std::env::var("ADMIN_PASSWORD").unwrap_or_default();
let name = std::env::var("ADMIN_NAME").unwrap_or_else(|_| "Admin".to_string());
if email.is_empty() || password.is_empty() {
tracing::warn!("ADMIN_EMAIL / ADMIN_PASSWORD not set in .env; admin not seeded");
} else if users::Model::find_by_email(&ctx.db, &email).await.is_err() {
users::Model::create_with_password(
&ctx.db,
&RegisterParams {
email: email.clone(),
password,
name,
},
)
.await?;
tracing::info!(admin = %email, "admin user seeded");
}
Ok(())
}
}

View File

@@ -1 +1,2 @@
pub mod admin_seeder;
pub mod view_engine; pub mod view_engine;