feat: implement user management features including creation, updating, and deletion of users

- Added user management routes and logic in `+page.server.ts` for creating, updating, resetting passwords, and deleting users.
- Created a user management interface in `+page.svelte` with dialogs for user actions.
- Integrated password validation and hashing using `@node-rs/argon2`.
- Updated database schema to include a `user` table with necessary fields.
- Seeded a default admin user during database migration if no users exist.
- Added necessary dependencies in `package.json`.
This commit is contained in:
Flavio Fois
2026-02-15 13:03:58 +01:00
parent 1fd15a737b
commit a89b18d434
138 changed files with 4367 additions and 383 deletions

View File

@@ -1,5 +1,7 @@
import { readFileSync } from "fs";
import { join } from "path";
import { randomUUID } from "crypto";
import { hash } from "@node-rs/argon2";
import { getPool } from "./connection";
export async function runMigrations(): Promise<void> {
@@ -33,5 +35,23 @@ export async function runMigrations(): Promise<void> {
}
}
// Seed default admin user if user table is empty
const [rows] = await pool.execute("SELECT COUNT(*) as count FROM `user`");
const userCount = (rows as Array<{ count: number }>)[0].count;
if (userCount === 0) {
const passwordHash = await hash("admin", {
memoryCost: 19456,
timeCost: 2,
outputLen: 32,
parallelism: 1
});
const id = randomUUID();
await pool.execute(
"INSERT INTO `user` (`id`, `username`, `password_hash`, `role`) VALUES (?, ?, ?, ?)",
[id, "admin", passwordHash, "admin"]
);
console.log("Default admin user created (username: admin, password: admin)");
}
console.log("Database migrations completed");
}

View File

@@ -36,3 +36,19 @@ CREATE TABLE IF NOT EXISTS `rate_limit_hwid` (
`window_start` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`count` INT UNSIGNED NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `user` (
`id` VARCHAR(255) PRIMARY KEY,
`username` VARCHAR(255) NOT NULL UNIQUE,
`password_hash` VARCHAR(255) NOT NULL,
`role` ENUM('admin', 'user') NOT NULL DEFAULT 'user',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`displayname` VARCHAR(255) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `session` (
`id` VARCHAR(255) PRIMARY KEY,
`user_id` VARCHAR(255) NOT NULL,
`expires_at` DATETIME NOT NULL,
CONSTRAINT `fk_session_user` FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;