File size: 5,397 Bytes
48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b 48f4e54 8db1a4b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
const dotenv = require('dotenv');
const path = require('path');
const fs = require('fs');
const envPath = path.join(__dirname, '../../.env');
if (fs.existsSync(envPath)) {
dotenv.config({ path: envPath });
console.log('π Loaded .env file');
} else {
dotenv.config();
}
const ADMIN_EMAIL = process.env.ADMIN_EMAIL?.trim();
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD?.trim();
if (!ADMIN_EMAIL || !ADMIN_PASSWORD) {
console.error('β ADMIN_EMAIL and ADMIN_PASSWORD must be set in environment variables');
process.exit(1);
}
console.log('π§ Admin Email:', ADMIN_EMAIL);
console.log('π Password:', ADMIN_PASSWORD ? 'SET' : 'NOT SET');
console.log('π Database URL:', process.env.DATABASE_URL ? 'SET' : 'NOT SET');
import 'reflect-metadata';
import { AppDataSource } from '../config/database';
import { User } from '../entities/User';
import bcrypt from 'bcryptjs';
/**
* Initialize permanent admin user from environment variables
* This ensures the admin user always exists and maintains admin status
*/
export async function initializeAdmin(): Promise<void> {
const adminEmail = process.env.ADMIN_EMAIL?.trim();
const adminPassword = process.env.ADMIN_PASSWORD?.trim();
if (!adminEmail || !adminPassword) {
console.log('β οΈ ADMIN_EMAIL and ADMIN_PASSWORD not set - skipping admin initialization');
return;
}
try {
// Check if database is already initialized
if (!AppDataSource.isInitialized) {
await AppDataSource.initialize();
}
const userRepository = AppDataSource.getRepository(User);
let user = await userRepository.findOne({ where: { email: adminEmail.toLowerCase() } });
if (user) {
// User exists - ensure they are admin and update password if needed
const needsUpdate = !user.isAdmin || !user.isActive;
if (needsUpdate) {
user.isAdmin = true;
user.isActive = true;
console.log(`β
Updated user ${adminEmail} to admin status`);
}
// Always update password to match env (ensures it stays in sync)
const hashedPassword = await bcrypt.hash(adminPassword, 12);
user.password = hashedPassword;
await userRepository.save(user);
console.log(`β
Permanent admin user maintained: ${adminEmail}`);
} else {
// Create new admin user
const hashedPassword = await bcrypt.hash(adminPassword, 12);
user = userRepository.create({
email: adminEmail.toLowerCase(),
password: hashedPassword,
name: process.env.ADMIN_NAME || 'Admin User',
isAdmin: true,
isActive: true,
isCreator: false,
});
await userRepository.save(user);
console.log(`β
Permanent admin user created: ${adminEmail}`);
}
} catch (error) {
console.error('β Error initializing admin:', error);
if (error instanceof Error) {
console.error('Error message:', error.message);
}
// Don't throw - allow app to continue even if admin init fails
}
}
/**
* Standalone script to create/update admin user
*/
async function createAdmin() {
const adminEmail = ADMIN_EMAIL;
const adminPassword = ADMIN_PASSWORD;
if (!adminEmail || !adminPassword) {
console.error('β ADMIN_EMAIL and ADMIN_PASSWORD must be set in environment variables');
process.exit(1);
}
try {
console.log('\nπ§ Initializing database connection...');
await AppDataSource.initialize();
console.log('β
Database connected');
const userRepository = AppDataSource.getRepository(User);
console.log(`\nπ Looking for user: ${adminEmail}`);
let user = await userRepository.findOne({ where: { email: adminEmail.toLowerCase() } });
if (user) {
console.log('β
User found, updating to permanent admin...');
user.isAdmin = true;
user.isActive = true;
const hashedPassword = await bcrypt.hash(adminPassword, 12);
user.password = hashedPassword;
console.log('β
Password updated');
await userRepository.save(user);
console.log('β
User updated to permanent admin successfully!');
console.log(`\nπ§ Email: ${user.email}`);
console.log(`π€ Admin: ${user.isAdmin}`);
console.log(`π User ID: ${user.id}`);
} else {
console.log('β οΈ User not found, creating new permanent admin user...');
const hashedPassword = await bcrypt.hash(adminPassword, 12);
user = userRepository.create({
email: adminEmail.toLowerCase(),
password: hashedPassword,
name: process.env.ADMIN_NAME || 'Admin User',
isAdmin: true,
isActive: true,
isCreator: false,
});
await userRepository.save(user);
console.log('β
Permanent admin user created successfully!');
console.log(`\nπ§ Email: ${user.email}`);
console.log(`π€ Admin: ${user.isAdmin}`);
console.log(`π User ID: ${user.id}`);
}
await AppDataSource.destroy();
console.log('\nβ
Done!');
process.exit(0);
} catch (error) {
console.error('β Error:', error);
if (error instanceof Error) {
console.error('Error message:', error.message);
console.error('Error stack:', error.stack);
}
process.exit(1);
}
}
// Only run if this file is executed directly (not imported)
if (require.main === module) {
createAdmin();
}
|