0

I'm trying to sync my auth0 userinfo with my local database:

userRouter.get("/sync", validateAccessToken, (req, res) => {
  var request = require("request");
  var usertoken;
  var options = { method: 'POST',
    url: 'https://MYDOMAN.eu.auth0.com/oauth/token',
    headers: { 'content-type': 'application/json' },
    body: '{"client_id":"myclienttoken","client_secret":"myclientsecret","audience":"https://MYDOMAIN.eu.auth0.com/api/v2/","grant_type":"client_credentials"}' };
  
  request(options, function (error, response, body) {
    if (error) throw new Error(error);
    usertoken = body;
    console.log(body);
  });
  var auth0options = {
    method: "GET",
    url: "https://MYDOMAIN.eu.auth0.com/api/v2/users",
    params: {id: 'email:"testuser"', search_engine: 'v3'},
    headers: {
      "content-type": "application/json",
      authorization: `Bearer` + usertoken.access_token,
    },
  };

  axios.request(auth0options).then(function (response) {
      console.log("RES DATA: ", response.data);
    })
    .catch(function (error) {
      console.error(error);
    });
  console.log("called");
  res.status(200).json("message");
});

The following line, results in a error:

authorization: Bearer + usertoken.access_token,

"Cannot read properties of Undefined (reading 'access_token)"

But I don't get the userinfo when calling the auth0 api with that token. I'm using the audience from the Auth0 Management API ex:

https://MYDOMAIN.eu.auth0.com/api/v2/

And not the audience from my own API, as I have read that's the correct way:

https://mydomain

Any ideas on what I'm doing wrong?

Mathias
  • 43
  • 1
  • 6
  • So what do you mean by it doesn’t work? Do you get an error, an empty result? – Sorix Feb 19 '23 at 10:54
  • Hi, I have updated my original question. – Mathias Feb 20 '23 at 17:44
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) `request` is an asynchronous function. – Heiko Theißen Feb 20 '23 at 17:47
  • It could seem so, but I'm not sure how to implement it in my code. "async = request" doesn't seem to do the trick? – Mathias Feb 20 '23 at 18:04
  • It's not clear to me why you're using two different types of http requests? In the axios one, you are actually handling the return of an async function, in the `.then(...)` part. – Sorix Feb 21 '23 at 10:29
  • It helped that I converted both requests to axios, and put the second inside the first (to wait for the response). I call twice, first to get the mgmt token, to be used for search after a user. – Mathias Feb 22 '23 at 19:50

0 Answers0