48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import { PrismaClient as MainPrismaClient } from '.prisma/main-client';
|
||
import bcrypt from 'bcryptjs';
|
||
|
||
const prisma = new MainPrismaClient();
|
||
|
||
async function main() {
|
||
const username = process.argv[2] || 'admin';
|
||
const password = process.argv[3] || process.env.ADMIN_INITIAL_PASSWORD;
|
||
|
||
if (!password) {
|
||
throw new Error('请通过命令参数或 ADMIN_INITIAL_PASSWORD 提供管理员初始密码');
|
||
}
|
||
|
||
// 检查是否已存在
|
||
const existing = await prisma.admin.findUnique({
|
||
where: { username },
|
||
});
|
||
|
||
if (existing) {
|
||
console.log(`管理员 "${username}" 已存在`);
|
||
return;
|
||
}
|
||
|
||
const passwordHash = await bcrypt.hash(password, 10);
|
||
|
||
await prisma.admin.create({
|
||
data: {
|
||
username,
|
||
passwordHash,
|
||
isSuperAdmin: true,
|
||
},
|
||
});
|
||
|
||
console.log(`管理员创建成功!`);
|
||
console.log(`用户名: ${username}`);
|
||
console.log(`密码: 已按输入值写入哈希,不在日志中输出明文`);
|
||
console.log(`\n请使用提供的初始密码登录后立即修改密码!`);
|
||
}
|
||
|
||
main()
|
||
.catch((e) => {
|
||
console.error('创建管理员失败:', e);
|
||
process.exit(1);
|
||
})
|
||
.finally(async () => {
|
||
await prisma.$disconnect();
|
||
});
|