jwt-decode: Invalid token specified: Cannot read property 'replace' of undefined
I get Invalid token specified: Cannot read property ‘replace’ of undefined:
Object.<anonymous> ../node_modules/jwt-decode/lib/index.js 9:0
Showing original source content from sourcemap
'use strict';
var base64_url_decode = require('./base64_url_decode');
function InvalidTokenError(message) {
this.message = message;
}
InvalidTokenError.prototype = new Error();
InvalidTokenError.prototype.name = 'InvalidTokenError';
module.exports = function(token, options) {
if (typeof token !== 'string') {
throw new InvalidTokenError('Invalid token specified');
}
Can i get any help ASAP please?!
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 16 (2 by maintainers)
Commits related to this issue
- More helpful error messages The current error messages can be a bit confusing; especially the `cannot read property 'replace' of undefined` error (see #72). This adds 2 more checks to give more helpf... — committed to thisisdevelopment/jwt-decode by ederuiter 2 years ago
- More helpful error messages The current error messages can be a bit confusing; especially the `cannot read property 'replace' of undefined` error (see #72). This adds 2 more checks to give more helpf... — committed to auth0/jwt-decode by ederuiter 2 years ago
- More helpful error messages (#138) * More helpful error messages The current error messages can be a bit confusing; especially the `cannot read property 'replace' of undefined` error (see #72). This... — committed to auth0/jwt-decode by frederikprijck a year ago
I had same problem as well, If you are login to the App and you did play with token in backend(postman). then token mismatch might happen so you take recent token from postman and add it to the localstorage in the browser, which was in undefined.
In my case, I’m attempting to protect the client-side app against “corrupt” cookies (tokens that aren’t in JWT format) in the event a user messes with their cookies. In the moment I’m decoding, if the token can’t be parsed, the error thrown breaks the app, despite my attempt to catch.
Upon attempting to decode a non-JWT format value, the catch triggers, but the error still shows as Uncaught and breaks runtime:
I was having this issue when using jwtDecode with a token issued from our Auth0 tenant.
Ensure your “audience” option in your Auth0 config exists and is correct. If it is not, you get back a token in the incorrect format.
You need to post a little more context when posting an issue. How are you using jwtDecode? What format is the token you are passing in?
For future reference: this error occurs when the token does not adhere to the expected format of a jwt token
For example I ran into this in unit tests where the token was mocked with something like “FAKE_TOKEN” Currently this lib assumes a valid syntax for the token meaning the token must include a single dot (.) and the part after the dot must be a valid base64 encode otherwise you’d get these errors.
Adding a check on undefined in base64_url_decode would solve this and could give developers a good clue to what is going wrong.
For now I replaced my
FAKE_TOKEN
withFAKE.eyJmYWtlIjogdHJ1ZX0=
which causes the JwtDecode to succeedHey friends,
I’ve been trying to reproduce all of the errors mentioned above. When using the
decode
function, make sure:a–z
,A–Z
,0–9
, and-_.
I feel it is only happening because token is set to undefined. Either clear out that token from inspect menu and refresh your application. It may work then.
in my case, I had to do some manipulation to remove the word "Bearer " out of the token string