JWT - 401-advanced-javascript-v/seattle-javascript-401d29 GitHub Wiki

jwt.sign(payload, secretOrPrivateKey, [options, callback])

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, buffer, or object containing either the secret for HMAC algorithms or the PEM encoded private key for RSA and ECDSA.

options:

  • algorithm (default: HS256)
  • expiresIn: expressed in seconds or a string describing a time span zeit/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 zeit/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.

Token Expiration (exp claim)

The standard for JWT defines an exp claim for expiration. The expiration is represented as a NumericDate

jwt.verify(token, secretOrPublicKey, [options, callback])

  • token is the JsonWebToken string
  • secretOrPublicKey is a string or buffer containing either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA. If jwt.verify is called asynchronous, secretOrPublicKey can be a function that should fetch the secret or public key. See below for a detailed example