feat: add user management scripts and update dependencies

- Added `change-password.js` script for changing user passwords with bcrypt hashing.
- Added `create-admin.js` script to create an admin user if it doesn't already exist.
- Added `list-users.js` script to list all users in the database.
- Updated `package.json` to include `bcrypt` for password hashing and added testing libraries.
- Updated `ts-jest` version and ensured `prompts` is using a caret version for flexibility.
This commit is contained in:
mihf05 2025-05-23 02:44:29 +06:00
parent b04077db02
commit 46da6f3770
5 changed files with 32788 additions and 2 deletions

52
scripts/create-admin.js Normal file
View file

@ -0,0 +1,52 @@
/* eslint-disable no-console */
require('dotenv').config();
const { PrismaClient } = require('@prisma/client');
const bcrypt = require('bcrypt');
const { v4: uuidv4 } = require('uuid');
const prisma = new PrismaClient();
// Function to hash password with bcrypt
function hashPassword(password) {
const salt = bcrypt.genSaltSync(10);
return bcrypt.hashSync(password, salt);
}
async function createAdmin() {
const username = 'admin';
const password = 'umami';
try {
// Check if user already exists
const existingUser = await prisma.user.findUnique({
where: { username },
});
if (existingUser) {
console.log(`User ${username} already exists.`);
return;
}
// Create new admin user
const hash = hashPassword(password);
const user = await prisma.user.create({
data: {
id: uuidv4(),
username,
password: hash,
role: 'admin',
},
});
console.log(`Admin user created successfully with ID: ${user.id}`);
console.log(`Username: ${username}`);
console.log(`Password: ${password}`);
} catch (e) {
console.error('Error creating admin user:', e);
} finally {
await prisma.$disconnect();
}
}
createAdmin();