Add admin promotion options

This commit is contained in:
Randall Stillwell 2025-07-22 07:23:03 -05:00
parent 824c962f47
commit 04d7383025
2 changed files with 86 additions and 0 deletions

49
api/make-admin.js Normal file
View file

@ -0,0 +1,49 @@
const { Pool } = require('pg');
export default async function handler(req, res) {
try {
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false,
});
const client = await pool.connect();
// Simple promotion - just add user 1 to admin role
const result = await client.query(`
INSERT INTO user_roles (user_id, role_id)
SELECT 1, id FROM roles WHERE name = 'admin'
ON CONFLICT DO NOTHING
RETURNING *
`);
// Check current roles
const userRoles = await client.query(`
SELECT
u.username,
ARRAY_AGG(r.name) as roles
FROM users u
LEFT JOIN user_roles ur ON u.id = ur.user_id
LEFT JOIN roles r ON ur.role_id = r.id
WHERE u.id = 1
GROUP BY u.username
`);
client.release();
res.status(200).json({
success: true,
message: 'Admin promotion complete',
inserted: result.rows.length > 0,
user: userRoles.rows[0] || null,
timestamp: new Date().toISOString()
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message,
timestamp: new Date().toISOString()
});
}
}

View file

@ -0,0 +1,37 @@
-- Promote test user to admin role
-- Run this script directly against your Neon database
-- First, let's see the current user and roles
SELECT
u.id,
u.username,
u.email,
ARRAY_AGG(r.name) as current_roles
FROM users u
LEFT JOIN user_roles ur ON u.id = ur.user_id
LEFT JOIN roles r ON ur.role_id = r.id
WHERE u.username = 'testuser'
GROUP BY u.id, u.username, u.email;
-- Get the admin role ID
SELECT id as admin_role_id FROM roles WHERE name = 'admin';
-- Add the user to admin role (assuming user ID is 1 and admin role ID is 2)
-- You may need to adjust these IDs based on the output above
INSERT INTO user_roles (user_id, role_id)
SELECT 1, id FROM roles WHERE name = 'admin'
ON CONFLICT DO NOTHING;
-- Verify the promotion worked
SELECT
u.id,
u.username,
u.email,
u.first_name,
u.last_name,
ARRAY_AGG(DISTINCT r.name) as roles
FROM users u
LEFT JOIN user_roles ur ON u.id = ur.user_id
LEFT JOIN roles r ON ur.role_id = r.id
WHERE u.username = 'testuser'
GROUP BY u.id, u.username, u.email, u.first_name, u.last_name;