Is there a way to add a Social Login user to a role or org before they sign in the first time?

Hi @jgleason,

Thanks for reaching out to the Auth0 Community!

I understand that you are trying to add Roles and Organization to a user before they attempt to log in the first time.

In this situation, I recommend leveraging the Post-Login Action script to check for the user’s login_count and determine if it’s their first time logging in and adding the roles and Organization to their user profile.
See an example of adding members to an Organization below:

exports.onExecutePostLogin = async (event, api) => {
  const ManagementClient = require('auth0').ManagementClient;
  
  if(event.stats.logins_count === 1){
    const management = new ManagementClient({
        domain: event.secrets.domain,
        clientId: event.secrets.client_id,
        clientSecret: event.secrets.client_secret,
    });
    var params =  { id :'YOUR_ORG_ID'};
    var data = { members: [ event.user.user_id ] }
    management.organizations.addMembers(params, data, function (err) {
        if (err) {
            // Handle error.
        }
    });
  }
};

Please see this FAQ to learn more about using the Management API in Actions.

Please let me know if you help with this.

Thank you!

1 Like