Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions .eslintrc.json

This file was deleted.

6 changes: 3 additions & 3 deletions .github/actions/npm-publish/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ inputs:
require-build:
default: true
release-directory:
default: './'
default: "./"

runs:
using: composite
Expand All @@ -23,8 +23,8 @@ runs:
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: 'npm'
registry-url: 'https://registry.npmjs.org'
cache: "npm"
registry-url: "https://registry.npmjs.org"

- name: Install dependencies
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/tag-exists/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ inputs:

outputs:
exists:
description: 'Whether the tag exists or not'
description: "Whether the tag exists or not"
value: ${{ steps.tag-exists.outputs.EXISTS }}

runs:
Expand Down
2 changes: 1 addition & 1 deletion .github/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ staleLabel: closed:stale

# Comment to post when marking as stale. Set to `false` to disable
markComment: >
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇‍♂️
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you have not received a response for our team (apologies for the delay) and this is still a blocker, please reply with additional information or just a ping. Thank you for your contribution! 🙇‍♂️
17 changes: 8 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: Build and Test
on:
workflow_dispatch:
push:
branches: [ main, beta ]
branches: [main, beta]
pull_request:
branches: [ main, beta ]
branches: [main, beta]

permissions:
contents: read
Expand All @@ -15,11 +15,10 @@ concurrency:
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:

build:
strategy:
matrix:
node: [ 18, 20 ]
node: [18, 20]

name: Build Package (Node ${{ matrix.node }})
runs-on: ubuntu-latest
Expand All @@ -35,15 +34,15 @@ jobs:

- name: Install dependencies
run: npm ci

- name: Build package
run: npm run build

- name: Lint
run: npm run lint && npm run lint:package
run: npm run lint && npm run lint:package && npm run format:check

- name: Run tests against browser
run: npm run test:browser

