35 lines
1.0 KiB
JavaScript
35 lines
1.0 KiB
JavaScript
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
const { PrismaClient } = require('@prisma/client');
|
|
const { scrypt, randomBytes } = require('crypto');
|
|
const { promisify } = require('util');
|
|
|
|
const prisma = new PrismaClient();
|
|
const scryptAsync = promisify(scrypt);
|
|
|
|
async function hashPassword(password) {
|
|
const salt = randomBytes(16).toString("hex");
|
|
const derivedKey = await scryptAsync(password, salt, 64);
|
|
return `${salt}:${derivedKey.toString("hex")}`;
|
|
}
|
|
|
|
async function main() {
|
|
const count = await prisma.globalSettings.count();
|
|
if (count === 0) {
|
|
console.log("Initializing default settings...");
|
|
const hashedPassword = await hashPassword("admin");
|
|
await prisma.globalSettings.create({
|
|
data: {
|
|
id: "default",
|
|
password: hashedPassword,
|
|
},
|
|
});
|
|
console.log("Done.");
|
|
} else {
|
|
console.log("Settings already exist.");
|
|
}
|
|
}
|
|
|
|
main()
|
|
.catch(e => console.error(e))
|
|
.finally(async () => await prisma.$disconnect());
|