api.user.setUserMetadata not working in asynchronous methods?

Hello,

I have created this action :

/**
 * Handler that will be called during the execution of a PostLogin flow.
 *
 * @param {Event} event - Details about the user and the context in which they are logging in.
 * @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
 */

const axios = require('axios');

exports.onExecutePostLogin = async (event, api) => {
    var tokenBearer;
    api.user.setUserMetadata("customerId", "checkit");


    // Get access create account query
    axios.request({
        url: "/api/oauth/token",
        method: "post",
        baseURL: event.secrets.CRMAPIHOST,
        auth: {
            username: event.secrets.CRMAPIUSERNAME, // This is the client_id
            password: event.secrets.CRMAPIPASSWORD // This is the client_secret
        },
        data: {
            "grant_type": "client_credentials"
        },
        headers: {
            "Content-Type":"application/x-www-form-urlencoded"
        }
    }).then(response => {
        tokenBearer = response.data.access_token;
        executeCreateCustomerRequest(tokenBearer, event, api);
    });

    console.log("Token bearer is " + tokenBearer)
};

function executeCreateCustomerRequest(tokenBearer, event, api)  {
    var options = {
        method: 'POST',
        url: event.secrets.CRMAPICREATECUSTOMERURL,
        headers: {
            'Content-Type': 'application/json',
            'User-Agent': 'insomnia/9.3.3',
            ip_country_code: 'FR',
            Authorization: 'Bearer ' + tokenBearer
        },
        data: {
            email_optin: event.user.user_metadata.optin,
            gender: event.user.user_metadata.salutation,
            last_name: event.user.user_metadata.last_name,
            first_name_int: event.user.user_metadata.first_name,
            nationality: event.user.user_metadata.location,
            location: event.user.user_metadata.location,
            primary_email: event.user.email,
            salutation: event.user.user_metadata.salutation,
            last_name_int: event.user.user_metadata.last_name,
            first_name: event.user.user_metadata.first_name,
            preferred_language: event.user.user_metadata.location
        }
    };

    console.log(options);

    axios.request(options).then(function (response) {
        console.log(response.data);
        console.log(response.data.customer_id);
        var crmId = response.data.customer_id;
        api.user.setUserMetadata("customerId", crmId);
        console.log("customerid was added to user metadata");
    }).catch(function (error) {
        console.error(error.code);
    });
};


/**
 * Handler that will be invoked when this action is resuming after an external redirect. If your
 * onExecutePostLogin function does not perform a redirect, this function can be safely ignored.
 *
 * @param {Event} event - Details about the user and the context in which they are logging in.
 * @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
 */
//exports.onContinuePostLogin = async (event, api) => {
//};

And insert it in the login flow.

As you can see, I’m calling two axios requests the create a user in the customer CRM and would like to store in the user metadata the CRM ID return by the 2nd axios call.
However, it seems that for some reason, the second call to api.user.setUserMetadata is not working.
When I check my user metadata in auth0 dashboard, the value is “checkit” because the first call to setUserMetadata is working.

Is there any reason that would explain this issue ?
How can I acheive what I’m trying to do ?

Thanks and have a nice day,
Grégory

I would like to precise that all the console.log from this block are displayed in the log plugin and that the value of “response.data.customer_id” is correct.

I had a lot of pain with converting Rules to Actions until I realized that the custom metadata I was adding, for example…

api.user.setUserMetadata("foo", "bar");

…is not OIDC data. Turning off the “strict” requirement solved my problem.

img

Hello,

After discussing with the Auth0 support, the issue was coming from the asynchronous way to work of axios.
Changing the code to make it synchronous using await did fix my issue.

Regards,
Grégory

Hi @gregory.paillard

Thank you for sharing the solution with other community members!

Thanks!
Dawid