Tokens

Copied!
( )

Description

The Tokens function provides access to the system collection that stores access tokens, created with the Login function. This is useful for:

  • Listing currently existing tokens

  • Creating tokens without credentials

  • Indexing tokens so that you can query tokens based on relationships with other documents.

If you have a reference to a specific token, acquired with Login, you can add custom fields to the token’s document using Update or Replace. Access tokens can be invalidated by calling Logout.

Due to implementation details, Paginate(Tokens()) does not list existing tokens. See the example below.

Parameters

None.

Returns

None.

Examples

  1. List existing tokens

    The following query lists any existing tokens:

    Copied!
    client.query(
      q.(q.(q.()))
    )
    .then((ret) => console.log(ret))
    { data: [ ((), "255391018093904394") ] }
  2. Use Tokens() in an index

    The following query creates an index on the internal tokens collection, so that tokens associated with a specific service can be queried:

    Copied!
    client.query(
      q.({
        name: 'tokens_by_instance',
        permissions: { read: 'public' },
        source: q.(),
        terms: [ { field: 'instance' } ],
        values: [ { field: ['data', 'name'] } ],
      })
    )
    .then((ret) => console.log(ret))
    { ref: ("tokens_by_instance"),
      ts: 1576104922970000,
      active: false,
      serialized: true,
      name: 'tokens_by_instance',
      permissions: { read: 'public' },
      source: (),
      terms: [ { field: 'instance' } ],
      values: [ { field: [Array] } ],
      partitions: 1 }
  3. Modify a token

    The following query generates a token by calling Login():

    Copied!
    client.query(
      q.(
        q.(q.('users_by_email'), 'alice@site.example'),
        { password: 'new password' },
      )
    )
    .then((ret) => console.log(ret))
    { ref: ((), "251500495731950080"),
      ts: 1576108413380000,
      instance: (("users"), "251407645221585408"),
      secret: 'fnEDfYJeTPACAAN9IwrU8AIAItH5Pfj5cqbybb_JmqNOncUKI14' }

    In the following query, we use the token’s reference, from the previous query, to update the token with some metadata:

    Copied!
    client.query(
      q.(
        q.(q.(), '251500495731950080'),
        { data: { meta: 'data' } }
      )
    )
    .then((ret) => console.log(ret))
    { ref: ((), "251500495731950080"),
      ts: 1576108413460000,
      instance: (("users"), "251407645221585408"),
      data: { meta: 'data' } }

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!