Building a simple ToDo app with ReactJs frontend and NodeJs/Express backend. I configured my frontend to include userId as a request header:
export default function authHeader() {
const user = JSON.parse(localStorage.getItem('user'));
if (user && user.accessToken) {
// return { Authorization: 'Bearer ' + user.accessToken }; // for Spring Boot back-end
return {
'x-access-token': user.accessToken,
'userid': user.id
}; // for Node.js Express back-end
} else {
return {};
}
}
This header is included with the Axios request:
// List all Group Members
listMembers() {
return http.get(`/group`, { headers: authHeader() });
}
Consequently, I can see the headers in the request:
Part of my auth middleware references the access token:
verifyToken = (req, res, next) => {
let token = req.headers["x-access-token"];
if (!token) {
return res.status(403).send({
message: "No token provided!"
});
}
jwt.verify(token, config.secret, (err, decoded) => {
if (err) {
return res.status(401).send({
message: "Unauthorized!"
});
}
req.userId = decoded.id;
next();
});
};
The req.headers lines near the top is able to pull the token from header "x-access-token" with no problem. HOWEVER - for debug purposes, I have tried to pull the header userId value instead using let token = req.headers["userid"]; but this simply comes back as undefined in my debug tools. Why can it pull x-access token header but not userId? My eventual goal is to refer to the req.header userId value in backend SQL queries, this is just a test.
It ended being a capitalization error somewhere in pipeline. @Sardar's comment was on the money, make sure everything is spelled correctly and matches case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.