mirror of
https://github.com/JetSprow/J-Board-Lite.git
synced 2026-05-01 01:14:10 +05:30
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import "dotenv/config";
|
|
import { PrismaClient } from "@prisma/client";
|
|
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
|
|
import bcrypt from "bcryptjs";
|
|
|
|
const adapter = new PrismaBetterSqlite3({
|
|
url: process.env.DATABASE_URL || "file:./storage/jboard.db",
|
|
});
|
|
const prisma = new PrismaClient({ adapter });
|
|
|
|
function envValue(key: string, fallback = "") {
|
|
return process.env[key]?.trim() || fallback;
|
|
}
|
|
|
|
async function main() {
|
|
const email = envValue("ADMIN_RESET_EMAIL", envValue("ADMIN_EMAIL", "admin@jboard.local")).toLowerCase();
|
|
const password = envValue("ADMIN_RESET_PASSWORD", envValue("ADMIN_PASSWORD"));
|
|
const name = envValue("ADMIN_RESET_NAME", envValue("ADMIN_NAME", "Admin"));
|
|
|
|
if (!email) {
|
|
throw new Error("ADMIN_RESET_EMAIL is required.");
|
|
}
|
|
|
|
if (!password || password.length < 6) {
|
|
throw new Error("ADMIN_RESET_PASSWORD must be at least 6 characters.");
|
|
}
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 12);
|
|
|
|
await prisma.user.upsert({
|
|
where: { email },
|
|
update: {
|
|
password: hashedPassword,
|
|
name,
|
|
role: "ADMIN",
|
|
status: "ACTIVE",
|
|
emailVerifiedAt: new Date(),
|
|
},
|
|
create: {
|
|
email,
|
|
password: hashedPassword,
|
|
name,
|
|
role: "ADMIN",
|
|
status: "ACTIVE",
|
|
emailVerifiedAt: new Date(),
|
|
},
|
|
});
|
|
|
|
console.log(`Admin reset completed: ${email}`);
|
|
}
|
|
|
|
main()
|
|
.catch((error) => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
})
|
|
.finally(() => prisma.$disconnect());
|