Overview

Implemented an auth middleware with jwt.verify() to decode tokens, attach the payload to req, and render it on the profile page. Validation and richer error handling are queued for tomorrow.

What I Learned

Key Concepts

Auth Middleware (JWT Verification)

const authenticate = async (req, res, next) => {
  const token = req.cookies.token;
  if (!token) return res.status(401).send("Access denied");

  try {
    const decoded = jwt.verify(token, process.env.JWT_SECRET);
    req.user = decoded;// Attach user data to reqnext();
  } catch (error) {
    res.status(401).send("Invalid token");
  }
};

Profile Route (Using Middleware)

app.get("/profile", authenticate, (req, res) => {
  res.render("profile", { user: req.user });
});

What I Built