fixing issues needed for hashing #17
+12
-14
@@ -1,4 +1,5 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
|
import bcrypt from 'bcryptjs'
|
||||||
import { pool } from '../../config/index.js'
|
import { pool } from '../../config/index.js'
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
@@ -20,14 +21,16 @@ router.get('/', async (req, res) => {
|
|||||||
* @param {string} role_name - The name of the role to assign to the user
|
* @param {string} role_name - The name of the role to assign to the user
|
||||||
*/
|
*/
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
const { name, email, password, role_name } = req.body
|
const { username, email, password, role_name } = req.body
|
||||||
|
|
||||||
const client = await pool.connect()
|
if (!password) {
|
||||||
|
return res.status(400).send('Password is required')
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await client.query('BEGIN')
|
// Hash the password using bcrypt
|
||||||
|
const hashedPassword = await bcrypt.hash(password, 10)
|
||||||
// Query for the UUID of the role by name
|
// Query for the UUID of the role by name
|
||||||
const roleResult = await client.query(
|
const roleResult = await pool.query(
|
||||||
'SELECT id FROM roles WHERE name = $1',
|
'SELECT id FROM roles WHERE name = $1',
|
||||||
[role_name]
|
[role_name]
|
||||||
)
|
)
|
||||||
@@ -37,20 +40,15 @@ router.post('/', async (req, res) => {
|
|||||||
const roleId = roleResult.rows[0].id
|
const roleId = roleResult.rows[0].id
|
||||||
|
|
||||||
// Insert the new user into the database
|
// Insert the new user into the database
|
||||||
const result = await client.query(
|
const result = await pool.query(
|
||||||
'INSERT INTO users (name, email, password, role_id, created_at, updated_at) VALUES ($1, $2, $3, $4, NOW(), NOW()) RETURNING *',
|
'INSERT INTO users (username, email, password, role_id, created_at, updated_at) VALUES ($1, $2, $3, $4, NOW(), NOW()) RETURNING *',
|
||||||
[name, email, password, roleId]
|
[username, email, hashedPassword, roleId]
|
||||||
)
|
)
|
||||||
|
|
||||||
// Return the newly created user
|
// Return the newly created user
|
||||||
res.status(201).json(result.rows[0])
|
res.status(201).json(result.rows[0])
|
||||||
|
|
||||||
await client.query('COMMIT')
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await client.query('ROLLBACK')
|
|
||||||
res.status(500).send(err)
|
res.status(500).send(err)
|
||||||
} finally {
|
|
||||||
client.release()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -95,7 +93,7 @@ router.put('/:id', async (req, res) => {
|
|||||||
|
|
||||||
if (password) {
|
if (password) {
|
||||||
queryText += ', password = $3'
|
queryText += ', password = $3'
|
||||||
values.push(password)
|
values.push(await bcrypt.hash(password, 10))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (roleId !== null) {
|
if (roleId !== null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user