Auth0 Home Blog Docs

Retrieve User Details via nodejs-auth0 SDK

Hi All,

I’m running an application on AWS where a cronjob fires, retrieves a message from a SQS queue which I then need to retrieve the users Name and Email. I’ve got the users ID but I cant seem to find any working examples of getting the details from Auth0 using the nodejs-auth0 SDK

  • Which SDK does this apply to? nodejs-auth0
  • Which verison of the SDK you are using? v2.20.0
  • Which version of the platform are you facing this error on? NodeJS 10.x

Any help on how I can achieve this or pointing me in the right direction would be greatly appreciated as its driving me nuts now.

Thanks,
Tom

Hi @gintomm,

If you would like to fetch the user data via a call to the management api, you can do so by calling the getUser function of the node auth0 management client.

This demonstrates a few ways to initialize the management client in the auth0-node library:

And here is an example of getting a user:

auth0.users.get({ id: USER_ID }, function (err, user) {
  console.log(user);
});

Does that make sense?

Thanks,
Dan

Hi Dan,

Thanks for the quick response, but unfortunately this doesn’t seem to be working.

Its not returning anything and there doesn’t seem to be any errors either.

Not sure if this helps but this is firing via a Lambda function??

Thanks,
Tom

Hi Dan,

I’m wondering if there is any more help available as I’ve been at this now for 4 days and is a major blocker for us, because if we can’t retrieve user details in our lambda functions then we’re needing to re think our authentication platform ASAP.

Thanks,
Tom

@gintomm,

Can you help us with more information? Post your code in full. Have you tried debugging with console logs, what is happening? Are other functions working correctly, or is the whole library not working? Are you getting a valid token?

Also try this:

auth0
  .getUser({id:userId})
  .then(function(users) {
    console.log(users);
  })
  .catch(function(err) {
    console.log(err);
  });

edit: user id option

Hi @dan.woda

So this is my full code:

It gets into my function GetUserDetails and the console.log of the user id works but then thats it.

When I console log the auth0 variable then I get all the object of whats return by being authorised.

import { ManagementClient } from 'auth0';

const auth0 = new ManagementClient({
  domain: 'xxx.eu.auth0.com',
  clientId: 'xxx',
  clientSecret:'xxx',
  scope: 'read:users update:users',
});

const GetUserDetails = async userId => {
  console.log('userId', userId); <-- This gets logged

  auth0
    .getUser({ id: userId })
    .then(function(users) {
      console.log(users); <-- Returns nothing
    })
    .catch(function(err) {
      console.log(err); <-- Returns nothing
    });
};

@gintomm,

I got this to work:

const ManagementClient = require('auth0').ManagementClient;

const auth0 = new ManagementClient({
  domain: 'xxxx.auth0.com',
  clientId: 'xxxx',
  clientSecret:'xxxx',
  scope: 'read:users update:users',
});

const GetUserDetails = async userId => {
  console.log('userId', userId);

  auth0
    .getUser({ id: userId })
    .then(function(users) {
      console.log(users);
    })
    .catch(function(err) {
      console.log(err);
    });
};

const userId = 'auth0|xxxx';

GetUserDetails(userId);