diff --git a/api/make-admin.js b/api/make-admin.js new file mode 100644 index 0000000..af07804 --- /dev/null +++ b/api/make-admin.js @@ -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() + }); + } +} \ No newline at end of file diff --git a/scripts/promote-user-to-admin.sql b/scripts/promote-user-to-admin.sql new file mode 100644 index 0000000..a85d234 --- /dev/null +++ b/scripts/promote-user-to-admin.sql @@ -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; \ No newline at end of file