Skip to content

What This Adds In Cloud Code

  • Fast token issue/verify flow without external dependency setup.
  • Consistent defaults (HS256, typ: JWT).
  • Useful for signed links, short-lived API access, and callback state.

Quick Start

js
const JWT = require('jwt');
const token = JWT.encode({ customer_id: 'cus_123' }, Nimbu.Site.env.get('JWT_SECRET'));

API

FunctionParamsReturnsNotes
encode(payload, secret, algorithm, headerFields)object/string paramsstring | nullReturns token; null on encode failure.
decode(token, secret, verify, options)token + optionsobject | { error: string }Returns payload object or error object.

Practical Example

js
const JWT = require('jwt');

const payload = JWT.decode(request.params.token, Nimbu.Site.env.get('JWT_SECRET'), true);
if (payload.error) {
  return response.error('token', payload.error);
}

response.success({ customerId: payload.customer_id });

Failure Modes & Gotchas

  • Decode returns { error } instead of throwing.
  • Validate expiry claims (exp) in your logic.