Action Backend Call Connection Refused

Hello everyone

I’m trying to setup an action that calls my backend endpoint (Java, Spring Boot) in the Post User Registration Flow to store some user data to my backend database.

To achieve this I get an access token and then try to call my backend api /v1/accounts with a POST Method to create an account entity.

When I try to call my backend API axios throws a connect ECONNREFUSED 127.0.0.1:9000 error and also says "error": { "id": "invalid_argument", "msg": "Invalid Argument" }

I’m not sure if this might be a CORS issue since I setup CORS for my backend. For now my allowed origins look like this: allowedOrigins("http://localhost:4200", "https://<censored-domain>.auth0.com"). I’m not sure if this is the right origin for action calls.

When I follow the exact same steps with the same values in Postman the API Call works. Just in the action it fails.

Below my code.

Action:

const axios = require("axios");

exports.onExecutePostUserRegistration = async (event, api) => {
  console.log("entered action");

  const tokenOptions = {
    method: "POST",
    url: `https://${event.secrets.DOMAIN}/oauth/token`,
    headers: { "content-type": "application/json" },
    data: {
      grant_type: "client_credentials",
      client_id: event.secrets.CLIENT_ID,
      client_secret: event.secrets.CLIENT_SECRET,
      audience: `https://${event.secrets.DOMAIN}/api/v2/`,
      scope: 'read:users',
      subject: event.user.user_id,
    }
  };

  const res = await axios.request(tokenOptions);
  console.log("Access Token: ", res.data.access_token);

  const authUser = event.user;
  const headers = {
    "Authorization": `Bearer ${res.data.access_token}`,
    "Content-Type": "application/json"
  }

  const createUserOptions = {
    method: "POST",
    url: "http://localhost:9000/api/v1/accounts",
    headers,
    withCredentials: false,
    data: {
      id: authUser.user_id,
      name: authUser.email.split("@")[0],
      email: authUser.email
    }
  }

  console.log("Call API", createUserOptions);

  try {
    const res = await axios.request (createUserOptions);

    console.log('res', res);
  } catch(error) {
    console.log("Error", error);
  }
};

I’m grateful for any tips into the right direction. Thank you

Hi @Helvetios,

Welcome to the Auth0 Community!

Firstly, I recommend following our documentation on how to use the Management API in Actions below:

Additionally, it looks like you are taking this access token and trying to use it to make a POST request to your localhost. Unfortunately, it could not find your localhost URL and cannot send the POST request. Could you please confirm if you have a webserver configured locally, and listening at that port?

Thanks,
Rueben

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