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(), "251500495731950080"), ts: 1576108413380000, instance: Ref(Collection("users"), "251407645221585408"), secret: 'fnEDfYJeTPACAAN9IwrU8AIAItH5Pfj5cqbybb_JmqNOncUKI14' }
You can also create a token directly, without knowing the credentials for an identity document:
{ ref: Ref(Tokens(), "293497693263626752"), ts: 1616160062000000, instance: Ref(Collection("users"), "1"), secret: 'fnEEEraY1WACAAQStphjIAYALk1agYtuTK3v_x8KTSs7bpe2scg' }
When you create a token directly, you can also set its
ttl
(time to live):{ ref: Ref(Tokens(), "294155374728053248"), ts: 1616787275910000, instance: Ref(Collection("users"), "1"), ttl: Time("2021-03-28T19:34:35.900Z"), secret: 'fnEEFQzBO3ACAAQVDMDIIAYAvxbzRoRT6Tax-k8N9fGhR2ledDk' }
-
List existing tokens
{ data: [ Ref(Tokens(), "255391018093904394") ] }
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: 1592112574490000, 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(), "251500495731950080"), ts: 1576108413460000, instance: Ref(Collection("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!