Added very basic admin user creation

This commit is contained in:
Jef Roosens 2021-08-21 18:05:16 +02:00
parent ac762a3c31
commit 0d4d96d761
Signed by: Jef Roosens
GPG key ID: 955C0660072F691F
5 changed files with 104 additions and 25 deletions

View file

@ -1,5 +1,5 @@
use crate::RbDbConn;
use rb::auth::{verify_user, JWTResponse, generate_jwt_token};
use rb::auth::{generate_jwt_token, verify_user, JWTResponse};
use rocket::serde::json::Json;
use serde::Deserialize;

View file

@ -28,6 +28,25 @@ async fn run_db_migrations(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocke
.await
}
async fn create_admin_user(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>> {
// In debug mode, the admin user is just a test user
let (admin_user, admin_password): (String, String);
// if rocket.config().profile == "debug" {
admin_user = String::from("test");
admin_password = String::from("test");
// }else{
// admin_user = std::env::var("ADMIN_USER").expect("no admin user provided");
// admin_password = std::env::var("ADMIN_PASSWORD").expect("no admin password provided");
// }
let conn = RbDbConn::get_one(&rocket)
.await
.expect("database connection");
conn.run(move |c| rb::auth::create_admin_user(c, &admin_user, &admin_password).expect("failed to create admin user")).await;
Ok(rocket)
}
#[launch]
fn rocket() -> _ {
rocket::build()
@ -36,5 +55,9 @@ fn rocket() -> _ {
"Run database migrations",
run_db_migrations,
))
.attach(AdHoc::try_on_ignite(
"Create admin user",
create_admin_user
))
.mount("/auth", auth::routes())
}