How to get a users id token inside of the Post User Registration Auth0 Action

Hey, In my backend I am using the Auth0 auth middleware that checks that each request has a valid token in its header.

I want to create a Post User Registration Auth0 Action that adds the user to my database by calling my createUser API.

To do this I need to also pass that token from the Action.

How would I go about doing this?

Cheers

exports.onExecutePostUserRegistration = async (event) => {
  const token = ???;
  const url = "http://localhost:8080/api/user/createUser";
  const res = await axios.post(url, {
    params: {
      user: event.user,
    },
    headers: {
      Authorization: "Bearer " + token,
    },
  });
};

Hi @joshuarichards001,

Welcome to the Auth0 Community!

You should not be using the ID token as an authorization bearer token. You should be using an access token.

To get a token, you need to set up your Action as an application in the Dashboard. In this case, your Action will act as a machine to machine (M2M) application that makes requests to your API.

Then you can request an access token for your API with a client credentials grant.

It is very similar to the process outlined here.

Cheers Dan, that ended up working I think.

Another quick question, is there any way to run actions locally? I would like the possibility to execute that API endpoint ‘http://localhost:8080/api/user/createUser’ from the action.

You can’t run an action from your local machine.

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