CurrentToken

Copied!
()

Description

The CurrentToken function returns information about the active authentication:

  • When a token is used, a Reference to the token document is returned.

  • When a key is used, and the key exists in the current database (and not in a parent database), a Reference to the key document is returned.

  • When authentication is performed by an identity provider, an object containing the active JWT’s claims is returned.

For all other cases, CurrentToken emits an error. These cases include:

  • Authentication uses the "root" Fauna secret.

  • Authentication uses a key that has the database field set.

  • Authentication uses a key that exists in a parent database, for example, when using a scoped key.

Parameters

None.

Returns

For tokens, and keys defined in the current database, a Reference to the associated token/key.

For JWTs, an object containing the claims from the active JWT.

For connections using the "root" Fauna key, or a key that has the database field defined, an error is returned.

Examples

  1. The following query runs in the context of a Fauna token:

    Copied!
    client.query(
      q.()
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    ((), "1")
    Query metrics:
    •    bytesIn:  22

    •   bytesOut:  70

    • computeOps:   1

    •    readOps:   0

    •   writeOps:   0

    •  readBytes:  96

    • writeBytes:   0

    •  queryTime: 7ms

    •    retries:   0

  2. The following query runs in the context of a Fauna key:

    Copied!
    client.query(
      q.()
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    ((), "1")
    Query metrics:
    •    bytesIn:    22

    •   bytesOut:    68

    • computeOps:     1

    •    readOps:     0

    •   writeOps:     0

    •  readBytes:     0

    • writeBytes:     0

    •  queryTime: 343ms

    •    retries:     0

  3. The following query runs in the context of a Fauna key defined in a parent database:

    Copied!
    client.query(
      q.()
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    Error: [BadRequest] invalid token: Token metadata is not accessible.
    Query metrics:
    •    bytesIn:  22

    •   bytesOut: 101

    • computeOps:   1

    •    readOps:   0

    •   writeOps:   0

    •  readBytes:   0

    • writeBytes:   0

    •  queryTime: 2ms

    •    retries:   0

  4. The following query runs in the context of a JWT token:

    Copied!
    client.query(
      q.()
    )
    .then((ret) => console.log(ret))
    .catch((err) => console.error(
      'Error: [%s] %s: %s',
      err.name,
      err.message,
      err.errors()[0].description,
    ))
    {
      iss: 'https://dev--nozpv3z.us.auth0.com/',
      sub: 'l6vJS8QvHC2LmiGRaOTiE16givuufR22@clients',
      aud: 'https://db.fauna.com/db/yxxf5x9w1ybyn',
      iat: 1611084659,
      exp: 1611171059,
      azp: 'l6vJS8QvHC2LmiGRaOTiE16givuufR22',
      gty: 'client-credentials'
    }
    Query metrics:
    •    bytesIn:  22

    •   bytesOut: 254

    • computeOps:   1

    •    readOps:   0

    •   writeOps:   0

    •  readBytes:   0

    • writeBytes:   0

    •  queryTime: 1ms

    •    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!