Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

jwtDecode throws InvalidTokenError in React Native

ErrorInvalidTokenError: Invalid token specified: invalid base64 for part #2

Short answer

jwt-decode uses the global atob function. React Native before 0.74 does not define it, so decoding fails with an invalid base64 error. Update React Native to 0.74 or newer, or polyfill atob once at app start. If atob exists, the token string itself is malformed.

  • Applies to jwt-decode 4.x, React Native 0.7x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check in your app whether atob exists at runtime, then look at the token string you pass in.

    console.log(typeof atob);
    console.log(typeof token, token?.split('.').length);
  2. 2Match what you see, then jump to its fix

    1. 1Property 'atob' doesn't existatob is missing in this React Native versionFix
    2. 2Invalid token specified: must be a string, or: missing partsThe value passed is not a clean JWT stringFix

What does the error look like?

  • InvalidTokenError: Invalid token specified: invalid base64 for part #2
  • Property 'atob' doesn't exist

Cause 1 of 2

atob is missing in this React Native version

1Check

The jwt-decode README says atob is not available in React Native before 0.74. Print the type; it logs 'undefined' when missing.

npx react-native --version
# in the app:
console.log(typeof atob)

2Fix

Upgrade React Native to 0.74 or newer. If you cannot upgrade yet, install the base-64 package and assign it to global.atob before any jwtDecode call.

npm install base-64

// index.js, first lines
import { decode } from 'base-64';
if (!global.atob) global.atob = decode;

3Verify

typeof atob is now 'function' and decoding returns the payload.

import { jwtDecode } from 'jwt-decode';
console.log(jwtDecode(token));

Confirmed by github.com

Did this fix it?

Cause 2 of 2

The value passed is not a clean JWT string

1Check

The README lists the failures: the token is not a string, a part is missing (usually a dot is absent), a part is not valid base64, or a part is not valid JSON. A common slip is passing the whole 'Bearer ...' header, an object, or undefined from storage.

console.log(typeof token);
console.log(token?.split('.').length); // a JWT has 3 parts

2Fix

Pass only the raw token, and guard against empty storage.

const raw = header?.startsWith('Bearer ') ? header.slice(7) : header;
if (raw) {
  const payload = jwtDecode(raw);
}

3Verify

A token with three dot-separated parts decodes without throwing.

console.log(jwtDecode(raw).exp);

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Wrap jwtDecode in try/catch and treat InvalidTokenError as a signed-out state.
  • Remember the library only decodes; it does not validate. Verify tokens on the server.
  • Keep React Native on a supported version so platform APIs such as atob exist.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.