Token.create()

Create a token.

Signature

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

Description

This method creates a token based on the data parameters. Use the Token.create() method when identity-based access is required, but authentication is unnecessary 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 authorization to database resources. When a query is made with the token, Fauna matches the document collection to a role membership for authorization. If none match, the query is not authorized.

When a token is created, Fauna returns a secret that is a password equivalent. A caller must retrieve the secret from the result and store it. Fauna cannot 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.

The Credential.login() method creates a token when it authenticates an identity. Query authorization is accomplished using the privileges associated with the credentials role provided with the method.

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

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.

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"
}

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!