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

View file

@ -0,0 +1,58 @@
/* eslint-disable no-console */
require('dotenv').config();
const { PrismaClient } = require('@prisma/client');
const bcrypt = require('bcrypt');
const prompts = require('prompts');
const prisma = new PrismaClient();
// Function to hash password with bcrypt (replaces imported hashPassword)
function hashPassword(password) {
const salt = bcrypt.genSaltSync(10);
return bcrypt.hashSync(password, salt);
}
async function changePassword() {
console.log('Change user password');
console.log('-------------------');
const { username } = await prompts({
type: 'text',
name: 'username',
message: 'Username:',
initial: 'admin',
});
const { password } = await prompts({
type: 'password',
name: 'password',
message: 'New password:',
validate: value => (value.length >= 8 ? true : 'Password must be at least 8 characters'),
});
try {
const user = await prisma.user.findUnique({
where: { username },
});
if (!user) {
console.log(`User not found: ${username}`);
return;
}
const hash = hashPassword(password);
await prisma.user.update({
where: { id: user.id },
data: { password: hash },
});
console.log(`Password changed successfully for ${username}.`);
} catch (e) {
console.error('Error changing password:', e);
} finally {
await prisma.$disconnect();
}
}
changePassword();