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

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