I'm trying to POST form data which is going via some authentication middleware.
The problem that I'm having is with the middleware i'm unable to find the JWT token on req.headers (or any other part of the req object).
My post request
axios.post('/restaurant/saveMenu', {
headers: {
'Authorization': `bearer ${jwt}`
},
data: {
'lorem': 'ipsum'
}
})
.then(response => {
console.log(response)
})
.catch(response => {
console.log(response);
});
req.headers in the middleware when consoled out.
{ host: 'localhost:3000',
connection: 'keep-alive',
'content-length': '269',
accept: 'application/json, text/plain, */*',
origin: 'http://localhost:3000',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/57.0.2987.133 Safari/
537.36',
'content-type': 'application/json;charset=UTF-8',
referer: 'http://localhost:3000/restaurantmenu',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.8' }
In my server file i'm settings the Cors to (not sure if this is correct?)
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods',
'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type,
Authorization, Content-Length, X-Requested-With');
On another note if I was to use bodyparser I could find the JWT token on req.body.headers, I would like to avoid using bodyparser for this route as I would like to share the same auth middleware for the GET & POST requests.
Screenshot of network tab in chrome
Thanks in advance!
via user3816882
No comments:
Post a Comment