Signing In a User Automatically after Sign-Up

Last edit: Aug 16, 2019
  • Contributors:
  • pavelloz
  • Slashek

This guide will help you add automatic sign in to a sign-up form so that a new user will be logged in right after account creation.

Requirements

This tutorial assumes that you already have a working sign up form, where a user can create a new account.

Steps

Automatic sign in after sign up is a three-step process:

Step 1: Add callback_actions key to Sign up form

In your sign_up form configuration add callback_actions at the end (order doesn't matter) of the YML definition.

app/forms/sign_up.liquid

---
...
callback_actions: "{% graphql res = 'user_session_create', email: @form.email, password: @form.password %}"
---

This code snippet executes the user_session_create query, so you have to create it.

Step 2: Create user_session_create GraphQL mutation

This mutation takes two obligatory arguments: email and password – both strings.

app/graphql/user_session_create.graphql
mutation user_session_create($email: String!, $password: String!) {
  user_session_create(
    form_name: "session_create_form" # must match with `name` of your form
    email: $email
    password: $password
  ) {
    email
  }
}

This calls the session_create_form with the same arguments you received from the callback_actions.

Step 3: Create session_create_form form

SessionForm is supported by the server, so you don't need to define anything – the server will take care of handling user credentials and authenticating the user.

app/forms/session_create_form.liquid
---
name: session_create_form
resource: Session
---

Next steps

Congratulations! You have set up automatic user login after sign up. You can now learn about logging out the user:

Questions?

We are always happy to help with any questions you may have.