Skip to content

Commit b957d8c

Browse files
committed
Added a way to reset the token of user upon leaving the server
1 parent 08845a5 commit b957d8c

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/controllers/v4/internal/user.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import Users from '../../../models/schemas/User.js';
33
import generateToken from '../../../modules/generateToken.js';
44

55
/**
6-
* Fetches user profile data based on the provided user ID.
6+
* Fetches user profile data based on the provided user ID and Reset Token.
77
*
88
* @param {Object} req - Express request object.
99
* @param {Object} res - Express response object.
1010
* @param {Function} next - Express next middleware function.
1111
* @returns {Object} - User profile data.
1212
*/
13-
const getUserProfile = async (req, res, next) => {
13+
const retrieveAndUpdateUserProfile = async (req, res, next) => {
1414
const key = req.headers.key;
1515
// Check for valid access key in headers
1616
if (!key || key !== process.env.ACCESS_KEY) {
@@ -23,6 +23,14 @@ const getUserProfile = async (req, res, next) => {
2323
return res.status(404).json({ message: 'User not found' }); // User not found
2424
}
2525

26+
// Update user's token in the database
27+
await Users.updateOne(
28+
{ _id: { $eq: req.params.id } },
29+
{ $set: { token: generateToken(req.params.id, process.env.HMAC_KEY) } },
30+
{ upsert: true }, // Create the document if it doesn't exist
31+
);
32+
33+
// This will return the data however it won't be the latest one after updating the token
2634
return res.status(200).json(user);
2735
};
2836

@@ -104,4 +112,4 @@ const userEndpoint = async (req, res, next) => {
104112
}
105113
};
106114

107-
export { userEndpoint, getUserProfile };
115+
export { userEndpoint, retrieveAndUpdateUserProfile };

src/routes/v4/internal/user.js

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

55
const router = Router();
@@ -38,7 +38,7 @@ router
3838
/**
3939
* @api {get} v4/user/profile/:id Get User Profile
4040
* @apiDescription Get the profile of a specific user.
41-
* @apiName getUserProfile
41+
* @apiName retrieveAndUpdateUserProfile
4242
* @apiGroup UserManagement
4343
* @apiPermission user
4444
*
@@ -62,7 +62,7 @@ router
6262
* @apiSuccess {function} middleware Express middleware function that handles rate limiting.
6363
*
6464
*/
65-
.get(createRateLimiter(), getUserProfile);
65+
.get(createRateLimiter(), retrieveAndUpdateUserProfile);
6666

6767
// Export the router
6868
export default router;

0 commit comments

Comments
 (0)