- name: Run tests against Node
run: npm run test:node
4 changes: 2 additions & 2 deletions .github/workflows/npm-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
default: true
type: string
release-directory:
default: './'
default: "./"
type: string
secrets:
github-token:
Expand Down Expand Up @@ -77,4 +77,4 @@ jobs:
body: ${{ steps.get_release_notes.outputs.release-notes }}
tag: ${{ steps.get_version.outputs.version }}
commit: ${{ github.sha }}
prerelease: ${{ steps.get_prerelease.outputs.prerelease }}
prerelease: ${{ steps.get_prerelease.outputs.prerelease }}
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
require-build: true
secrets:
npm-token: ${{ secrets.NPM_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/semgrep.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: ["master", "main"]

schedule:
- cron: '30 0 1,15 * *'
- cron: "30 0 1,15 * *"

jobs:
semgrep:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/snyk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
branches:
- main
schedule:
- cron: '30 0 1,15 * *'
- cron: "30 0 1,15 * *"

permissions:
contents: read
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ The `jwtDecode` function is now no longer the default export, and is instead pro
[Full Changelog](https://github.com/auth0/jwt-decode/compare/v4.0.0-beta.3..v4.0.0-beta.4)

**Breaking changes**

- Raise minimum Node.js version to 18 [\#209](https://github.com/auth0/jwt-decode/pull/209) ([jonkoops](https://github.com/jonkoops))

**Fixed**

- fix default condition should be the last one [\#199](https://github.com/auth0/jwt-decode/pull/199) ([frederikprijck](https://github.com/frederikprijck))

## Version [4.0.0-beta.3](https://github.com/auth0/jwt-decode/releases/tag/v4.0.0-beta.3)

[Full Changelog](https://github.com/auth0/jwt-decode/compare/v4.0.0-beta.2..v4.0.0-beta.3)

**Breaking changes**

- Drop UMD bundle [\#193](https://github.com/auth0/jwt-decode/pull/193) ([frederikprijck](https://github.com/frederikprijck))

**Changed**

- Use modern JavaScript syntax [\#187](https://github.com/auth0/jwt-decode/pull/187) ([jonkoops](https://github.com/jonkoops))
- Use ESNext as default module system [\#188](https://github.com/auth0/jwt-decode/pull/188) ([jonkoops](https://github.com/jonkoops))
- Avoid using any bundlers but use tsc instead [\#192](https://github.com/auth0/jwt-decode/pull/192) ([frederikprijck](https://github.com/frederikprijck))
Expand All @@ -53,17 +57,20 @@ The `jwtDecode` function is now no longer the default export, and is instead pro
[Full Changelog](https://github.com/auth0/jwt-decode/compare/v4.0.0-beta.1..v4.0.0-beta.2)

**Changed**

- Avoid using default exports [\#175](https://github.com/auth0/jwt-decode/pull/175) ([frederikprijck](https://github.com/frederikprijck))
- Make options optional no default function parameter initializer [\#179](https://github.com/auth0/jwt-decode/pull/179) ([cristobal](https://github.com/cristobal))

**Fixed**

- Ensure types are bundled and correctly linked [\#174](https://github.com/auth0/jwt-decode/pull/174) ([jonkoops](https://github.com/jonkoops))

## Version [4.0.0-beta.1](https://github.com/auth0/jwt-decode/releases/tag/v4.0.0-beta.1)

[Full Changelog](https://github.com/auth0/jwt-decode/compare/v4.0.0-beta.0..v4.0.0-beta.1)

**Fixed**

- Ensure build is run on prepack [\#167](https://github.com/auth0/jwt-decode/pull/167) ([frederikprijck](https://github.com/frederikprijck))

## Version [4.0.0-beta.0](https://github.com/auth0/jwt-decode/releases/tag/v4.0.0-beta.0)
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const decoded = jwtDecode(token);
console.log(decoded);

/* prints:
* {
* {
* foo: "bar",
* exp: 1393286893,
* iat: 1393268893
* iat: 1393268893
* }
*/

Expand All @@ -44,9 +44,9 @@ const decodedHeader = jwtDecode(token, { header: true });
console.log(decodedHeader);

/* prints:
* {
* {
* typ: "JWT",
* alg: "HS256"
* alg: "HS256"
* }
*/
```
Expand All @@ -73,22 +73,24 @@ global.atob = decode;
## Errors

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

```
[part1].[part2].[part3]
```

All parts are supposed to be valid base64 (url) encoded json.
Depending on the `{ header: <option> }` option it will decode part 1 (only if header: true is specified) or part 2 (default)

Not adhering to the format will result in a `InvalidTokenError` with one of the following messages:

- `Invalid token specified: must be a string` => the token passed was not a string, this library only works on strings.
- `Invalid token specified: missing part #` => this probably means you are missing a dot (`.`) in the token
- `Invalid token specified: must be a string` => the token passed was not a string, this library only works on strings.
- `Invalid token specified: missing part #` => this probably means you are missing a dot (`.`) in the token
- `Invalid token specified: invalid base64 for part #` => the part could not be base64 decoded (the message should contain the error the base64 decoder gave)
- `Invalid token specified: invalid json for part #` => the part was correctly base64 decoded, however, the decoded value was not valid JSON (the message should contain the error the JSON parser gave)

#### Use with TypeScript

The return type of the `jwtDecode` function is determined by the `header` property of the object passed as the second argument. If omitted (or set to false), it'll use `JwtPayload`, when true it will use `JwtHeader`.
The return type of the `jwtDecode` function is determined by the `header` property of the object passed as the second argument. If omitted (or set to false), it'll use `JwtPayload`, when true it will use `JwtHeader`.
If needed, you can specify what the expected return type should be by passing a type argument to the `jwtDecode` function.

You can extend both `JwtHeader` and `JwtPayload` to include non-standard claims or properties.
Expand Down
26 changes: 26 additions & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import eslint from "@eslint/js";
import eslintConfigPrettier from "eslint-config-prettier/flat";
import { defineConfig, globalIgnores } from "eslint/config";
import tseslint from "typescript-eslint";

export default defineConfig(
globalIgnores(["build", "coverage"]),
eslint.configs.recommended,
tseslint.configs.strictTypeChecked,
tseslint.configs.stylisticTypeChecked,
eslintConfigPrettier,
{
languageOptions: {
parserOptions: {
projectService: true,
},
},
},
{
files: ["lib/**/*.ts"],
rules: {
"@typescript-eslint/no-unnecessary-type-parameters": "off",
"@typescript-eslint/restrict-template-expressions": "off",
Comment on lines +22 to +23
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These rules seem to give some false positives, so I have disabled them.

},
},
);
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function base64UrlDecode(str: string) {

try {
return b64DecodeUnicode(output);
} catch (err) {
} catch {
return atob(output);
}
}
Expand All @@ -69,7 +69,7 @@ export function jwtDecode<T = JwtHeader | JwtPayload>(
throw new InvalidTokenError("Invalid token specified: must be a string");
}

options ||= {};
options ??= {};

const pos = options.header === true ? 0 : 1;
const part = token.split(".")[pos];
Expand Down
Loading