encodeToken()function
encodeToken({ nbf = "now", iat = "now", exp = DAY * 30, ...claims }: TokenClaims, secret: PossibleBytes): Promise<string>| Param | Type | |
|---|---|---|
claims | TokenClaims | The payload claims to include in the JWT, including optional iat, nbf, and exp values. required |
.iat | PossibleDate | "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 |
.nbf | PossibleDate | "Not before" date. - When validating the token, tokens before this date will be rejected readonly |
.exp | number | Expiry in milliseconds (defaults to "30 days"). - When validating the token, tokens after this date will be rejected readonly |
secret | PossibleBytes | 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
expclaim defaults to 30 days from now, andiat/nbfdefault to "now".
Examples
const token = await encodeToken({ sub: "user1" }, secret)