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.
jwt.verify() to decode and validate tokensreq objectjwt.verify() checks token validity and extracts the payloadreq is available to downstream handlersconst 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");
}
};
app.get("/profile", authenticate, (req, res) => {
res.render("profile", { user: req.user });
});