'Confusions about "Authorization: Bearer JWT_ACCESS_TOKEN"
I am a beginner using jsonwebtoken to authenticate a user in my node.js web app. I signed a jwt token in app.post('/login') using jwt.sign() and when i try to access/ verify it in app.get('/dashboard') using req.headers['authorization'].split(' ').[1] it gives null or undefined as there is no authorization header in console.log(req.headers).
But it can be access or verify in postman as there i set auth type to bearer <token> due to which number of headers increment by 1 that is authorization: bearer <token>.
Then i store the token in cookie using res.cookie('token', accessToken) and i can access it in my routes without postman.
keeping in view above scenario i have following confusions:
- Where else can i store
jwt tokento access in my browser? and which is most secure store to accessjwt tokenin browser? - Why there is no
authorization: bearer <token>header in my browser?? - Can i add
authorization: bearer <token>header manually to access the token in my browser? if yes...how? - If my app use
httpsinsteadhttpprotocol then will it be same situation of noauthorization: bearer <token>header in browser?
I googled it but cant conclude. please help....!
Solution 1:[1]
In postman you can add token field in header like 
key = token
value = token
and access it:
let token = req.headers['token'] //token = 'token'
it is up to you if you add Bearer before token or not.
key = token
value = Bearer token
additionally postman have an auth tab that do exact thing.
it names key as authorization and for value it adds a prefix based on what you selected like Bearer and then adds it manually to header.
when writing client side cade, it is up to you whether to add Bearer prefix or not.
headers: {
'Authorization': `Basic ${token}`
}
this is not related to http or https.
for how to use jwt in browser read:
Should JWT be stored in localStorage or cookie?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ali Shefaee |
