Is there a Post User Registration hook for social connections?

The docs indicate that the Post User Registration hook is only for Database Connections.

I would like to be able to send an API request to my backend to add the user_id to my API database, but my app is only using a social connection (GitHub in this case).

Is there any functionality that resembles the post-user-registration hook, but for social connections?

Thanks!

2 Likes

I think I figured it out. In short, “no” there is no post-user-registration hook for social connections.

However, it’s possible to get roughly the same functionality with rules. It is possible to require 3rd-party NPM packages in Auth0 rules. Since rules are executed every time a person logs in (as opposed to only after registration with hooks), you have to store a flag somewhere in the user’s profile to allow the rule to short-circuit the API call after the first attempt. My implementation looks something like this:

function (user, context, callback) {
  // check app_metadata; is user NOT already added to the API DB?
  if (user.app_metadata && !user.app_metadata.added_to_api) {
    // no, they haven't been added, so...
    // load a library for making HTTP requests (I like axios)
    const axios = require('axios');

    // construct the URL and POST body of our API request
    const url = 'https://api.example.com';
    const body = { user_id: user.user_id };

    // make our API call to add the user
    axios.post(url, body, {headers: {'content-type': 'application/json'}})
      .then(() => {
        // success!! update app_metadata with a flag to record this
        user.app_metadata.added_to_api = true;
        return auth0.users.updateAppMetadata(user.user_id, user.app_metadata);
      })
      .then(() => {
        // keep calm and carry on...
        callback(null, user, context);
      })
      .catch(() => {
        // ruh-roh! API request failed; record this in app_metadata
        user.app_metadata.added_to_api = false;
        return auth0.users.updateAppMetadata(user.user_id, user.app_metadata);
      })
      .then(() => {
        // keep calm and carry on...
        callback(null, user, context);
      });
  } else {
    // they were already added to the API; carry on...
    callback(null, user, context);
  }
}

Comparing Rules to Post-User-Registration-Hooks

  • Frequency:
    • The post-user-registration hook only runs once after the user has registered.
    • Rules run every time a user logs in.
  • Record-keeping:
    • You don’t have to keep track of whether or not the post-user-registration hook has run or not.
    • To prevent your rule from making an API call on every login, you have to set a flag in the app_metadata.
  • Extensibility:
  • Availability:
    • Post-user-registration hooks are only available for Database Connections.
    • Rules can be used for any kind of connection, and are the only option for running code after logins with purely Social Connections.
  • Debugging:
    • Hooks use the webtask editor and have a really robust interface for testing your code right in the browser.
    • Rules allow you to do some testing, but you don’t get quite the same level of information, e.g. I wasn’t able to get the actual HTTP response info like I was when I tried implementing my rule as a hook.

All-in-all there was a little bit more overhead and work involved in using a Rule in place of a Hook, but I was eventually able to accomplish basically what I wanted.

It would be nice if hooks were enabled/available for Social Connections, but now that I understand how to do it, I can probably make do without them.

HTH,
Morgan

6 Likes

Glad you have figured it out! I’ll make sure to relay your feedback to appropriate team as we’re working on extending capabilities of hooks!

2 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.