Rule Not Updating my app_metadata but in debug it works

I have a rule in place that calls an external public api and returns data if the email exists. based on that data i set :

user.app_metadata.roles = userData;
user.app_metadata.companyId = userCompany;

when i debug it or i test it. It works and i see my data update. Now if I test it using the google auth, it does not work at at all. When i go to the user section in auth0 is does not show my updated app_metadata… Any ideas?

TIA!

Hi @oflores2313

Install the real-time webtask logs extension and use console.log to view what is happening in the non-debug case.

John

I did and my profile is set correctly there when i console log it.

This is my code. Am i doing something wrong here:

function setRolesToUser(user, context, callback) {
  // setting my meta data
  user.app_metadata = user.app_metadata || {};
  user.app_metadata.roles = user.app_metadata.roles || '';
  user.app_metadata.companyId = user.app_metadata.companyId || '';

  console.log('OSCAR USER PROFILE', user);
	
  var axios = require("axios").default;
  var options = {
  	method: 'GET',
    url: `http://e68c26fc1d1d.ngrok.io/users/find-user?email=${user.email}`,
    headers: {'content-type':'application/json'}
  };
  
  axios(options)
  .then( res => {
    console.log(res);
  	let userData;
    let userCompany;
    // checking if theres any returned data
    if(res.data.length === 0){
      userData = '';
      userCompany = '';
    }else {
      console.log('res:', res.data);
      // if data is returned from the API, then set the role and the companyId
      userData = res.data[0].role.role_name;
      userCompany = res.data[0].company_id;
    }
    user.app_metadata.roles = userData;
    user.app_metadata.companyId = userCompany;
    if(user.app_metadata.roles) {
    	user.roles = user.app_metadata.roles;
    }
    if(user.app_metadata.companyId) {
    	user.companyId = user.app_metadata.companyId;
    }
    console.log('HELP!!!!!!!', user);
    callback(null, user, context);
    
  }).catch(err => {
    callback(err);
  });
}

You are missing the code to update the metadata in the profile:

 auth0.users
.updateUserMetadata(user.user_id, user.user_metadata)
.then(function () {
  callback(null, user, context);
})
.catch(function (err) {
  callback(err);
});

John

1 Like

Thanks for helping on this one John!

Thank you John!!!

1 Like

We are here for you!