How do I add a default role to a new user on first login?

Question: How do I assign a default role to a new user on first login?

Answer:
If you are using the Authorization Core, you can leverage the Management API in a rule to assign a role based on login count.

Example Rule:

function (user, context, callback) {

    const count = context.stats && context.stats.loginsCount ? context.stats.loginsCount : 0;
    if (count > 1) {
        return callback(null, user, context);
    }

    const ManagementClient = require('auth0@2.27.0').ManagementClient;
    const management = new ManagementClient({
      token: auth0.accessToken,
      domain: auth0.domain
    });

    const params =  { id : user.user_id};
    const data = { "roles" : ["ROLE_ID_1","ROLE_ID_2"]};

    management.users.assignRoles(params, data, function (err, user) {
    if (err) {
        // Handle error.
        console.log(err);
     }
    callback(null, user, context);
    });
    
}

Note: We are using node-auth0 2.27.0, the latest version available in rules. Make sure to check what Node.js modules are available in rules.

Supporting Documentation:

5 Likes
How to add roles, permissions to a user during signup?
How do I add a default role & permissions to a user?
Add Default Role to New User Sign-up
Assign Roles to users using Rules
Custom roles in Access token from application
After applying a Role to a User using a Rule at signup (first login), the permissions are not in the user's access token
Automatically adding permissions to JWT on first login
Redirect to onboarding flow after sign up (or first log in)?
Role Based-Access Control on Signup?
Assigning user roles in a rule
External API call in Auth0 Rules
Help to set up authentication and authorization for SPA
Add new user to role
Rule using node-auth0, AuthenticationClient is not a constructor
Reading roles inside a rule
How to add "roles" to user via Rules and include in root attributes
Assign default role during signup (react native)
RBAC and Plans and which method
Multiple audiences (/userinfo and custom API) with Social Login
Fine-Grained User Provisioning and Management
Hook at post-registration to assign a role
Adding role to user from hook
Is it really this difficult: Assign roles and put into JWT
How do I add a default role & permissions to a user?
Default role with Authorization Extension
Multiple Rules not Triggering and Hubspot Integration not working
Permissions not updating when adding Role via a Rule
How to get all user Claims with tokenOptions
Assigning Roles to Federated Users before creation
How to give roles and permissions to users in a Python WebApp
Assigning User roles on Custom Database action script
Hook is not executed for all new signups