Files
euchre_camp/scripts/create-admin-better-auth.js
T

65 lines
1.9 KiB
JavaScript

const { PrismaClient } = require('@prisma/client');
const bcrypt = require('bcrypt');
const { PrismaPg } = require('@prisma/adapter-pg');
// Load environment variables from .env file
require('dotenv').config();
// Create PrismaClient with PostgreSQL adapter (matching src/lib/prisma.ts)
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
const prisma = new PrismaClient({ adapter });
async function main() {
try {
// Hash password using bcrypt (Better Auth compatible)
const password = 'admin';
const salt = await bcrypt.genSalt(12);
const passwordHash = await bcrypt.hash(password, salt);
// Generate cuid-like ID
const cuid = `clx_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
// Check if user exists
const existing = await prisma.user.findUnique({
where: { email: 'david@dhg.lol' }
});
if (existing) {
await prisma.user.delete({ where: { id: existing.id } });
console.log('🗑️ Deleted existing user');
}
// Create admin user with Better Auth compatible password hash
const user = await prisma.user.create({
data: {
id: cuid,
email: 'david@dhg.lol',
emailVerified: true,
role: 'club_admin',
name: 'David Admin',
accounts: {
create: {
id: `${cuid}_acc`,
accountId: `${cuid}_acc`,
providerId: 'credential',
password: passwordHash, // bcrypt hash
}
}
}
});
console.log('✅ Admin user created successfully!');
console.log(' Email:', user.email);
console.log(' Role:', user.role);
console.log(' Password: admin');
console.log(' Name:', user.name);
} catch (error) {
console.error('❌ Error:', error.message);
} finally {
await prisma.$disconnect();
}
}
main();