question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jwt.verify/decode working only on static strings when using websockets

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
FrederickEngelhardtcommented, Apr 11, 2019

@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.

      this.socket.on('authentication_token', (token) => {
        jwt.verify(JSON.parse(token), process.env.JWT_KEY, { algorithm: 'HS512', maxAge: '2d'}, function(err, decode) {
          console.log(decode)
        })
      });
0reactions
mirajehossaincommented, Apr 13, 2020

I was also receiving a null value when trying to use jwt.decode(token). Turns out I also needed to parse it jwt.decode(JSON.parse(token))

token already string, but when I parse it using JSON.parse() it throw parse error.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found