simple default user generation
This commit is contained in:
parent
626b01e56b
commit
e2abd2ca83
@ -1,8 +1,11 @@
|
|||||||
|
use argon2::{password_hash::SaltString, Algorithm, Argon2, Params, PasswordHasher, Version};
|
||||||
|
use rand_core::OsRng;
|
||||||
use rusqlite::{Connection, Result};
|
use rusqlite::{Connection, Result};
|
||||||
|
|
||||||
pub fn init_db() -> Result<Connection> {
|
pub fn init_db() -> Result<Connection> {
|
||||||
let conn = Connection::open("form_data.db")?;
|
let conn = Connection::open("form_data.db")?;
|
||||||
|
|
||||||
|
// Create tables
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"CREATE TABLE IF NOT EXISTS forms (
|
"CREATE TABLE IF NOT EXISTS forms (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
@ -32,5 +35,29 @@ pub fn init_db() -> Result<Connection> {
|
|||||||
[],
|
[],
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
// Check if the admin_users table is empty
|
||||||
|
let count: i64 = conn
|
||||||
|
.query_row("SELECT COUNT(*) FROM admin_users", [], |row| row.get(0))
|
||||||
|
.unwrap_or(0);
|
||||||
|
|
||||||
|
if count == 0 {
|
||||||
|
// Create a default admin user
|
||||||
|
let default_username = "admin";
|
||||||
|
let default_password = "admin123"; // This should be replaced with a secure method for real applications
|
||||||
|
|
||||||
|
// Hash the default password
|
||||||
|
let salt = SaltString::generate(&mut OsRng);
|
||||||
|
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, Params::default());
|
||||||
|
let password_hash = argon2
|
||||||
|
.hash_password(default_password.as_bytes(), &salt)
|
||||||
|
.unwrap()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO admin_users (username, password_hash) VALUES (?1, ?2)",
|
||||||
|
&[default_username, password_hash.as_str()],
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(conn)
|
Ok(conn)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user