mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
52 lines
1.2 KiB
JavaScript
52 lines
1.2 KiB
JavaScript
/* eslint-disable no-console */
|
|
require('dotenv').config();
|
|
const { PrismaClient } = require('@prisma/client');
|
|
const bcrypt = require('bcryptjs');
|
|
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: [REDACTED]');
|
|
} catch (e) {
|
|
console.error('Error creating admin user:', e);
|
|
} finally {
|
|
await prisma.$disconnect();
|
|
}
|
|
}
|
|
|
|
createAdmin();
|