User signup event in Auth0 Lock

2019-05-02 23:35发布

问题:

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?

回答1:

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)