FlowForge/backend/create-admin.js

44 lines
1.3 KiB
JavaScript

const bcrypt = require('bcrypt');
const { v4: uuidv4 } = require('uuid');
const { db } = require('./src/config/db');
async function createAdminUser() {
try {
// Generate a new hash for the password
const password = 'FlowForge123!';
const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(password, salt);
console.log('Generated hash:', hash);
// Generate a UUID for the user
const userId = uuidv4();
// Delete any existing admin user
await db('users').where({ email: 'admin@flowforge.test' }).del();
// Insert the new admin user
const [user] = await db('users').insert({
id: userId,
email: 'admin@flowforge.test',
password: hash,
created_at: new Date(),
updated_at: new Date()
}).returning(['id', 'email', 'created_at']);
console.log('Admin user created successfully:', user);
// Verify the password works
const dbUser = await db('users').where({ email: 'admin@flowforge.test' }).first();
const isValid = await bcrypt.compare(password, dbUser.password);
console.log('Password validation:', isValid);
process.exit(0);
} catch (error) {
console.error('Error creating admin user:', error);
process.exit(1);
}
}
createAdminUser();