Skip to content

Commit 2989241

Browse files
committed
add option to update data of user (.put)
1 parent b2afe5a commit 2989241

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

controllers/userController.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const bcrypt = require('bcrypt')
55
const router = require('express').Router()
66
const User = require('../models/user')
7+
const { userExtractor } = require('../utils/middleware')
78

89
// Post a new user to the database
910
router.post('/', async (request, response) => {
@@ -33,6 +34,44 @@ router.post('/', async (request, response) => {
3334
response.status(201).json(savedUser)
3435
})
3536

37+
// update a user data
38+
router.put('/:id', userExtractor, async (request, response) => {
39+
// const userdata = await User.findById(request.params.id)
40+
const { username, name, password, email } = request.body
41+
42+
const user = request.user
43+
44+
// only the user can update their own data
45+
if (!user || request.params.id.toString() !== user.id.toString()) {
46+
return response.status(401).json({ error: 'operation not permitted' })
47+
}
48+
49+
// Check that the password is at least 3 characters long
50+
if (!password || password.length < 3) {
51+
return response.status(400).json({
52+
error: '`password` is shorter than the minimum allowed length (3)',
53+
})
54+
}
55+
56+
// saltRounds is the number of times the password is hashed
57+
const saltRounds = 10
58+
const passwordHash = await bcrypt.hash(password, saltRounds)
59+
60+
const updatedUser = await User.findByIdAndUpdate(
61+
request.params.id,
62+
{
63+
username,
64+
name,
65+
email,
66+
passwordHash,
67+
// Add new fields to be updated here
68+
},
69+
{ new: true }
70+
)
71+
72+
response.json(updatedUser)
73+
})
74+
3675
// Get all users
3776
router.get('/', async (request, response) => {
3877
// Populate the prototypes field with the title of the prototype

requests/login.rest

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
###
2+
# Create a new user
23
POST http://localhost:3003/api/users
34
Content-Type: application/json
45

@@ -11,6 +12,7 @@ Content-Type: application/json
1112

1213

1314
###
15+
# Login with the user and take the token
1416
POST http://localhost:3003/api/login
1517
Content-Type: application/json
1618

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
###
2+
# Get all prototypes with success (not need token)
23
GET http://localhost:3003/api/prototypes
3-
Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJvb3QiLCJpZCI6IjY0ZmVkMjkyM2EyMDYzZGIyYzAzYmJjNyIsImlhdCI6MTY5NDQyODYzNiwiZXhwIjoxNjk0NDMyMjM2fQ.Z5GHfVEJgYwC_TVS6eBmWYrG6j5etoNpqg4TUkpEfRU
44

55
###
6+
# Login with success to get token
67
POST http://localhost:3003/api/login
78
Content-Type: application/json
89

910
{
1011
"username": "root",
11-
"password": "test"
12+
"password": "test123"
1213
}
1314

1415
###
16+
# Create a prototype with success (token provided)
1517
POST http://localhost:3003/api/prototypes
1618
Content-Type: application/json
1719
Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJvb3QiLCJpZCI6IjY0ZmY5NjUzNmU1NjFkOTg2MDkyYWY1ZiIsImlhdCI6MTY5NDQ3MjA2MX0.pcBkU-C2mopwEoBsqnsv6zl5bOLBCeJmfAZQ3_HHABw
@@ -22,6 +24,17 @@ Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJvb
2224
}
2325

2426
###
27+
# Update a prototype
28+
PUT http://localhost:3003/api/prototypes/64ff96536e561d986092af5f
29+
Content-Type: application/json
30+
Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJvb3QiLCJpZCI6IjY0ZmY5NjUzNmU1NjFkOTg2MDkyYWY1ZiIsImlhdCI6MTY5NDYxMzU5Nn0.RQ8rtgOAET2zPP49M77XPXtm750wiaGFpC4raHbh2nQ
31+
32+
{
33+
"title": "El nuevo ocaso 2",
34+
}
35+
36+
###
37+
# Update a prototype with token
2538
POST http://localhost:3003/api/prototypes
2639
Content-Type: application/json
2740
Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJvb3QiLCJpZCI6IjY0ZmY5NjUzNmU1NjFkOTg2MDkyYWY1ZiIsImlhdCI6MTY5NDQ3MjA2MX0.pcBkU-C2mopwEoBsqnsv6zl5bOLBCeJmfAZQ3_HHABw
@@ -32,19 +45,21 @@ Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJvb
3245
}
3346

3447
###
35-
DELETE http://localhost:3003/api/prototypes/64feedea0f268b96ab900f3e
36-
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJvb3QiLCJpZCI6IjY0ZmVkMjkyM2EyMDYzZGIyYzAzYmJjNyIsImlhdCI6MTY5NDQyODYzNiwiZXhwIjoxNjk0NDMyMjM2fQ.Z5GHfVEJgYwC_TVS6eBmWYrG6j5etoNpqg4TUkpEfRU
48+
# Delete a prototype with success (token provided)
49+
DELETE http://localhost:3003/api/prototypes/6501c28cafdaa3643dcd0633
50+
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJvb3QiLCJpZCI6IjY0ZmY5NjUzNmU1NjFkOTg2MDkyYWY1ZiIsImlhdCI6MTY5NDYxNDIxOX0.Euwz-1ZdOhVHGK52vaXQa_s0K9p5jslbm7OynqtNHN0
3751

3852

3953
###
54+
# Create a prototype withou token
4055
POST http://localhost:3003/api/prototypes
4156
Content-Type: application/json
4257

4358
{
4459
"title": "Test of blog title 2",
45-
"userId": "64fed2923a2063db2c03bbc7"
4660
}
4761

4862
###
49-
DELETE http://localhost:3003/api/prototypes/64fdd30b5e8ccd4fa2a5ac26
63+
# Delete a prototype with error (not token provided)
64+
DELETE http://localhost:3003/api/prototypes/6501c28cafdaa3643dcd0633
5065

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Create a new user
12
POST http://localhost:3003/api/users
23
Content-Type: application/json
34

@@ -9,6 +10,7 @@ Content-Type: application/json
910
}
1011

1112
###
13+
# Create a new user
1214
POST http://localhost:3003/api/users
1315
Content-Type: application/json
1416

@@ -22,5 +24,18 @@ Content-Type: application/json
2224
###
2325
GET http://localhost:3003/api/users/
2426

27+
###
28+
# Update a user
29+
PUT http://localhost:3003/api/users/64ff96536e561d986092af5f
30+
Content-Type: application/json
31+
Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJvb3QiLCJpZCI6IjY0ZmY5NjUzNmU1NjFkOTg2MDkyYWY1ZiIsImlhdCI6MTY5NDYxMzU5Nn0.RQ8rtgOAET2zPP49M77XPXtm750wiaGFpC4raHbh2nQ
32+
33+
{
34+
"username": "root",
35+
"name": "Superuser",
36+
"password": "test123",
37+
"email": "myemail@email.com"
38+
}
39+
2540
###
2641
DELETE http://localhost:3003/api/users/64ff96297079dd5a4d82316f

0 commit comments

Comments
 (0)