jwt.verify/decode working only on static strings when using websockets
See original GitHub issueHi,
I’m able to generate a token and then send it to a used. The user then stores this locally. Afterwards we call a authentication script to send that token up to the server
Issue: The dynamic token sent is never valid. However, if you console.log this value and hardcode it into the verify function you are able to get a verification and result.
Using this with websockets if that matters.
this.socket.on('authentication_token', (token) => {
jwt.verify(token, process.env.JWT_KEY, { algorithm: 'HS512', maxAge: '2d'}, function(err, decode) {
console.log(decode)
})
});
Working example with REST
router.post('/jwt/verify', async (req,res,next) => {
const { token } = req.body;
const data = jwt.verify(token, process.env.JWT_KEY, { algorithm: 'HS512', maxAge: '2d'}, function(err, decode) {
if (err){
console.log('ERROR', err)
}
console.log(decode)
});
res.send(true);
});
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Authenticating Users Over WebSockets with JSON Web ...
In this guide, you will learn how to use Websockets and JSON Web Tokens, which are also called JWTs, together in your app...
Read more >Spring Boot 4.3.5 WebSocket Chat with jwt authorization. No ...
When user authenticates with Spring Security, WebSocket module creates unique channel for that user based on his Principal.
Read more >Using WebSockets with React.js, the right way (no library ...
In the project I'm currently working on, I have a React.js frontend and a WebSocket server that need to be connected. I spent...
Read more >Using Spring Boot for WebSocket Implementation with STOMP
To keep things short, it will let our WebSockets work even if the WebSocket protocol is not supported by an internet browser. I...
Read more >Using WebSockets - Quarkus
If you only want to use the WebSocket client you should include quarkus-websockets-client instead. Handling web sockets. Our application contains a single class ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@panva looks like I was missing a JSON.parse for the token inside of the websocket response. Adding that did the trick. Thank you!
See code below for reference.
token already string, but when I parse it using JSON.parse() it throw parse error.