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

32620
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -77,6 +77,7 @@
"@react-spring/web": "^9.7.3",
"@tanstack/react-query": "^5.28.6",
"@umami/redis-client": "^0.26.0",
"bcrypt": "^6.0.0",
"bcryptjs": "^2.4.3",
"chalk": "^4.1.1",
"chart.js": "^4.4.9",
@ -135,6 +136,8 @@
"@rollup/plugin-terser": "^0.4.4",
"@svgr/rollup": "^8.1.0",
"@svgr/webpack": "^8.1.0",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.3.0",
"@types/jest": "^29.5.14",
"@types/node": "^22.13.4",
"@types/react": "^19.0.8",
@ -157,13 +160,14 @@
"extract-react-intl-messages": "^4.1.1",
"husky": "^8.0.3",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"lint-staged": "^14.0.1",
"postcss": "^8.4.31",
"postcss-flexbugs-fixes": "^5.0.2",
"postcss-import": "^15.1.0",
"postcss-preset-env": "7.8.3",
"prettier": "^2.6.2",
"prompts": "2.4.2",
"prompts": "^2.4.2",
"rollup": "^3.28.0",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-delete": "^2.0.0",
@ -176,7 +180,7 @@
"stylelint-config-prettier": "^9.0.3",
"stylelint-config-recommended": "^14.0.0",
"tar": "^6.1.2",
"ts-jest": "^29.1.2",
"ts-jest": "^29.3.4",
"ts-node": "^10.9.1",
"typescript": "^5.5.3"
}

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();

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();

52
scripts/list-users.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();