encodeToken()function

encodeToken({ nbf = "now", iat = "now", exp = DAY * 30, ...claims }: TokenClaims, secret: PossibleBytes): Promise<string>
ParamType
claimsTokenClaims
The payload claims to include in the JWT, including optional iat, nbf, and exp values. required
    .iatPossibleDate
"Issued at" date (defaults to "now").
- Not used for validation, but always set in the token payload.
- Can be used to determine when the token was issued, and possibly revoke tokens issued before a certain date. readonly
    .nbfPossibleDate
"Not before" date.
- When validating the token, tokens before this date will be rejected readonly
    .expnumber
Expiry in milliseconds (defaults to "30 days").
- When validating the token, tokens after this date will be rejected readonly
secretPossibleBytes
The secret key to sign the JWT with (minimum 64 bytes / 512 bits). required
Return
Promise<string>
A promise resolving to the signed JWT string token.
Throws
unknown
ValueError If the secret is not a byte sequence of at least 64 bytes.

Encode a JWT and return the string token.

  • Currently only supports HMAC SHA-512 signing.
  • The exp claim defaults to 30 days from now, and iat / nbf default to "now".

Examples

const token = await encodeToken({ sub: "user1" }, secret)