I have a ReactJS SPA (frontend) with NodeJS/Express (backend). My Auth0 login form is on the frontend. I would like to send user_metadata from the frontend (the User object, created when User logs in) to my backend. I need to use some of the user attributes to filter API calls on the backend (i.e. pull data from a table and filter based on user’s company name).
I setup a M2M app and a custom API in Auth0 to pass the appropriate tokens. I am able to POST the access token, but I receive a 404 error when I try to GET /api/v2/userinfo
Below is a sample of my code. Any help will be greatly appreciated:
// I am able to retrieve the access token
const myFunction = async function (req, res) {
try {
var opts = {
method: "POST",
url: "<my auth0 domain>/oauth/token",
headers: { "content-type": "application/x-www-form-urlencoded" },
data: new URLSearchParams({
grant_type: "client_credentials",
client_id: <my client id>,
client_secret: <my client secret>,
audience: "https://dev-tyofb4m1.us.auth0.com/api/v2/",
}),
};
axios
.request(opts)
.then(function (response) {
const accessToken = response.data.access_token;
console.log("accessToken: ", accessToken);
})
.catch(function (error) {
console.error(error);
});
}
//But Axios returns 404 (Not Found) when i try to GET /api/v2/userinfo
var opt = {
method: "GET",
url: "https://<my auth0 domain>/api/v2/userinfo",
headers: {
"content-type": "application/json",
authorization: `Bearer ${accessToken}`,
},
};
axios
.request(opt)
.then(function (res) {
console.log(res.data);
})
.catch(function (error) {
console.error(error);
});