85 lines
2.8 KiB
JavaScript
85 lines
2.8 KiB
JavaScript
// Script to fix the existing Google-authenticated user to be admin
|
|
// This will update the first user (by creation date) to have administrator role
|
|
|
|
const { Pool } = require('pg');
|
|
|
|
// Using the postgres user since we know that password
|
|
const DATABASE_URL = process.env.DATABASE_URL ||
|
|
'postgresql://postgres:changeme@localhost:5432/vip_coordinator';
|
|
|
|
console.log('Connecting to database...');
|
|
|
|
const pool = new Pool({
|
|
connectionString: DATABASE_URL,
|
|
ssl: false
|
|
});
|
|
|
|
async function fixExistingUserToAdmin() {
|
|
try {
|
|
// 1. Show current users
|
|
console.log('\n📋 Current Google-authenticated users:');
|
|
console.log('=====================================');
|
|
const allUsers = await pool.query(`
|
|
SELECT email, name, role, created_at, is_active
|
|
FROM users
|
|
ORDER BY created_at ASC
|
|
`);
|
|
|
|
if (allUsers.rows.length === 0) {
|
|
console.log('❌ No users found in database!');
|
|
console.log('\nThe first user needs to log in with Google first.');
|
|
return;
|
|
}
|
|
|
|
console.log(`Found ${allUsers.rows.length} user(s):\n`);
|
|
allUsers.rows.forEach((user, index) => {
|
|
console.log(`User #${index + 1}:`);
|
|
console.log(` Email: ${user.email}`);
|
|
console.log(` Name: ${user.name}`);
|
|
console.log(` Current Role: ${user.role} ${user.role !== 'administrator' ? '❌' : '✅'}`);
|
|
console.log(` Is Active: ${user.is_active ? 'Yes' : 'No'}`);
|
|
console.log(` Created: ${user.created_at}`);
|
|
console.log('');
|
|
});
|
|
|
|
// 2. Update the first user to administrator
|
|
const firstUser = allUsers.rows[0];
|
|
if (firstUser.role === 'administrator') {
|
|
console.log('✅ First user is already an administrator!');
|
|
return;
|
|
}
|
|
|
|
console.log(`🔧 Updating ${firstUser.name} (${firstUser.email}) to administrator...`);
|
|
|
|
const updateResult = await pool.query(`
|
|
UPDATE users
|
|
SET
|
|
role = 'administrator',
|
|
is_active = true,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE email = $1
|
|
RETURNING email, name, role, is_active
|
|
`, [firstUser.email]);
|
|
|
|
if (updateResult.rows.length > 0) {
|
|
const updated = updateResult.rows[0];
|
|
console.log('\n✅ Successfully updated user!');
|
|
console.log(` Email: ${updated.email}`);
|
|
console.log(` Name: ${updated.name}`);
|
|
console.log(` New Role: ${updated.role} ✅`);
|
|
console.log(` Is Active: ${updated.is_active ? 'Yes' : 'No'}`);
|
|
console.log('\n🎉 This user can now log in and access the Admin dashboard!');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('\n❌ Error:', error.message);
|
|
if (error.code === '28P01') {
|
|
console.error('\nPassword authentication failed. Make sure Docker containers are running.');
|
|
}
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
// Run the fix
|
|
fixExistingUserToAdmin(); |