Interface Auth

Creates Ably TokenRequest objects and obtains Ably Tokens from Ably to subsequently issue to less trusted clients.

Hierarchy

  • Auth

Properties

clientId: string

The client ID this client is identified as when publishing messages or entering presence.

The value is resolved from the clientId in ClientOptions, or from the clientId in the token the client authenticated with. A conflict between the two raises an ErrorInfo.

The value is unset for an anonymous client, for example a key-authenticated client with no clientId configured. A populated value makes this an identified client. Guard against an unset value despite the declared type.

Methods

  • Instructs the library to get a new token immediately.

    On a realtime client it re-authenticates a connection that is already in the connected state, and otherwise starts or restarts the connection. The returned promise resolves only once the new token has taken effect on a connection in the connected state. It rejects with an ErrorInfo if re-authentication fails or the connection cannot be established.

    The client must have a way to obtain a token, so the resolved AuthOptions must include one of the token authentication mechanisms authCallback, authUrl, or key, or a token supplied directly. Without any of these the call rejects with an ErrorInfo.

    authorize() cannot change the API key, so passing an authOptions.key that differs from the one the client was constructed with is rejected with an ErrorInfo.

    Any TokenParams and AuthOptions passed in are stored as the new defaults for subsequent token requests. They replace, rather than merge with, the stored defaults.

    Parameters

    Returns Promise<TokenDetails>

    A promise which, upon success, will be fulfilled with a TokenDetails object. Upon failure, the promise will be rejected with an ErrorInfo object which explains the error.

    Example

    const tokenDetails = await realtime.auth.authorize({ clientId: 'bob' });
    

    See

    https://ably.com/docs/pub-sub/api/javascript/realtime/auth#authorize

  • Parameters

    Returns void

    Deprecated

    v1 callback signature — no longer supported. Use realtime.auth.authorize(tokenParams) and await the returned promise. See the v2 migration guide.

    Example

    // v1 (no longer supported — IDE shows this with strikethrough):
    realtime.auth.authorize(tokenParams, authOptions, (err, token) => {});

    // v2:
    const token = await realtime.auth.authorize(tokenParams, authOptions);
  • Obtains an Ably Token according to the specified TokenParams and AuthOptions.

    Both TokenParams and AuthOptions are optional. When omitted or null, the client's stored defaults are used, as specified at instantiation or later updated by an authorize() request. Any values passed in replace, rather than merge with, those defaults.

    The client must have a way to obtain a token, so the resolved AuthOptions must include one of the token authentication mechanisms authCallback, authUrl, or key. A client given only a literal token cannot request a new one and the call rejects with an ErrorInfo.

    Parameters

    Returns Promise<TokenDetails>

    A promise which, upon success, will be fulfilled with a TokenDetails object. Upon failure, the promise will be rejected with an ErrorInfo object which explains the error.

    Example

    const tokenDetails = await realtime.auth.requestToken({ clientId: 'bob' });
    

    See

    https://ably.com/docs/pub-sub/api/javascript/realtime/auth#request-token

  • Parameters

    Returns void

    Deprecated

    v1 callback signature — no longer supported. Use realtime.auth.requestToken(tokenParams) and await the returned promise. See the v2 migration guide.

    Example

    // v1 (no longer supported — IDE shows this with strikethrough):
    realtime.auth.requestToken(tokenParams, authOptions, (err, token) => {});

    // v2:
    const token = await realtime.auth.requestToken(tokenParams, authOptions);

Generated using TypeDoc