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.

A token’s secret is shown once — when you create the token.

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.

data

Object

User-defined metadata to store supplemental token information.

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: Customer.byId("111")
})
{
  id: "401671202234433613",
  coll: Token,
  ts: Time("2099-06-25T13:32:39.240Z"),
  document: Customer("111"),
  secret: "fn..."
}

See also

See credential.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!