jwtDecode throws InvalidTokenError in React Native
InvalidTokenError: Invalid token specified: invalid base64 for part #2Short 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.
Fix it fast
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);2Match what you see, then jump to its fix
What does the error look like?
InvalidTokenError: Invalid token specified: invalid base64 for part #2Property '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
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 parts2Fix
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Back end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
npm start stops with Could not find a required file in a React app
Name: index.html
react-scripts checks for two files before the dev server starts: public/index.html and a src/index file.
3 causes, each with a fixBack end
Create React App says Node is too old, and npm will not run on the new one
npm does not support Node.js v10.19.0
The shell is running an old Node.js that is below the version Create React App needs.
3 causes, each with a fixBack end
Flask-WTF rejects a React request even though the CSRF token is sent
400 Bad Request on POST, PUT, PATCH or DELETE from a React app
Flask-WTF checks two things: the token sent with the request and a copy kept in the session cookie.
4 causes, each with a fixBack end
CSRF token mismatch when a React app logs in to a Laravel Sanctum API
Access-Control-Allow-Credentials
With Sanctum's cookie-based SPA login, Laravel only accepts a state-changing request if the browser first got an XSRF-TOKEN cookie and sends it back with its session cookie.
4 causes, each with a fixBack end
npm audit stops with ENOLOCK because there is no lockfile
npm ERR! audit
npm audit sends the dependency tree from your lockfile to the registry.
2 causes, each with a fix