generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42f31d8
commit 68ddf85
Showing
8 changed files
with
104 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const bcrypt = require('bcrypt'); | ||
const User = require('./user-model'); | ||
|
||
// Function to validate required fields in the request body | ||
function validateRequiredFields(req, requiredFields) { | ||
for (const field of requiredFields) { | ||
if (!(field in req.body)) { | ||
throw new Error(`Missing required field: ${field}`); | ||
} | ||
} | ||
} | ||
|
||
exports.addUserController = async (req, res) => { | ||
try { | ||
// Check if required fields are present in the request body | ||
validateRequiredFields(req, ['username', 'password']); | ||
|
||
// Encrypt the password before saving it | ||
const hashedPassword = await bcrypt.hash(req.body.password, 10); | ||
|
||
const newUser = new User({ | ||
name: req.body.name, | ||
lastName: req.body.lastName, | ||
email: req.body.email, | ||
username: req.body.username, | ||
password: hashedPassword, | ||
role: req.body.role, | ||
}); | ||
|
||
await newUser.save(); | ||
res.json(newUser); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
exports.getUsersController = async (req, res) => { | ||
try { | ||
const users = await User.find(); // Fetch all users, only return username field for security | ||
res.json(users); | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// userRouter.js | ||
const express = require('express'); | ||
const { | ||
addUserController, | ||
getUsersController, | ||
} = require('./user-controller.js'); | ||
|
||
const userRouter = express.Router(); | ||
|
||
// Define la ruta para el login y asocia el controlador | ||
userRouter.post('/adduser', addUserController); | ||
userRouter.get('/users', getUsersController); | ||
|
||
module.exports = userRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters