okta/okta-sdk-nodejs

Get a list of groups the user belongs to

kate-longueil opened this issue · 5 comments

Is there a way to do this? I tried several variations on client.getUser(userId).listGroups().each(group =>{});

Hi @kateloo. Does client.listUserGroups(userId) suit your needs?

This is perfect! Thank you @oleksandrpravosudko-okta!

Hi @oleksandrpravosudko-okta,

I am also trying to fetch the groups of a user and I am using the method you mentioned above. But this is not working for me. I saw that it returns a collection but it is returning an object with nextUri and request parameters.

I have tried this for users within single group as well as multiple groups but no difference.

async function getUserGroup(userId) { const result = await client.listUserGroups(userId); return result; }

result -> { "nextUri": "https://<orgUrl>/api/v1/users/<userId>/groups", "client": { "requestExecutor": { "_events": {}, "_eventsCount": 0, "r.....................

I also tried using the each method for that also does not yield the groups. Could you guide me what am I doing wrong?

Here's how I'm accessing group names in typescript. Hope it helps @ashutosh8

            let groupNames: Array<string> = [];
            await _this.client.listUserGroups(userId).each(group => {
                groupNames.push(group.profile.name);
            }).catch(err => {
                //send user not found response
                httpStatus = 404;
                errorMessage = err;
            });

Thanks for posting the example @kateloo.

I saw that it returns a collection but it is returning an object with nextUri

@ashutosh8 in this case collection refers to a custom class instance - you should be able to access the resources it wraps with its methods.