Assigning user roles in a rule

I have auth0 connected to a database in mongodb, I already did the login process from auth0 to my database, but I have a problem, the user created it in my database and until he logs in it doesn’t matter to auth0, so I can’t assign him the role until he exists in auth0, my idea is to create a rule that when the user logs in for the first time that auth0 matters it will fire a rule that assigns him the role, which comes in the data of mongo db, I appreciate your help

function (user, context, callback) {
    	const axios = require('axios');
      function getAccessToken() {
        return axios.post(
          "https://" + auth0.domain + "/oauth/token",
          {
            grant_type: "client_credentials",
            client_id: configuration.MGMT_API_ID,
            client_secret: configuration.MGMT_API_SECRET,
            audience: "https://" + auth0.domain + "/api/v2/"
          },
          { headers: { "content-type": "application/json" } }
        );
      }
      
      function assignRole(userId, roleId, accessToken) {
        return axios.post(
          `https://${auth0.domain}/api/v2/users/${userId}/roles`,
          { roles: [roleId] },
          {
            headers: {
              "cache-control": "no-cache",
              authorization: `Bearer ${accessToken}`,
              "content-type": "application/json"
            }
          }
        );
      }

    }

I am not sure i am getting your question correct. Are you migrationg user to auth0 when user login first time? If it is the case you can use post-registration hook and assign role to user when user has been created successfully at auth0.

Sounds similar to

1 Like

Let us know if you find the solution to your struggles in those threads!

Hey there!

As this topic is related to Rules - Hooks - Actions, I’m excited to let you know about our next Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

Learn more here!

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