Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add README instructions for polyfilling atob #242

Merged
merged 10 commits into from
Nov 14, 2023
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ 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()`, ensure to provide the corresponding polyfill in your application:

```js
import "core-js/stable/atob";
```

Some environments might not work well with polyfills and require you to import the pure function and expose it yourself instead (e.g. React Native):

```js
import atob from "core-js-pure/stable/atob";

global.atob = atob;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does React Native have support for globalThis? That might be preferential here.

Copy link
Member Author

@frederikprijck frederikprijck Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on your investigation, I am considering reverting this and only recommend using the core-js/stable/atob, or whatever polyfill works for the platform of choise. What do you think?

Copy link
Member Author

@frederikprijck frederikprijck Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better, I wonder if we should also just recommend base-64, which has been succesfuly used here: https://github.com/techmatters/terraso-mobile-client/pull/567/files

The benefit of base-64 is that it's created by an active and well-known person, so I am confident. It also has 0 dependencies, so even though it hasnt had any release since 2020, I do not think there is any reason to cut releases and it shouldn't mean it's a package to avoid.

jwt-decode was just as stale before we cut the new major, for the same reason. There was no reason to cut any release for it.

Even more so, based on the messages in this PR, I am leaning towards only mentioning base-64 in the readme. What do you think?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base-64 LGFM

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good idea.

Copy link
Member Author

@frederikprijck frederikprijck Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the PR to only mention base-64. LMK what you think. I tried this in your sample app @jonkoops and it seems to work.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. 👍

Copy link
Member Author

@frederikprijck frederikprijck Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the repo on base-64 does look kinda stale. We can go with both core-js and base-64 and see if any issues arise and remove the recommendation of base-64 if that's the case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a fine solution @frederikprijck. I am hopeful Hermes might eventually implement this feature, so we might be able to remove this in the future sometime.

```

## Errors

This library works with valid JSON web tokens. The basic format of these token is
Expand Down