Is there a way to use "localhost" as the axios post destination for Auth0 Actions?

Question:: Can we use “localhost” in Auth0 Actions?

I use Auth0 for Authentication for SPA(Next.js and Rails API)

Like this:

I want to sync auth0’s “user_id” with our MySQL Database, when a user sign up.
To achieve this, I thought Auth0 Actions’s “post registrations” looked good.
And I found good example in “Post User Registration documentation”. (Actions Triggers: post-user-registration)

I tried this example, my new actions. like this.

const axios = require("axios");

exports.onExecutePostUserRegistration = async (event) => {
  const { user: { email, user_id } } = event
  try {
    await axios.post("http://localhost:9292/api/v1/users", { params: { email, user_id }});
  } catch (error) {
    console.error(error)
  }
};

But, this code was not working. I got a error message.

Error { Error: connect ECONNREFUSED 127.0.0.1:9292
    at TCPConnectWrap.afterConnect [as oncomplete]
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 9292,

I’m guessing this is because localhost means the server where Actions is running, I ran the sample code by exposing my localhost using ngrok (https://ngrok.com/).

0d2du0602266.ngrok.io → localhost:9292

const axios = require("axios");

exports.onExecutePostUserRegistration = async (event) => {
  const { user: { email, user_id } } = event
  try {
    await axios.post("http://0d2du0602266.ngrok.io/api/v1/users", { params: { email, user_id }});
  } catch (error) {
    console.error(error)
  }
};

This code is working, and User Data is inserted our MySQL Database.

But, this solution isn’t good. Because we have to keep ngrok running during development, and the endpoint URL will change for each member of the team.

Is there a way to use the localhost endpoint in Auth0 Actions, or is there a non-Actions option to synchronize Auth0 user data?

Hi @NerdyBoyCool, Welcome to the Auth0 Community!

It’s not possible to use localhost in Auth0 Actions/Rules/Hooks. Simply because, as you have mentioned, the localhost refers to the server instance the Actions/Rules/Hooks are running.

You have to use the exact location in the URL which is available over the public internet or use a tool like you have used (ngrok) to connect the request to the actual server.

Hope this answers your question!

3 Likes

Thanks for helping on this one Supun!

Thank you @supun .

The auth0 tenant itself is operated separately so that each developer can have a fixed webhook url, and all tenants have the same configuration in auth0-cli-deploy.

When I wrote this, actions were not supported by auth0-cli-deploy, so I used rules to implement the same code and set the fixed URL for ngrok as the URL for webhook in the environment variable of auth0-cli-deploy.

Today, I found that actions is no longer in beta, so I’ll try the same strategy for actions.

1 Like

Perfect let us know if you have any other questions down the road!

I think I am late to the party, but you can use localhost if you combine it with Ngrok, which is tool for tunneling calls like web hooks into your localhost. The tool is free with a limited set of features, which are enough for developers

https://ngrok.com/

1 Like

Thanks for sharing it with the rest of community @cibrax !