feat: make first registered user an admin
This commit is contained in:
+5
-1
@@ -23,12 +23,16 @@ router.post('/register', async (req, res) => {
|
||||
const checkUser = await pgPool.query('SELECT id FROM users WHERE email = $1', [email]);
|
||||
if (checkUser.rows.length > 0) return res.status(400).json({ error: 'User already exists' });
|
||||
|
||||
const countQuery = await pgPool.query('SELECT COUNT(*) FROM users');
|
||||
const userCount = parseInt(countQuery.rows[0].count, 10);
|
||||
const assignedRole = userCount === 0 ? 'admin' : 'free_trial';
|
||||
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
const hash = await bcrypt.hash(password, salt);
|
||||
|
||||
const newUser = await pgPool.query(
|
||||
'INSERT INTO users (email, password_hash, role) VALUES ($1, $2, $3) RETURNING id, email, role',
|
||||
[email, hash, 'free_trial']
|
||||
[email, hash, assignedRole]
|
||||
);
|
||||
|
||||
const token = jwt.sign({ id: newUser.rows[0].id, email: newUser.rows[0].email, role: newUser.rows[0].role }, JWT_SECRET, { expiresIn: '14d' });
|
||||
|
||||
Reference in New Issue
Block a user