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.
-
In the Fauna Shell, select the Admin Built-in Role.
-
Add a unique constraint to the
People
collectionemail
field:{ name: "People", coll: Collection, ts: Time("2023-07-25T22:03:50.820Z"), constraints: [ { unique: [ "email" ], status: "active" } ] }
-
Create a user-defined function that signs up users:
{ 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
, andpassword
parameters. -
The function creates a
People
document that becomes the identity of theCredential
document that the function returns. Verify theSignup
function:{ 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!