Add simple test user promotion endpoint
🔧 Simple Admin Promotion:
- Create api/promote-testuser.js endpoint
- Promotes user ID 1 (testuser) to admin role
- No authentication required (for initial setup)
- Uses working JavaScript + req/res pattern
- Simpler than complex promote-user endpoint
This commit is contained in:
parent
5dd7de86ad
commit
824c962f47
1 changed files with 108 additions and 0 deletions
108
api/promote-testuser.js
Normal file
108
api/promote-testuser.js
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
const { Pool } = require('pg');
|
||||||
|
|
||||||
|
const pool = new Pool({
|
||||||
|
connectionString: process.env.DATABASE_URL,
|
||||||
|
ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default async function handler(req, res) {
|
||||||
|
if (req.method !== 'POST') {
|
||||||
|
return res.status(405).json({ error: 'Method not allowed' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = await pool.connect();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get the test user (ID 1)
|
||||||
|
const userResult = await client.query(
|
||||||
|
'SELECT id, username, email FROM users WHERE id = $1',
|
||||||
|
[1]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (userResult.rows.length === 0) {
|
||||||
|
return res.status(404).json({
|
||||||
|
error: 'Test user not found'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = userResult.rows[0];
|
||||||
|
|
||||||
|
// Check if user is already an admin
|
||||||
|
const adminCheckQuery = `
|
||||||
|
SELECT ur.user_id
|
||||||
|
FROM user_roles ur
|
||||||
|
JOIN roles r ON ur.role_id = r.id
|
||||||
|
WHERE ur.user_id = $1 AND r.name = 'admin'
|
||||||
|
`;
|
||||||
|
|
||||||
|
const adminCheck = await client.query(adminCheckQuery, [user.id]);
|
||||||
|
|
||||||
|
if (adminCheck.rows.length > 0) {
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: 'User is already an admin',
|
||||||
|
user: {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get admin role ID
|
||||||
|
const roleResult = await client.query(
|
||||||
|
'SELECT id FROM roles WHERE name = $1',
|
||||||
|
['admin']
|
||||||
|
);
|
||||||
|
|
||||||
|
if (roleResult.rows.length === 0) {
|
||||||
|
return res.status(500).json({
|
||||||
|
error: 'Admin role not found in database'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const adminRoleId = roleResult.rows[0].id;
|
||||||
|
|
||||||
|
// Add user to admin role
|
||||||
|
await client.query(
|
||||||
|
'INSERT INTO user_roles (user_id, role_id) VALUES ($1, $2)',
|
||||||
|
[user.id, adminRoleId]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get updated user info with roles
|
||||||
|
const updatedUserQuery = `
|
||||||
|
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.id = $1
|
||||||
|
GROUP BY u.id, u.username, u.email, u.first_name, u.last_name
|
||||||
|
`;
|
||||||
|
|
||||||
|
const updatedUser = await client.query(updatedUserQuery, [user.id]);
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: 'Test user successfully promoted to admin',
|
||||||
|
user: {
|
||||||
|
id: updatedUser.rows[0].id,
|
||||||
|
username: updatedUser.rows[0].username,
|
||||||
|
email: updatedUser.rows[0].email,
|
||||||
|
firstName: updatedUser.rows[0].first_name,
|
||||||
|
lastName: updatedUser.rows[0].last_name,
|
||||||
|
roles: updatedUser.rows[0].roles || []
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Promote test user error:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Internal server error',
|
||||||
|
details: error.message
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
client.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue