|
|
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'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 { |
|
|
|
|
|
if (!AppDataSource.isInitialized) { |
|
|
await AppDataSource.initialize(); |
|
|
} |
|
|
|
|
|
const userRepository = AppDataSource.getRepository(User); |
|
|
|
|
|
let user = await userRepository.findOne({ where: { email: adminEmail.toLowerCase() } }); |
|
|
|
|
|
if (user) { |
|
|
|
|
|
const needsUpdate = !user.isAdmin || !user.isActive; |
|
|
|
|
|
if (needsUpdate) { |
|
|
user.isAdmin = true; |
|
|
user.isActive = true; |
|
|
console.log(`β
Updated user ${adminEmail} to admin status`); |
|
|
} |
|
|
|
|
|
|
|
|
const hashedPassword = await bcrypt.hash(adminPassword, 12); |
|
|
user.password = hashedPassword; |
|
|
|
|
|
await userRepository.save(user); |
|
|
console.log(`β
Permanent admin user maintained: ${adminEmail}`); |
|
|
} else { |
|
|
|
|
|
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); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (require.main === module) { |
|
|
createAdmin(); |
|
|
} |
|
|
|