Compare commits

...

2 Commits

Author SHA1 Message Date
joseph.nelson4456 a04ff9a898 Merge pull request 'fixing issues needed for hashing' (#17) from bug/fix-users-api-to-hash-password into main
Reviewed-on: #17
2026-06-10 23:35:11 -07:00
joseph.nelson4456 5585fb2557 fixing issues needed for hashing
Test Workflow / test-and-lint (pull_request) Successful in 48s
Build and Push Image / build-and-push (pull_request) Successful in 1m40s
2026-06-10 23:30:43 -07:00
+12 -14
View File
@@ -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) {