Token.create()

Create a token.

Signature

Token.create(data: {*: Any}): Token

Description

The Token.create() method creates a token with the provided data. This method is used when identity-based access is required, but authentication is not needed or handled outside of Fauna.

By default, a token grants access to the current database. If you provide an optional database field, the token is associated with and provides access to that database. The document field controls authorized access to database resources.

This method returns a secret that must be extracted from the result and saved. Fauna can’t recover a secret that is discarded or lost.

Applications include the secret as a bearer token in subsequent queries. A token secret can be used in multiple queries until its token becomes invalid or is deleted.

Optionally, set a time-to-live (ttl) value to define the token duration. The default value is null, which persists the token indefinitely or until it is deleted.

Parameters

Parameter Type Required Description

data

Object

Object describing the token.

data fields

Name Type Required Description

document

String

Yes

Document associated with the token and used to authorize queries.

secret

String

Token secret, this is a password-equivalent value.

Database creation time is the only time the secret is displayed. If the secret is discarded or lost, it can’t be recovered so make sure to copy and save the secret to a password manager or other safe location.

database

String

Name of an existing child database. If omitted, the new token grants access to the current database.

data

Object

User-defined metadata to store supplemental token information.

name

String

Name of the token differentiating this token from other tokens. If provided, this field must exist in the data field.

ttl

Time

Timestamp indicating a document lifespan. When the ttl is reached, Fauna removes it. If ttl isn’t set, its default value is null, which causes the document to persist indefinitely or until deleted.

Return value

Type Description

Token

New Token document.

At token creation, all document fields are returned. Subsequent queries for the token return the following fields only:

  • id

  • coll

Examples

Token.create({
  document: People.byId("372696914650464289")
})
{
  id: "373081653342371873",
  coll: Token,
  ts: Time("2023-08-14T23:54:00.750Z"),
  document: People.byId("372696914650464289"),
  secret: "fnEFLXPJe7AAIQUh3mNk4AAhp6iMjCB6XHC44_eP3E1Tr9HXGcQ"
}

See also

See login() for an alternative way to create a credential.

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!