SDK
SDK Javascript v7.x
2

refreshToken #

Available since 6.1.0

Refreshes a valid, non-expired authentication token.

If this action is successful, then the jwt property of this class instance is set to the new authentication token.

All further requests emitted by this SDK instance will be on behalf of the authenticated user, until either the authenticated token expires, the logout action is called, or the jwt property is manually set to another value.

Arguments #

refreshToken ([options])

ArgumentsTypeDescription
options
object
Query options

options #

Additional query options

PropertyType
(default)
Description
expiresIn
string
Expiration time in ms library format. (e.g. 2h)
queuable
boolean

(true)
If true, queues the request during downtime, until connected to Kuzzle again
timeout
number

(-1)
Time (in ms) during which a request will still be waited to be resolved. Set it -1 if you want to wait indefinitely
triggerEvents
boolean

(false)
If set to true, will trigger events even if using Embeded SDK. You should always ensure that your events/pipes does not create an infinite loop.
Available since Kuzzle 2.31.0

expiresIn #

The default value for the expiresIn option is defined at server level, in Kuzzle's configuration file.

Resolves #

The refreshToken action resolves to a token object with the following properties:

PropertyTypeDescription
_id
string
User unique identifier (kuid)
expiresAt
number
Expiration timestamp in Epoch-millis format (UTC)
jwt
string
Authentication token (returned only if the option cookieAuth is not enabled in the SDK, otherwise stored in an http cookie)
ttl
number
Time to live of the authentication token, in milliseconds

Usage #

const credentials = { username: 'foo', password: 'bar' };
try {
  const jwt = await kuzzle.auth.login('local', credentials);
  // Prints the encrypted authentication token
  console.log(jwt);
  // Note: to get a different token, you actually need to wait at least
  // 1 second. Otherwise you do receive a refreshed token, but with the exact
  // same caracteristics, as the key depends on the timestamp in Epoch format
  await new Promise(resolve => setTimeout(resolve, 1000));
  // Prints:
  // { _id: '<user kuid>',
  //   jwt: '<a different encrypted authentication token>'
  //   expiresAt: 1553185334220,
  //   ttl: 3600000 }
  console.log(await kuzzle.auth.refreshToken());
} catch (error) {
  console.error(error.message);
}