Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User signup event in Auth0 Lock

The 'authenticated' event is emitted after a successful authentication.

lock.on('authenticated', function(authResult) { });

But is there any way to detect when a new user signs up to your application or do I have to store the user in my database and check it each time a user authenticates?

like image 299
Joe Avatar asked Oct 25 '16 21:10

Joe


1 Answers

The Auth0 Lock does not trigger a specific event for user signup.

You can however detect this on a custom rule and enrich the user profile with this metadata. There's a signup sample rule that illustrates this possibility

function (user, context, callback) {
    user.app_metadata = user.app_metadata || {};

    // short-circuit if the user signed up already
    if (user.app_metadata.signed_up) return callback(null, user, context);

    // execute first time login/signup logic here
    // ...

    // update application metadata so that signup logic is skipped on subsequent logins
    user.app_metadata.signed_up = true;
    auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
        .then(function () {
            callback(null, user, context);
        })
        .catch(function (err) {
            callback(err);
        });
}

This uses app_metadata to store information associated to the user so that you can keep track for which users you already executed their additional first-time signup logic.

Have in mind that rules will execute on the server-side of the authentication pipeline, so if the logic you want to implement requires user interaction you could achieve something similar by doing these set of steps:

  1. Upon login to the application get the user profile
  2. If there's no flag set assume the user just signed up and do your custom logic
  3. After doing your custom logic update the user app_metadata to set a signup flag (you can do this on your server-side application logic through Auth0 Management API)
like image 143
João Angelo Avatar answered Sep 28 '22 16:09

João Angelo