Skip to content

Commit

Permalink
Added a way to reset the token of user upon leaving the server
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrea committed Jan 9, 2024
1 parent 08845a5 commit b957d8c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/controllers/v4/internal/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import Users from '../../../models/schemas/User.js';
import generateToken from '../../../modules/generateToken.js';

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

// Update user's token in the database
await Users.updateOne(
{ _id: { $eq: req.params.id } },
{ $set: { token: generateToken(req.params.id, process.env.HMAC_KEY) } },
{ upsert: true }, // Create the document if it doesn't exist
);

// This will return the data however it won't be the latest one after updating the token
return res.status(200).json(user);
};

Expand Down Expand Up @@ -104,4 +112,4 @@ const userEndpoint = async (req, res, next) => {
}
};

export { userEndpoint, getUserProfile };
export { userEndpoint, retrieveAndUpdateUserProfile };
6 changes: 3 additions & 3 deletions src/routes/v4/internal/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router } from 'express';
import { userEndpoint, getUserProfile } from '../../../controllers/v4/internal/user.js';
import { userEndpoint, retrieveAndUpdateUserProfile } from '../../../controllers/v4/internal/user.js';
import createRateLimiter from '../../../middlewares/rateLimit.js';

const router = Router();
Expand Down Expand Up @@ -38,7 +38,7 @@ router
/**
* @api {get} v4/user/profile/:id Get User Profile
* @apiDescription Get the profile of a specific user.
* @apiName getUserProfile
* @apiName retrieveAndUpdateUserProfile
* @apiGroup UserManagement
* @apiPermission user
*
Expand All @@ -62,7 +62,7 @@ router
* @apiSuccess {function} middleware Express middleware function that handles rate limiting.
*
*/
.get(createRateLimiter(), getUserProfile);
.get(createRateLimiter(), retrieveAndUpdateUserProfile);

// Export the router
export default router;

0 comments on commit b957d8c

Please sign in to comment.