feat: Implement initial setup service for admin user creation

- Added `SetupService` to handle the generation and validation of setup tokens.
- Integrated setup token generation during application startup if no admin users exist.
- Created API endpoints for checking setup status and completing the initial setup.
- Updated `AuthService` to include functionality for creating the initial admin user.
- Enhanced error handling for setup and authentication processes.
- Added frontend components for login and protected routes.
- Implemented Zustand store for managing authentication state.
- Updated Vite configuration to check setup status and serve the setup page if required.
- Documented the initial setup process in `setup.md`.
This commit is contained in:
GW_MC
2026-03-03 07:46:49 +00:00
parent 520ab74391
commit 4eddf7e094
24 changed files with 2214 additions and 99 deletions

View File

@@ -37,10 +37,42 @@ pub async fn start(settings: Settings) -> Result<(), Box<dyn std::error::Error>>
info!("Database migrations complete");
// Create application state
let app_state = api::routes::AppState {
db: db.clone(),
settings: settings.clone(),
};
let app_state = api::routes::AppState::new(db.clone(), settings.clone());
// Generate setup token if no admin exists
match app_state.setup_service.has_admin_users().await {
Ok(false) => {
info!("=================================================================");
info!(" INITIAL SETUP REQUIRED ");
info!("=================================================================");
info!("No admin user found. A setup token has been generated.");
info!("");
match app_state.setup_service.generate_setup_token().await {
Ok(token_info) => {
info!("SETUP TOKEN: {}", token_info.plain_token);
info!("EXPIRES AT: {}", token_info.expires_at);
info!("");
info!("Use this token to create the first admin account at:");
info!(" http://{}:{}/setup", settings.server.bind_address, settings.server.port);
info!("");
info!("WARNING: This token is single-use and will expire in 24 hours.");
info!(" It is displayed only once in these logs.");
}
Err(e) => {
error!("Failed to generate setup token: {}", e);
}
}
info!("=================================================================");
}
Ok(true) => {
info!("Admin user exists - initial setup already completed");
}
Err(e) => {
error!("Failed to check admin status: {}", e);
}
}
// Create router
let app = api::routes::create_router(app_state);