Skip to content
Advertisement

Cannot verify JWT – UnhandledPromiseRejectionWarning: JsonWebTokenError: jwt malformed

I have function that just send data to database (my posts). I use private and public keys to sign and verify tokens. I can send this token in header from front-end to back-end, but has problem with verifying it. Here is how this flow looks like:

Front-end

JavaScript

Back-end

JavaScript

Middleware auth

JavaScript

And JWT service

JavaScript

The problem starts after getUserPromise function. This function can get a token, but can’t verify it and I have this problem:

UnhandledPromiseRejectionWarning: JsonWebTokenError: jwt malformed

Actually, I have no idea where problem is. I generated key pair, and sign function can sing and return token, which looks like this: 351e38a4bbc517b1c81e180479a221d404c724107988852c7768d813dd0510e6183306b1d837091b2cddaa07f2427b7a

So, what’s the problem?

Advertisement

Answer

I have found the solution of this problem and it feels shame. In JWT service pay attention to this string:

algorithm: "RS256"

As you can see I use RS256, but I generated certificates in other format, so, because of this I got that error.

So, if you use RSA certificates, pay attention to algorithm!

EDIT:

Here is how you can generate pair for RS256:

  1. Private
JavaScript
  1. Public from private
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement