Middleware auth che estrae il token dalla richiesta:
const { authorization } = req.headers;
if (!authorization) {
return res.status(401).send({ message: "Unauthorized" });
}
if (!authorization.startsWith("Bearer")) {
return res.status(401).send({ message: "Unauthorized" });
}
const split = authorization.split("Bearer ");
if (split.length !== 2) {
return res.status(401).send({ message: "Unauthorized" });
}
const token = split[1];Da Postman scegli il tipo Bearer Token. Sul frontend:
fetch("/api/path", {
method: "GET",
headers: { "Authorization": `Bearer ${token}` }
}).then(res => res.json());Dopo jwt.verify, usa il subject del payload per caricare il documento utente corrispondente.