From 34837b21a013647f3acb1729f3686d2720dd8e40 Mon Sep 17 00:00:00 2001 From: albidoo Date: Thu, 16 Oct 2025 17:07:27 +0530 Subject: [PATCH] feat(controller): implement get user profile endpoint --- backend/controllers/authController.js | 24 ++++++++++++++++++++---- backend/routes/authRoutes.js | 4 +++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/backend/controllers/authController.js b/backend/controllers/authController.js index c03bbcd..0b2019e 100644 --- a/backend/controllers/authController.js +++ b/backend/controllers/authController.js @@ -206,18 +206,34 @@ export const loginUser = async (req, res, next) => { export const getProfile = async (req, res, next) => { try { // TODO: Implement get profile logic + //altho user will be there, just incase added it + if (!req.user) { + return res.status(401).json({ + success: false, + message: "User is not validated", + }); + } + // Option 1: Use req.user directly (set by protect middleware) // return res.status(200).json({ success: true, user: req.user }); // Option 2: Fetch fresh data from database // const user = await User.findById(req.user.id).select('-password'); + const user = await User.findById(req.user.id).select("-password"); + // if (!user) { return 404 error } - // return res.status(200).json({ success: true, user }); + if (!user) { + return res.status(404).json({ + success: false, + message: "User not found!", + }); + } - res.status(501).json({ - success: false, - message: 'Get profile endpoint not implemented yet', + // return res.status(200).json({ success: true, user }); + return res.status(200).json({ + success: true, + user, }); } catch (error) { next(error); diff --git a/backend/routes/authRoutes.js b/backend/routes/authRoutes.js index cc6b719..6d85a21 100644 --- a/backend/routes/authRoutes.js +++ b/backend/routes/authRoutes.js @@ -47,7 +47,7 @@ import express from 'express'; // import { validateRegister, validateLogin, validate } from '../middleware/validationMiddleware.js'; // TODO: Import auth middleware -// import { protect } from '../middleware/authMiddleware.js'; +import { protect } from '../middleware/authMiddleware.js'; const router = express.Router(); @@ -80,6 +80,8 @@ router.get('/test', (req, res) => { }); }); +router.get('/profile', protect, getProfile); + // TODO: Add actual auth routes here export default router;