Docs site - explore our docs site and learn more about Auth0.
Getting started
Installation
Install with NPM or Yarn.
Run npm install jwt-decode or yarn add jwt-decode to install the library.
Usage
1import { jwtDecode } from "jwt-decode";
23const token = "eyJ0eXAiO.../// jwt token";
4const decoded = jwtDecode(token);
56console.log(decoded);
78/* prints:9 * {
10 * foo: "bar",
11 * exp: 1393286893,
12 * iat:139326889313 * }
14 */
1516// decode header by passing in options (useful for when you need `kid` to verify a JWT):
17const decodedHeader = jwtDecode(token, { header: true });
18console.log(decodedHeader);
1920/* prints:21 * {
22 * typ: "JWT",
23 * alg:"HS256"24 * }
25 */
Note: A falsy or malformed token will throw an InvalidTokenError error; see below for more information on specific errors.
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: 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.
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.
1import { jwtDecode } from"jwt-decode";
23const token = "eyJhsw5c";
4const decoded = jwtDecode<JwtPayload>(token); // Returns with the JwtPayload type
Copy the file jwt-decode.js from the root of the build/esm folder to your project somewhere, then import jwtDecode from it inside a script tag that's marked with type="module":
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?
This project is licensed under the MIT license. See the LICENSE file for more info.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
all changesets reviewed
Reason
license file detected
Details
Info: project has a license file: LICENSE:0
Info: FSF or OSI recognized license: MIT License: LICENSE:0
Info: Found linked content: github.com/auth0/.github/.github/SECURITY.md:1
Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/auth0/.github/.github/SECURITY.md:1
Info: Found text in security policy: github.com/auth0/.github/.github/SECURITY.md:1
Reason
SAST tool detected
Details
Info: SAST configuration detected: Snyk
Warn: 0 commits out of 30 are checked with a SAST tool
Reason
dependency not pinned by hash detected -- score normalized to 6
Details
Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/auth0/jwt-decode/build.yml/main?enable=pin
Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/auth0/jwt-decode/build.yml/main?enable=pin
Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-release.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/auth0/jwt-decode/npm-release.yml/main?enable=pin
Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/semgrep.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/auth0/jwt-decode/semgrep.yml/main?enable=pin
Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/snyk.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/auth0/jwt-decode/snyk.yml/main?enable=pin
Info: 0 out of 5 GitHub-owned GitHubAction dependencies pinned
Info: 1 out of 1 third-party GitHubAction dependencies pinned
Info: 1 out of 1 npmCommand dependencies pinned
Reason
5 existing vulnerabilities detected
Details
Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Info: topLevel 'contents' permission set to 'read': .github/workflows/build.yml:11
Warn: no topLevel permission defined: .github/workflows/npm-release.yml:1
Warn: topLevel 'contents' permission set to 'write': .github/workflows/release.yml:10
Warn: no topLevel permission defined: .github/workflows/semgrep.yml:1
Info: topLevel 'contents' permission set to 'read': .github/workflows/snyk.yml:17
Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Warn: no fuzzer integrations found
Score
6.1
/10
Last Scanned on 2024-12-02
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.