auth0 /node-jsonwebtoken
JsonWebToken implementation for node.js http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html
★ 18,183⑂ 1,294👁 246⑃ 341● 661▤ 0
TA的项目
+0
日增长
18,183
stars
1,294
forks
246
watchers
341
prs
661
issues
0
releases
README
jsonwebtoken
| Build | Dependency |
|-----------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|
| | |
An implementation of JSON Web Tokens.
This was developed against draft-ietf-oauth-json-web-token-08. It makes use of node-jws
Install
bash
$ npm install jsonwebtoken
Migration notes
From v8 to v9
From v7 to v8
Usage
jwt.sign(payload, secretOrPrivateKey, [options, callback])
(Asynchronous) If a callback is supplied, the callback is called with the err or the JWT.
(Synchronous) Returns the JsonWebToken as string
payload could be an object literal, buffer or string representing valid JSON.
Please _note_ that exp or any other claim is only set if the payload is an object literal. Buffer or string payloads are not checked for JSON validity.
If payload is not a buffer or a string, it will be coerced into a string using JSON.stringify.
secretOrPrivateKey is a string (utf-8 encoded), buffer, object, or KeyObject containing either the secret for HMAC algorithms or the PEM
encoded private key for RSA and ECDSA. In case of a private key with passphrase an object { key, passphrase } can be used (based on crypto documentation), in this case be sure you pass the algorithm option.
When signing with RSA algorithms the minimum modulus length is 2048 except when the allowInsecureKeySizes option is set to true. Private keys below this size will be rejected with an error.
options:
algorithm (default: HS256)
expiresIn: expressed in seconds or a string describing a time span vercel/ms.
Eg: 60, "2 days", "10h", "7d". A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), otherwise milliseconds unit is used by default ("120" is equal to "120ms").
notBefore: expressed in seconds or a string describing a time span vercel/ms.
Eg: 60, "2 days", "10h", "7d". A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), otherwise milliseconds unit is used by default ("120" is equal to "120ms").
audience
issuer
jwtid
subject
noTimestamp
header
keyid
mutatePayload: if true, the sign function will modify the payload object directly. This is useful if you need a raw reference to the payload after claims have been applied to it but before it has been encoded into a token.
allowInsecureKeySizes: if true allows private keys with a modulus below 2048 to be used for RSA
allowInvalidAsymmetricKeyTypes: if true, allows asymmetric keys which do not match the specified algorithm. ThisTop issues
#111Return a Promise58 comments · 2015-07-16#992Node 24 compatibility23 comments · 2025-05-07#785can't import jsonwebtoken in modern typescript esm app18 comments · 2021-07-22#939TypeError: Right hand side of instanceof is not an object15 comments · 2023-09-10#655Please add ES6 module/ESM support14 comments · 2020-01-10#950TypeError: invalid 'instanceof' operand KeyObject13 comments · 2023-12-02#898Error: secretOrPrivateKey must be an asymmetric key when using ES25613 comments · 2023-03-31#863Version 9.0.0 - `sign` function throws error on frontend (in browser)11 comments · 2022-12-22