Files
kompress_eshop/src/initializers/admin_seeder.rs
Priec f3daa27ce7
Some checks failed
CI / Check Style (push) Has been cancelled
CI / Run Clippy (push) Has been cancelled
CI / Run Tests (push) Has been cancelled
account type is permanent and password registration is now working at checkout
2026-06-18 22:10:17 +02:00

60 lines
2.2 KiB
Rust

use async_trait::async_trait;
use chrono::offset::Local;
use loco_rs::prelude::*;
use loco_rs::hash;
use sea_orm::{ActiveModelTrait, IntoActiveModel, Set};
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");
return Ok(());
}
if let Ok(user) = users::Model::find_by_email(&ctx.db, &email).await {
// User exists — update password so .env is always the source of truth.
let hash = hash::hash_password(&password)
.map_err(|e| Error::Message(e.to_string()))?;
let mut am = user.into_active_model();
am.password = Set(hash);
am.name = Set(name);
// The admin signs in through the same /login flow as everyone else,
// which requires a verified email — keep the seeded admin verified.
am.email_verified_at = Set(Some(Local::now().into()));
am.update(&ctx.db).await?;
tracing::info!(admin = %email, "admin password synced from .env");
} else {
let user = users::Model::create_with_password(
&ctx.db,
&RegisterParams {
email: email.clone(),
password,
name,
account_type: None,
},
)
.await?;
// Auto-verify so the seeded admin can log in without an email round-trip.
let mut am = user.into_active_model();
am.email_verified_at = Set(Some(Local::now().into()));
am.update(&ctx.db).await?;
tracing::info!(admin = %email, "admin user seeded");
}
Ok(())
}
}