Credentials
Static authentication
NatsConnectionConfig.auth supports userpass, token, and decentralised. Omit it for an unauthenticated development broker.
auth: { type: 'token', token: sessionToken }
// or:
auth: { type: 'userpass', user: username, pass: password }
// or sentinel credentials combined with user/password:
auth: { type: 'decentralised', sentinelB64, user: username, pass: password }The live demo accepts an optional token, keeps it in memory, and does not save it to local storage or show it in actor details. Browser code cannot hide a credential from the person running that browser; use credentials scoped to the subjects and operations required.
Short-lived credentials
Provide an application-owned adapter when credentials expire. The library does not depend on an identity provider.
actor.send({
type: 'CONFIGURE',
config: {
opts: { servers: ['wss://your-nats.example'] },
maxRetries: 3,
credentials: {
refreshBeforeExpiryMs: 60_000,
operationTimeoutMs: 30_000,
adapter: {
load: () => loadSessionCredentials(),
refresh: (current) => refreshSessionCredentials(current),
},
},
},
})loadSessionCredentials and refreshSessionCredentials are functions supplied by your application. They return NatsCredentials: optional auth, optional NATS requestHeaders, and optional expiresAt in epoch milliseconds. Use the earliest expiry of credentials contained in that value.
The child credential actor shares concurrent loads, bounds adapter calls, and refreshes before expiry. Refresh results must leave more time than the configured refresh window. NATS reconnect authentication reads the latest cached connection credential synchronously. Requests await current credential headers before sending.
If loading/refresh fails or credentials expire without refresh, requests fail closed and an active connection is closed. Repair the application session, send CREDENTIALS.RELOAD, wait for credentials to become ready, and then send CONNECT. Credentials can also be managed directly through the exported credentialMachine and getCredentials.