Tokens

Copied!
( [database] )

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.

A token’s instance field contains a Reference to the associated identity document. See Tokens for details.

Parameters

Parameter Type Definition and requirements

database

Optional - A Reference to a child database. If not specified, the current database is used.

Returns

An Set Reference for the available Tokens in the specified child database, or the current database if database is not specified.

Examples

  1. Create a token

    The following query lists any existing tokens (up to the pagination limit) in the current database:

    Copied!
    (
      (('users_by_email'), 'alice@site.example.com'),
      { password: 'secret password' },
    )
    {
      ref: ((), "302044099688530432"),
      ts: 1624310550300000,
      instance: (("users"), "1"),
      secret: 'fnEEMROCJHACAAQxE4GIcAIADYV7pzCKqnsAm8N0nsY1vlCMJYQ'
    }
    Query metrics:
    •    bytesIn:  130

    •   bytesOut:  291

    • computeOps:    1

    •    readOps:    1

    •   writeOps:    1

    •  readBytes:  295

    • writeBytes:  414

    •  queryTime: 50ms

    •    retries:    0

    You can also create a token directly, without knowing the credentials for an identity document:

    Copied!
    (
      (),
      { instance: (('users'), '1') },
    )
    {
      ref: ((), "302044084418118144"),
      ts: 1624310535730000,
      instance: (("users"), "1"),
      secret: 'fnEEMRN-lkACAAQxE33O0AYA3fbFMCO_kONC35tJ2tPZIMoJ4Lc'
    }
    Query metrics:
    •    bytesIn:   99

    •   bytesOut:  291

    • computeOps:    1

    •    readOps:    0

    •   writeOps:    1

    •  readBytes:    0

    • writeBytes:  414

    •  queryTime: 39ms

    •    retries:    0

    When you create a token directly, you can also set its ttl (time to live):

    Copied!
    (
      (),
      {
        instance: (('users'), '1'),
        ttl: ((), 2, 'days'),
      },
    )
    {
      ref: ((), "302044087434871296"),
      ts: 1624310538620000,
      instance: (("users"), "1"),
      ttl: ("2021-06-23T21:22:18.607Z"),
      secret: 'fnEEMRN_ShACAAQxE36hQAYAWLgKxjGircfBLeB5z-IGW4eZU_Y'
    }
    Query metrics:
    •    bytesIn:  156

    •   bytesOut:  332

    • computeOps:    1

    •    readOps:    0

    •   writeOps:    1

    •  readBytes:    0

    • writeBytes:  430

    •  queryTime: 46ms

    •    retries:    0

  2. List existing tokens

    Copied!
    (())
    { data: [ ((), "1") ] }
    Query metrics:
    •    bytesIn:  28

    •   bytesOut:  81

    • computeOps:   1

    •    readOps:   8

    •   writeOps:   0

    •  readBytes: 250

    • writeBytes:   0

    •  queryTime: 8ms

    •    retries:   0

    The following query lists the References for any tokens (up to the pagination limit) in a child database named "child_db":

    Copied!
    ((('child_db')))
    { data: [] }
    Query metrics:
    •    bytesIn:   47

    •   bytesOut:   24

    • computeOps:    1

    •    readOps:    8

    •   writeOps:    0

    •  readBytes:  363

    • writeBytes:    0

    •  queryTime: 14ms

    •    retries:    0

  3. 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!
    ({
      name: 'tokens_by_instance',
      permissions: { read: 'public' },
      source: (),
      terms: [ { field: 'instance' } ],
      values: [ { field: ['data', 'name'] } ],
    })
    {
      ref: ("tokens_by_instance"),
      ts: 1624310541660000,
      active: true,
      serialized: true,
      name: 'tokens_by_instance',
      permissions: { read: 'public' },
      source: (),
      terms: [ { field: 'instance' } ],
      values: [ { field: [ 'data', 'name' ] } ],
      partitions: 1
    }
    Query metrics:
    •    bytesIn:   215

    •   bytesOut:   327

    • computeOps:     1

    •    readOps:     0

    •   writeOps:     1

    •  readBytes: 1,364

    • writeBytes:   484

    •  queryTime:  68ms

    •    retries:     0

  4. Modify a token

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

    Copied!
    (
      ((), '1'),
      { data: { meta: 'data' } }
    )
    {
      ref: ((), "1"),
      ts: 1624310552760000,
      instance: (("users"), "1"),
      data: { meta: 'data' }
    }
    Query metrics:
    •    bytesIn:   99

    •   bytesOut:  234

    • computeOps:    1

    •    readOps:    0

    •   writeOps:    1

    •  readBytes:  138

    • writeBytes:  172

    •  queryTime: 63ms

    •    retries:    0

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!