From 7d9c30c268e8cf414cc738422d102b9e09b3e000 Mon Sep 17 00:00:00 2001 From: Frederik Prijck Date: Tue, 14 Nov 2023 10:33:17 +0100 Subject: [PATCH] Add README instructions for polyfilling atob (#242) --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index dc4ce06..02f87be 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,23 @@ console.log(decodedHeader); **Note:** A falsy or malformed token will throw an `InvalidTokenError` error; see below for more information on specific errors. +## Polyfilling atob + +This library relies on `atob()`, which is a global function available on [all modern browsers as well as every supported node environment](https://developer.mozilla.org/en-US/docs/Web/API/atob#browser_compatibility). + +In order to use `jwt-decode` in an environment that has no access to `atob()` (e.g. [React Native](https://github.com/facebook/hermes/issues/1178)), ensure to provide the corresponding polyfill in your application by using [`core-js/stable/atob`](https://www.npmjs.com/package/core-js): + +```js +import "core-js/stable/atob"; +``` + +Alternatively, you can also use [`base-64`](https://www.npmjs.com/package/base-64) and polyfill `global.atob` yourself: + +```js +import { decode } from "base-64"; +global.atob = decode; +``` + ## Errors This library works with valid JSON web tokens. The basic format of these token is