Skip to content

Commit 2dd550b

Browse files
committed
Added a patch route to reset token
1 parent b957d8c commit 2dd550b

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

src/controllers/v4/internal/user.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@ import crypto from 'crypto';
22
import Users from '../../../models/schemas/User.js';
33
import generateToken from '../../../modules/generateToken.js';
44

5+
/**
6+
* Fetches user profile data based on the provided user ID
7+
*
8+
* @param {Object} req - Express request object.
9+
* @param {Object} res - Express response object.
10+
* @param {Function} next - Express next middleware function.
11+
* @returns {Object} - User profile data.
12+
*/
13+
const retrieveUserProfile = async (req, res, next) => {
14+
const key = req.headers.key;
15+
// Check for valid access key in headers
16+
if (!key || key !== process.env.ACCESS_KEY) {
17+
return res.status(401).json({
18+
message: 'Unauthorized',
19+
});
20+
}
21+
const user = await Users.findById(req.params.id);
22+
if (!user) {
23+
return res.status(404).json({ message: 'User not found' }); // User not found
24+
}
25+
26+
// This will return the data however it won't be the latest one after updating the token
27+
return res.status(200).json(user);
28+
};
29+
530
/**
631
* Fetches user profile data based on the provided user ID and Reset Token.
732
*
@@ -10,7 +35,7 @@ import generateToken from '../../../modules/generateToken.js';
1035
* @param {Function} next - Express next middleware function.
1136
* @returns {Object} - User profile data.
1237
*/
13-
const retrieveAndUpdateUserProfile = async (req, res, next) => {
38+
const updateUserToken = async (req, res, next) => {
1439
const key = req.headers.key;
1540
// Check for valid access key in headers
1641
if (!key || key !== process.env.ACCESS_KEY) {
@@ -27,11 +52,12 @@ const retrieveAndUpdateUserProfile = async (req, res, next) => {
2752
await Users.updateOne(
2853
{ _id: { $eq: req.params.id } },
2954
{ $set: { token: generateToken(req.params.id, process.env.HMAC_KEY) } },
30-
{ upsert: true }, // Create the document if it doesn't exist
3155
);
3256

3357
// This will return the data however it won't be the latest one after updating the token
34-
return res.status(200).json(user);
58+
return res.status(200).json({
59+
message: 'Token reset successfully.',
60+
});
3561
};
3662

3763
/**
@@ -112,4 +138,4 @@ const userEndpoint = async (req, res, next) => {
112138
}
113139
};
114140

115-
export { userEndpoint, retrieveAndUpdateUserProfile };
141+
export { userEndpoint, retrieveUserProfile, updateUserToken };

src/routes/v4/internal/user.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Router } from 'express';
2-
import { userEndpoint, retrieveAndUpdateUserProfile } from '../../../controllers/v4/internal/user.js';
2+
import { userEndpoint, retrieveUserProfile, updateUserToken } from '../../../controllers/v4/internal/user.js';
33
import createRateLimiter from '../../../middlewares/rateLimit.js';
44

55
const router = Router();
@@ -38,9 +38,9 @@ router
3838
/**
3939
* @api {get} v4/user/profile/:id Get User Profile
4040
* @apiDescription Get the profile of a specific user.
41-
* @apiName retrieveAndUpdateUserProfile
41+
* @apiName retrieveUserProfile
4242
* @apiGroup UserManagement
43-
* @apiPermission user
43+
* @apiPermission sudo
4444
*
4545
* @apiHeader {String} Authorization User's access token.
4646
*
@@ -62,7 +62,30 @@ router
6262
* @apiSuccess {function} middleware Express middleware function that handles rate limiting.
6363
*
6464
*/
65-
.get(createRateLimiter(), retrieveAndUpdateUserProfile);
65+
.get(createRateLimiter(), retrieveUserProfile)
66+
/**
67+
* @api {patch} v4/user/profile/:id Get User Profile and Update reset the existing token
68+
* @apiDescription Update the token for a specific user
69+
* @apiName updateUserToken
70+
* @apiGroup UserManagement
71+
* @apiPermission sudo
72+
*
73+
* @apiHeader {String} Authorization User's access token.
74+
*
75+
* @apiParam {String} id User's unique identifier.
76+
*
77+
* @apiSuccess {Object} message
78+
* @apiError (Unauthorized 401) Unauthorized Only authenticated users can access the data.
79+
* @apiError (Forbidden 403) Forbidden Only authorized users can access the data.
80+
* @apiError (Too Many Requests 429) TooManyRequests The client has exceeded the allowed number of requests within the time window.
81+
* @apiError (Internal Server Error 500) InternalServerError An error occurred while processing the rate limit.
82+
*
83+
* @api {function} createRateLimiter
84+
* @apiDescription Creates a rate limiter middleware to control the frequency of requests.
85+
* @apiSuccess {function} middleware Express middleware function that handles rate limiting.
86+
*
87+
*/
88+
.patch(createRateLimiter(), updateUserToken);
6689

6790
// Export the router
6891
export default router;

0 commit comments

Comments
 (0)