.env login is now working well
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -5060,6 +5060,7 @@ dependencies = [
|
|||||||
"axum",
|
"axum",
|
||||||
"axum-extra",
|
"axum-extra",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
"dotenvy",
|
||||||
"fluent-templates",
|
"fluent-templates",
|
||||||
"include_dir",
|
"include_dir",
|
||||||
"insta",
|
"insta",
|
||||||
|
|||||||
@@ -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" }
|
||||||
|
|||||||
12
src/app.rs
12
src/app.rs
@@ -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 {
|
||||||
|
|||||||
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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1,2 @@
|
|||||||
|
pub mod admin_seeder;
|
||||||
pub mod view_engine;
|
pub mod view_engine;
|
||||||
|
|||||||
Reference in New Issue
Block a user