Identify Sign Up Event in Post Login Action

Problem statement

How can Actions be used to help determine between a user signing up and a user logging in?

Solution

The following Post-Login Action performs some logic in order to understand whether the user is logging in for the first time or not. If this is a first login then some user_metadata is set so this Action will not have to perform any API calls on subsequent logins performed by this user. The logic implemented can vary by use case; in this example, an external database stores a record of the signup transaction and sets a piece of metadata once the user is added to this external database.

const axios = require('axios')
exports.onExecutePostLogin = async (event, api) => {
  // if user has app_metadata and signupProcessed = true, then we don't need to create the user
  if (event.user.app_metadata && event.user.app_metadata.signupProcessed) {
    console.log('User sign up has been processed already')
    return
  }

  try {
    // query app api to store user info
    const { data } = await axios.post('https://yourapi.test/api/user', {
      auth0UserId: event.user.user_id,
      email: event.user.email,
      name: event.user.name,
      picture: event.user.picture
    })
    console.log('User created', data)
    // if everything went well then update signupProcessed to true
    api.user.setAppMetadata('signupProcessed', true)
  } catch (err) {
    console.error(err)
  }
};