How can I make a post request to API after a user signs up?

Hello. I’m currently working a MERN app that uses Auth0 for user authentication. Currently I’m implementing the database user functionality. I want to make an axios post request to my backend API every time a user signs up with Auth0. I wanted to ask what the best way to implement this was?

Hi @FreshPineapples,

I suggest using a Post-User Registration Action to make an Axios request to your backend API. Below is an example:

exports.onExecutePostUserRegistration = async (event, api) => {
  const axios = require('axios');
  
  const url = 'https://YOUR_URL';
  const options = {
    headers: {
      'Content-Type': 'application/json'
    }
  };
  const data = {
    username: event.user.username, 
    email: event.user.email
  };
  var response = await axios.post(url, data, options);
  console.log(response.data);
};

See the following resources for more details:

Let me know if you have any questions.

Thanks,
Rueben

Hi, I’ve tried using this before although I’m not sure if I’m doing everything correctly. I setup my Auth0 Action in the following way. I also integrated this action into the Post User Registration flow on my Auth0 dashboard.

exports.onExecutePostUserRegistration = async (event, api) => {
  const axios = require('axios');
  
  const url = 'http://localhost:3001/api/users';
  const options = {
    headers: {
      'Content-Type': 'application/json'
    }
  };
  const data = {
    username: event.user.user_id, 
  };
  var response = await axios.post(url, data, options);
  console.log(response.data);
};

This should send a post request to my backend api which then uploads the user id into the database. However, the database does not update. The database and backend server are working because when I test it on Postman it works appropriately. Please let me know if I’m implementing something wrong or I’m missing something. Thanks!

Hi @FreshPineapples,

Your implementation looks good to me.

Are you still experiencing any errors? If so, could you provide details around what you are experiencing?

Thanks,
Rueben

1 Like

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