Tokens
Tokens( )
ScopedTokens( database )
Tokens( [database] )
tokens( [database] )
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.
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 |
---|---|---|
|
Reference |
Optional - A Reference to a child database. If not specified, the current database is used. A Reference to a child database. Optional - A Reference to a child database. If not specified, the current database is used. Optional - A Reference to a child database. If not specified, the current database is used. Optional - A Reference to a child database. If not specified, the current database is used. Optional - A Reference to a child database. If not specified, the current database is used. 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
-
Create a token
The following query lists any existing tokens (up to the pagination limit) in the current database:
map[instance:{1 0xc00008e510 0xc00008e510 <nil>} ref:{280481975565812224 0xc00008e360 0xc00008e360 <nil>} secret:fnED5Hje54ACAAPkeN55YAYAIveL6bdkDgZ0-2-Eng567vL_H_0 ts:1603747306320000]
{ ref: Ref(Tokens(), "251500495731950080"), ts: 1576108413380000, instance: Ref(Collection("users"), "251407645221585408"), secret: 'fnEDfYJeTPACAAN9IwrU8AIAItH5Pfj5cqbybb_JmqNOncUKI14' }
{'ref': Ref(id=251502661795316224, collection=Ref(id=tokens)), 'ts': 1576110479070000, 'instance': Ref(id=251407645221585408, collection=Ref(id=users, collection=Ref(id=collections))), 'secret': 'fnEDfYRWoGACAAN9IwrU8AIAFwnfIh4vM_28LzJqFIP_45o8X2Q'}
{ 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:
map[instance:{1 0xc00014c270 0xc00014c270 <nil>} ref:{293498065541661184 0xc00014c0c0 0xc00014c0c0 <nil>} secret:fnEEErbvguACAAQStu7jYAIAEIJ3Z3UMa7-cr_-6IVyErFg9ua4 ts:1616160417020000]
{ ref: Ref(Tokens(), "293497693263626752"), ts: 1616160062000000, instance: Ref(Collection("users"), "1"), secret: 'fnEEEraY1WACAAQStphjIAYALk1agYtuTK3v_x8KTSs7bpe2scg' }
{'ref': Ref(id=293498079059902976, collection=Ref(id=tokens)), 'ts': 1616160429920000, 'instance': Ref(id=1, collection=Ref(id=users, collection=Ref(id=collections))), 'secret': 'fnEEErbyqKACAAQStvIo8AYA8p8sN07-yrVgDzXke1NWRgnqxCc'}
{ 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):map[instance:{1 0xc000184660 0xc000184660 <nil>} ref:{294155364114366976 0xc0001844b0 0xc0001844b0 <nil>} secret:fnEEFQy-wtACAAQVDL37gAIADRiIr6AEwLSUV0A7XJJCKJO32x8 ts:1616787265790000 ttl:{780000000 63752556865 <nil>}]
{ ref: Ref(Tokens(), "294155374728053248"), ts: 1616787275910000, instance: Ref(Collection("users"), "1"), ttl: Time("2021-03-28T19:34:35.900Z"), secret: 'fnEEFQzBO3ACAAQVDMDIIAYAvxbzRoRT6Tax-k8N9fGhR2ledDk' }
{'ref': Ref(id=294155377506779648, collection=Ref(id=tokens)), 'ts': 1616787278560000, 'instance': Ref(id=1, collection=Ref(id=users, collection=Ref(id=collections))), 'ttl': FaunaTime('2021-03-28T19:34:38.550Z'), 'secret': 'fnEEFQzB4RACAAQVDMFlQAYAGniNtprOy79A7nWPQ8TVJfD-6zg'}
{ 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
map[data:[{1 0xc0000b7230 0xc0000b7230 <nil>}]]
{ data: [ Ref(Tokens(), "255391018093904394") ] }
{'data': [Ref(id=1, collection=Ref(id=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":
map[data:[]]
{ data: [] }
{'data': []}
{ 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:
map[active:true name:tokens_by_instance partitions:1 permissions:map[read:public] ref:{tokens_by_instance 0xc00008e210 0xc00008e210 <nil>} serialized:true source:0x15326c0 terms:[map[field:instance]] ts:1603747302540000 values:[map[field:[data name]]]]
{ 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 }
{'ref': Ref(id=tokens_by_instance, collection=Ref(id=indexes)), 'ts': 1592867275570000, 'active': True, 'serialized': True, 'name': 'tokens_by_instance', 'permissions': {'read': 'public'}, 'source': Ref(id=tokens), 'terms': [{'field': 'instance'}], 'values': [{'field': ['data', 'name']}], 'partitions': 1}
{ 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:
map[data:map[meta:data] instance:{1 0xc000174270 0xc000174270 <nil>} ref:{1 0xc0001740c0 0xc0001740c0 <nil>} ts:1603747308150000]
{ ref: Ref(Tokens(), "251500495731950080"), ts: 1576108413460000, instance: Ref(Collection("users"), "251407645221585408"), data: { meta: 'data' } }
{'ref': Ref(id=251502661795316224, collection=Ref(id=tokens)), 'ts': 1576110479090000, 'instance': Ref(id=251407645221585408, collection=Ref(id=users, collection=Ref(id=collections))), 'data': {'meta': 'data'}}
{ 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!