Hooks for Social Connections

I am using Hooks for getting a token from Twilio and storing it into user_metadata on Pre User Registration Hook. I also use the standard database for users.

When user is created via Social Connection (in my case it’s Google) the Pre User Registration Hook is not getting triggered. And I get it, I saw comments on the forum that hooks are not supported for Social Connections due to the database limitation. However, here is my point:

When I register a new via Google + standard database, and I go to the admin panel->users I perfectly see that the standard database has an entity there for a user that was created via social connection. And on top of this I can manually modify app_metadata and user_metadata right on the web and save it. That means that Auth0 is still able to create a separate user object in their standard database that has the same functionality as users created via email.

I don’t quite understand why hooks are not enabled with Social Connections.

Also, wanted to ask if somebody know a workaround via Rules and how to use them properly with my hook example (every time a new user registers I pull a create a sub account in twilio and get twill token +metadata and put it into user_metadata)

Hooks code

/**
@param {object} user - The user being created
@param {string} user.tenant - Auth0 tenant name
@param {string} user.username - user name
@param {string} user.password - user’s password
@param {string} user.email - email
@param {boolean} user.emailVerified - is e-mail verified?
@param {string} user.phoneNumber - phone number
@param {boolean} user.phoneNumberVerified - is phone number verified?
@param {object} context - Auth0 connection and other context info
@param {string} context.requestLanguage - language of the client agent
@param {object} context.connection - information about the Auth0 connection
@param {object} context.connection.id - connection id
@param {object} context.connection.name - connection name
@param {object} context.connection.tenant - connection tenant
@param {object} context.webtask - webtask context
@param {function} cb - function (error, response)
*/
module.exports = function (user, context, cb) {
// Perform any asynchronous actions, e.g. send notification to Slack.
var responseObject = {};
responseObject.user = user;

const axios = require(“axios”);
const url = ‘https://api.twilio.com/2010-04-01/Accounts.json’;
const headers = {
‘Content-Type’: ‘application/x-www-form-urlencoded’,
}
const auth = {
username: ‘',
password: '
*’,
}

const data = { FriendlyName: user.email }
axios.post(url, data, { headers, auth })
.then(function (response) {
console.log("response.data: “, response);
// Add user or app metadata to the newly created user
responseObject.user.user_metadata = response.data;
cb(null, responseObject);
})
.catch(function (error) {
console.log(error, " errorerror”);
cb(error, null);
});
};

Thank you for you help and if Auth0 guys read this, please respond with quick explanation why by design I can manually modify user_metadata through admin panel for social login users, but hooks don’t work for Social Connections? And When do you expect to enable it?

2 Likes

Hi @alexiceman,

Welcome to the Auth0 Community Forum!

You could accomplish a similar functionality in a rule doing something like this:

function (user, context, callback) {
  if (`user.app_metadata.twilioToken`){
    callback(null, user, context);
  } else {
    //make twilio request and handle response adding the metadata as twilioToken
    callback(null, user, context);
  }
}

As far as why social logins are not supported by hooks, I can’t find a definitive answer about that at this time, but I can say that it is something that has been requested before and our product managers are aware of it. It is helpful if you explain your use case and send in a formal feature request to our feedback page.

Hope this helps!

Thanks,
Dan

2 Likes

Thank you for your help and example.

How to I set the rule code so it runs on registration and not login?

I’ll describe the feature request at the link.

Best,
Alex

1 Like

Not an “official” answer but IMO social users and database users are not the same. In case of database connection the IdP is Auth0 whereas in case of social connections the IdP is social provider. Social accounts already exists at the IdP so there is not “registration” event at Auth0. You can treat social user’s first login as a “registration” but it is not the same case as database user registration.

Also for social users, a cached profile is created at Auth0. On every social login, user’s data is updated in auth0 if it was changed at the IdP for eg. if you change your email at the social IdP and then do a login with that user account, user’s profile at Auth0 will be automatically updated.

Now, following on @dan.woda 's suggestion, you can execute some custom login on social user’s first login (which is equivalent to registration since it is the first time Auth0 will create a cached profile for the user on Auth0 side). For eg.

function (user, context, callback) {

  const CLIENTS_ENABLED = ['REPLACE_WITH_YOUR_CLIENT_ID'];
  // run only for the specified clients
  if (CLIENTS_ENABLED.indexOf(context.clientID) === -1) {
    return callback(null, user, context);
  }

  // initialize app_metadata
  user.app_metadata = user.app_metadata || {};

  const is_social = context.connectionStrategy === context.connection;
  // if it is the first login (hence the `signup`) and it is a social login
  if (context.stats.loginsCount === 1 && is_social) {

    // turn on the flag
    user.app_metadata.is_signup = true;
    //  Now you can set up twillio token here for eg.

    // store the app_metadata
    auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
      .then(function(){
        // if no error continue rule pipeline execution
        return callback(null, user, context);
      })
      .catch(function(err){
        callback(err);
      });
  }
  }

  // else it is a non social login or it is not a signup
  callback(null, user, context);
}

NOTE: I haven’t tested this code so make sure you test it in DEV thoroughly before deploying to PRODUCTION

2 Likes

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