Download src/scripts/createAdmin.ts from nexusbert/zurri: direct link, hf CLI and curl.
- Browser
- Download file 5.4 kB
-
https://huggingface.co/spaces/nexusbert/zurri/resolve/main/src/scripts/createAdmin.ts
- Command line
-
hf download hf://spaces/nexusbert/zurri/src/scripts/createAdmin.ts
-
curl -L -o createAdmin.ts https://huggingface.co/spaces/nexusbert/zurri/resolve/main/src/scripts/createAdmin.ts
5.4 kB
| 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(); | |
| } | |