Tokens
Tokens( [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.
Examples
-
Create a token
The following query lists any existing tokens (up to the pagination limit) in the current database:
{ ref: Ref(Tokens(), "302044099688530432"), ts: 1624310550300000, instance: Ref(Collection("users"), "1"), secret: 'fnEEMROCJHACAAQxE4GIcAIADYV7pzCKqnsAm8N0nsY1vlCMJYQ' }
You can also create a token directly, without knowing the credentials for an identity document:
{ ref: Ref(Tokens(), "302044084418118144"), ts: 1624310535730000, instance: Ref(Collection("users"), "1"), secret: 'fnEEMRN-lkACAAQxE33O0AYA3fbFMCO_kONC35tJ2tPZIMoJ4Lc' }
When you create a token directly, you can also set its
ttl
(time to live):{ ref: Ref(Tokens(), "302044087434871296"), ts: 1624310538620000, instance: Ref(Collection("users"), "1"), ttl: Time("2021-06-23T21:22:18.607Z"), secret: 'fnEEMRN_ShACAAQxE36hQAYAWLgKxjGircfBLeB5z-IGW4eZU_Y' }
-
List existing tokens
{ data: [ Ref(Tokens(), "1") ] }
The following query lists the References for any tokens (up to the pagination limit) in a child database named "child_db":
{ data: [] }
-
Use
Tokens()
in an indexThe following query creates an index on the internal tokens collection, so that tokens associated with a specific service can be queried:
{ ref: Index("tokens_by_instance"), ts: 1624310541660000, active: true, serialized: true, name: 'tokens_by_instance', permissions: { read: 'public' }, source: Tokens(), terms: [ { field: 'instance' } ], values: [ { field: [ 'data', 'name' ] } ], partitions: 1 }
-
Modify a token
In the following query, we use an existing token’s reference to update the token with some metadata:
{ ref: Ref(Tokens(), "1"), ts: 1624310552760000, instance: Ref(Collection("users"), "1"), 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!