management.usersByEmail not recognized in login action

Hi,

Got stuck here while migrating auth0 rule to action. Specifically with getting the users by email using the ManagementClient. Was expecting that this would work seamlessly as it did with Auth0 Rules.

const auth0 = require("auth0");

exports.onExecutePostLogin = async (event, api) => {
  const ManagementClient = auth0.ManagementClient;

  const management = new ManagementClient({
    clientId: event.secrets.AUTH0_CLIENT_ID,
    clientSecret: event.secrets.AUTH0_CLIENT_SECRET,
    domain: event.secrets.DOMAIN
  });

  management.usersByEmail(event.user.email, function (err, users) {
    cb(err, users);
  });
};

With the code block above, I’m getting this error:

{
  "message": "management.usersByEmail is not a function",
  "name": "TypeError",
  "stack": "TypeError: management.usersByEmail is not a function\n    at auth0 (/data/io/node18-actions/f46fc91e-a5f3-4450-a202-542fd964e6c1/webtask.js:41:18)\n    at /data/layers/layers-5gZU/5gZUD7V-iEoZT02eg2ioFvNp4q4jyGU1398DwYiP6sM/node_modules/async/dist/async.js:3676:28\n    at replenish (/data/layers/layers-5gZU/5gZUD7V-iEoZT02eg2ioFvNp4q4jyGU1398DwYiP6sM/node_modules/async/dist/async.js:448:21)\n    at /data/layers/layers-5gZU/5gZUD7V-iEoZT02eg2ioFvNp4q4jyGU1398DwYiP6sM/node_modules/async/dist/async.js:453:13\n    at eachOfLimit (/data/layers/layers-5gZU/5gZUD7V-iEoZT02eg2ioFvNp4q4jyGU1398DwYiP6sM/node_modules/async/dist/async.js:479:36)\n    at awaitable (/data/layers/layers-5gZU/5gZUD7V-iEoZT02eg2ioFvNp4q4jyGU1398DwYiP6sM/node_modules/async/dist/async.js:212:32)\n    at eachOfGeneric (/data/layers/layers-5gZU/5gZUD7V-iEoZT02eg2ioFvNp4q4jyGU1398DwYiP6sM/node_modules/async/dist/async.js:514:16)\n    at eachOf (/data/layers/layers-5gZU/5gZUD7V-iEoZT02eg2ioFvNp4q4jyGU1398DwYiP6sM/node_modules/async/dist/async.js:628:16)\n    at awaitable (/data/layers/layers-5gZU/5gZUD7V-iEoZT02eg2ioFvNp4q4jyGU1398DwYiP6sM/node_modules/async/dist/async.js:212:32)\n    at /data/layers/layers-5gZU/5gZUD7V-iEoZT02eg2ioFvNp4q4jyGU1398DwYiP6sM/node_modules/async/dist/async.js:3675:9"
}

Just for verifying that I’m using the correct function name:

usersByEmail: UsersByEmailManager {
    configuration: {
      clientId: 'cf7EiMQP9nGVE3tvuXKjfIFFi33WoniO',
      clientSecret: 'QVge4qMxzSISJASGYuUr9_bw5HCSaEBlvVinLVKBeJ3H-Vl_2PqFxJfcF9beClIV',
      domain: 'bark-sandbox.us.auth0.com',
      scope: 'read:users',
      baseUrl: 'https://bark-sandbox.us.auth0.com/api/v2',
      middleware: [Array],
      parseError: [AsyncFunction: parseError]
    },
    fetchWithTimeout: [AsyncFunction (anonymous)],
    fetch: [AsyncFunction (anonymous)],
    middleware: [ [TokenProviderMiddleware], [TelemetryMiddleware] ],
    fetchApi: [AsyncFunction: fetch],
    parseError: [AsyncFunction: parseError],
    timeoutDuration: 10000
  }

What am I missing here?

Hi @cmoreno,

Recently, there was an update to the ManagementClient API. As part of this update, some of the methods underwent name changes.

The revised call to get users by email is now management.usersByEmail.getByEmail(requestParameters).

(Reference: UsersByEmailManager | auth0)

After implementing these updates in your Post Login action script, you should be able to get the user by email without any issues.

Please reach out if you encounter any difficulties.

Thanks,
Rueben

Hi @rueben.tiow,

Thank you for the quick response and documentation reference, though I’ve tried to replace the code with it but still getting the same error:

{
  "message": "management.getByEmail is not a function",
  "name": "TypeError",
  "stack": "TypeError: management.getByEmail is not a function\n    at auth0 (/data/io/node18-actions/1a95326b-d2a4-4900-88a8-6d00ef57ddea/webtask.js:42:18)\n    at /data/layers/layers-6lE4/6lE4f8h7liL-nqfuQmcOgvIvjrx_nYJrhIepd4yG4SY/node_modules/async/dist/async.js:3676:28\n    at replenish (/data/layers/layers-6lE4/6lE4f8h7liL-nqfuQmcOgvIvjrx_nYJrhIepd4yG4SY/node_modules/async/dist/async.js:448:21)\n    at /data/layers/layers-6lE4/6lE4f8h7liL-nqfuQmcOgvIvjrx_nYJrhIepd4yG4SY/node_modules/async/dist/async.js:453:13\n    at eachOfLimit (/data/layers/layers-6lE4/6lE4f8h7liL-nqfuQmcOgvIvjrx_nYJrhIepd4yG4SY/node_modules/async/dist/async.js:479:36)\n    at awaitable (/data/layers/layers-6lE4/6lE4f8h7liL-nqfuQmcOgvIvjrx_nYJrhIepd4yG4SY/node_modules/async/dist/async.js:212:32)\n    at eachOfGeneric (/data/layers/layers-6lE4/6lE4f8h7liL-nqfuQmcOgvIvjrx_nYJrhIepd4yG4SY/node_modules/async/dist/async.js:514:16)\n    at eachOf (/data/layers/layers-6lE4/6lE4f8h7liL-nqfuQmcOgvIvjrx_nYJrhIepd4yG4SY/node_modules/async/dist/async.js:628:16)\n    at awaitable (/data/layers/layers-6lE4/6lE4f8h7liL-nqfuQmcOgvIvjrx_nYJrhIepd4yG4SY/node_modules/async/dist/async.js:212:32)\n    at /data/layers/layers-6lE4/6lE4f8h7liL-nqfuQmcOgvIvjrx_nYJrhIepd4yG4SY/node_modules/async/dist/async.js:3675:9"
}

I’ve attached the dependencies of my action. So for the auth0, I’m using the latest one.

1 Like

This solved the issue for me:

management.usersByEmail.getByEmail({ email: event.user.email }
).then(response => {
    console.log(response.data);
}).catch(err => {
    console.log(err);
});
2 Likes

Hi @cmoreno,

I’m glad you were able to solve the issue and thank you for sharing the solution with the rest of the Community!

Please reach out again if you have any further questions.

Thanks,
Rueben

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