initial commit of gitara site
This commit is contained in:
36
src/initializers/admin_seeder.rs
Normal file
36
src/initializers/admin_seeder.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user