Create a sign-in function

This tutorial builds on the basic login feature by defining a function to log in users.

Typically, applications use email and password credentials for signing up new users. Each user is identified by their unique email address so it is important to constrain the People.email field so that it is unique.

  1. In the Fauna Shell, select the Admin Built-in Role.

  2. Add a unique constraint to the People collection email field:

    People.definition.update({
      constraints: [
        {
          unique: [ "email" ],
        }
      ]
    })
    {
      name: "People",
      coll: Collection,
      ts: Time("2023-07-25T22:03:50.820Z"),
      constraints: [
        {
          unique: [
            "email"
          ],
          status: "active"
        }
      ]
    }
  3. Create a user-defined function that signs up users:

    Function.create({
      name: "Signup",
      role: "server",
      body: "(username, email, password) => {
        let user = People.create({ name: username, email: email })
        Credentials.create({ document: user, password: password })
      }"
    })
    {
      name: "Signup",
      coll: Function,
      ts: Time("2023-07-25T15:59:03.950Z"),
      role: "server",
      body: <<-END
        (username, email, password) => {
            let user = People.create({ name: username, email: email })
            Credentials.create({ document: user, password: password })
          }
      END
    }

    When called from the Shell or an application you provide username, email, and password parameters.

  4. The function creates a People document that becomes the identity of the Credential document that the function returns. Verify the Signup function:

    Signup("Alice Jones", "ajones@gmail.com", "sekret")
    {
      id: "371240301830864930",
      coll: Credential,
      ts: Time("2023-07-25T16:06:31.110Z"),
      document: People.byId("371240301825622050")
    }

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!