DEV Community

Ali Hamza
Ali Hamza

Posted on

Day 167 of Learning MERN Stack

Hello Dev Community! 👋

It is officially Day 167 of my full-stack development journey! Today, I shifted focus to the backend of QuickChat, engineering the user authentication, verification middleware, and profile update endpoints using Node.js, Express, MongoDB, and Cloudinary! ⚙️🔑

Here is the breakdown of the backend architecture developed today.


🛠️ Technical Breakdown: Authentication & Profile Controllers

As shown in my implementation snapshots (Screenshots 407 to 410):

1. Express Routing & Protection Middleware

  • Encapsulated routing under userRouter.js and protected endpoints using custom protectRoute JWT middleware:

javascript
  userRouter.post('/signup', signup);
  userRouter.post('/login', Login);
  userRouter.put('/update-profile', protectRuote, updateProfile);
  userRouter.put('/check', protectRuote, checkAuth);
  **if (!profilePic) {
  updatedUser = await user.findByIdAndUpdate(userId, { bio, fullName }, { new: true });
} else {
  const upload = await cloudinary.uploader.upload(profilePic);
  updatedUser = await user.findByIdAndUpdate(
    userId, 
    { profilePic: upload.secure_url, bio, fullName }, 
    { new: true }
  ).select("-password");
}**
Enter fullscreen mode Exit fullscreen mode

Top comments (0)