punimtag/viewer-frontend/scripts/create-admin-user.ts
Tanya de2144be2a feat: Add new scripts and update project structure for database management and user authentication
This commit introduces several new scripts for managing database operations, including user creation, permission grants, and data migrations. It also adds new documentation files to guide users through the setup and configuration processes. Additionally, the project structure is updated to enhance organization and maintainability, ensuring a smoother development experience for contributors. These changes support the ongoing transition to a web-based architecture and improve overall project functionality.
2026-01-06 13:53:24 -05:00

71 lines
2.1 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { PrismaClient as PrismaClientAuth } from '../node_modules/.prisma/client-auth';
import bcrypt from 'bcryptjs';
import * as dotenv from 'dotenv';
// Load environment variables
dotenv.config({ path: '.env' });
const prisma = new PrismaClientAuth({
datasourceUrl: process.env.DATABASE_URL_AUTH,
});
async function createAdminUser() {
try {
console.log('Creating admin user...\n');
// Hash the password
const passwordHash = await bcrypt.hash('admin', 10);
// Check if admin user already exists
const existingAdmin = await prisma.user.findUnique({
where: { email: 'admin@admin.com' },
});
if (existingAdmin) {
console.log('Admin user already exists. Updating password and admin status...');
await prisma.user.update({
where: { email: 'admin@admin.com' },
data: {
passwordHash,
isAdmin: true,
name: 'Admin',
hasWriteAccess: true, // Admins should have write access
},
});
console.log('✅ Admin user updated');
} else {
console.log('Creating new admin user...');
await prisma.user.create({
data: {
email: 'admin@admin.com',
name: 'Admin',
passwordHash,
isAdmin: true,
hasWriteAccess: true, // Admins should have write access
},
});
console.log('✅ Admin user created');
}
console.log('\n✅ Admin user setup complete!');
console.log('\nAdmin credentials:');
console.log(' Email: admin@admin.com');
console.log(' Password: admin');
console.log(' Role: Admin (can approve identifications)');
} catch (error: any) {
console.error('Error creating admin user:', error);
if (error.message.includes('permission denied')) {
console.error('\n⚠ Permission denied. Make sure:');
console.error(' 1. Database tables are created (run setup-auth-complete.sql)');
console.error(' 2. Database user has INSERT/UPDATE permissions on users table');
console.error(' 3. DATABASE_URL_AUTH is correctly configured in .env');
}
throw error;
} finally {
await prisma.$disconnect();
}
}
createAdminUser();