Skip to content

Commit

Permalink
Added an endpoint to fetch user details
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrea committed Jan 5, 2024
1 parent bc10f32 commit ca3a4be
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@ import crypto from 'crypto';
import Users from '../../../models/schemas/User.js';
import generateToken from '../../../utils/generateToken.js';

/**
* Fetches user profile data based on the provided user ID.
*
* @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 key = req.headers;
// Check for valid access key in headers
if (!key || key !== process.env.ACCESS_KEY) {
return res.status(401).json({
message: 'Unauthorized',
});
}
const user = await Users.findById(req.params.id);

if (!user) {
return res.status(404).json({ message: 'User not found' }); // User not found
}

return res.status(200).json(user);
};

/**
* Handles user-related operations based on the HTTP method.
*
Expand Down Expand Up @@ -35,7 +60,7 @@ const userEndpoint = async (req, res, next) => {
await Users.updateOne(
{ _id: { $eq: id } },
{ $set: { token: token } },
{ upsert: true } // Create the document if it doesn't exist
{ upsert: true }, // Create the document if it doesn't exist
);

return res.status(200).json({
Expand Down Expand Up @@ -80,4 +105,4 @@ const userEndpoint = async (req, res, next) => {
}
};

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

const router = Router();

router
.route('/')
/**
* @api {post} v4/user Get User Details
* @apiDescription Get details about the authenticated user.
* @apiName getUserDetails
* @apiGroup UserManagement
* @apiPermission user
*
* @apiHeader {String} Authorization User's access token.
*
* @apiSuccess {Object} userDetails User's details.
* @apiSuccess {String} userDetails.username User's username.
* @apiSuccess {String} userDetails.email User's email address.
* @apiSuccess {String} userDetails.avatar User's avatar URL.
* @apiSuccess {Date} userDetails.createdAt Date when the user profile was created.
*
* @apiError (Unauthorized 401) Unauthorized Only authenticated users can access the data.
* @apiError (Forbidden 403) Forbidden Only authorized users can access the data.
* @apiError (Too Many Requests 429) TooManyRequests The client has exceeded the allowed number of requests within the time window.
* @apiError (Internal Server Error 500) InternalServerError An error occurred while processing the rate limit.
*
* @api {function} createRateLimiter
* @apiDescription Creates a rate limiter middleware to control the frequency of requests.
* @apiSuccess {function} middleware Express middleware function that handles rate limiting.
*
*/
.post(createRateLimiter(), userEndpoint);

router
.route('/profile/:id')
/**
* @api {get} v4/user/profile/:id Get User Profile
* @apiDescription Get the profile of a specific user.
* @apiName getUserProfile
* @apiGroup UserManagement
* @apiPermission user
*
* @apiHeader {String} Authorization User's access token.
*
* @apiParam {String} id User's unique identifier.
*
* @apiSuccess {Object} userProfile User's profile information.
* @apiSuccess {String} userProfile.username User's username.
* @apiSuccess {String} userProfile.email User's email address.
* @apiSuccess {String} userProfile.avatar User's avatar URL.
* @apiSuccess {Date} userProfile.createdAt Date when the user profile was created.
*
* @apiError (Unauthorized 401) Unauthorized Only authenticated users can access the data.
* @apiError (Forbidden 403) Forbidden Only authorized users can access the data.
* @apiError (Too Many Requests 429) TooManyRequests The client has exceeded the allowed number of requests within the time window.
* @apiError (Internal Server Error 500) InternalServerError An error occurred while processing the rate limit.
*
* @api {function} createRateLimiter
* @apiDescription Creates a rate limiter middleware to control the frequency of requests.
* @apiSuccess {function} middleware Express middleware function that handles rate limiting.
*
*/
.get(createRateLimiter(), getUserProfile);

// Export the router
export default router;

0 comments on commit ca3a4be

Please sign in to comment.