# Problem when find user groups in Angular

**URL:** <https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253>\
**Category:** Questions\
**Tags:** angular\
**Created:** [May 5, 2020, 6:58pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253 "2020-05-05T18:58:57Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![dukeim](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@dukeim](https://devforum.okta.com/u/dukeim)\
**Post date:** [May 5, 2020, 6:58pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/1 "2020-05-05T18:58:57Z")

</div>

Hello.

I want to validate if a user belongs to a specific group. I’m writing in Angular 8 the next code:

```
this.user = await this.okta.getUser();
    const result = this.user.groups.find((group) => group === 'ContactAdmin');
    if (result) {
        return true;
    } else {
        alert('User is not authorized to perform this operation');
        return false; 
}

```

Part of the data in the payload of the token is the next:  
“scp”: [  
“openid”  
],  
“sub”: “[jhon@trial.com](mailto:jhon@trial.com)”,  
“groups”: [  
“Everyone”,  
“ContactAdmin”  
]

But i am getting the next error in the console:

```
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'find' of undefined
TypeError: Cannot read property 'find' of undefined (admin.guard.ts:22) 

```

The line 22 is exactly the following::

` const result = this.user.groups.find((group) => group === 'ContactAdmin');`

Part of the package.json is the following:  
“dependencies”: {  
“@angular/animations”: “~8.2.13”,  
“@angular/cdk”: “~8.2.3”,  
“@angular/common”: “~8.2.13”,  
“@angular/compiler”: “~8.2.13”,  
“@angular/core”: “~8.2.13”,  
“@angular/forms”: “~8.2.13”,  
“@angular/material”: “^8.2.3”,  
“@angular/platform-browser”: “~8.2.13”,  
“@angular/platform-browser-dynamic”: “~8.2.13”,  
“@angular/router”: “~8.2.13”,  
“@okta/okta-angular”: “^1.4.0”,  
“@okta/okta-auth-js”: “^2.10.1”,  
“@oktadev/schematics”: “^1.0.1”,  
“hammerjs”: “^2.0.8”,  
“rxjs”: “~6.4.0”,  
“tslib”: “^1.10.0”,  
“zone.js”: “~0.9.1”  
},  
“devDependencies”: {  
“@angular-devkit/build-angular”: “~0.803.18”,  
“@angular/cli”: “~8.3.18”,  
“@angular/compiler-cli”: “~8.2.13”,  
“@angular/language-service”: “~8.2.13”,  
“@types/node”: “~8.9.4”,  
“@types/jasmine”: “~3.3.8”,  
“@types/jasminewd2”: “~2.0.3”,  
“codelyzer”: “^5.0.0”,  
“jasmine-core”: “~3.4.0”,  
“jasmine-spec-reporter”: “~4.2.1”,  
“karma”: “~4.1.0”,  
“karma-chrome-launcher”: “~2.2.0”,  
“karma-coverage-istanbul-reporter”: “~2.0.1”,  
“karma-jasmine”: “~2.0.1”,  
“karma-jasmine-html-reporter”: “^1.4.0”,  
“protractor”: “~5.4.0”,  
“ts-node”: “~7.0.0”,  
“tslint”: “~5.15.0”,  
“typescript”: “~3.5.3”  
}

How can i fix this?  
If the problem is because of libraries version. ¿How can i update them?, ¿What version should they be?

Thanks.

---

<div class="post-metadata">

**Author:** ![dukeim](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@dukeim](https://devforum.okta.com/u/dukeim)\
**Post date:** [May 6, 2020, 12:01am UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/2 "2020-05-06T00:01:20Z")

</div>

The cause is that **this.oktaAuth.getUser(**) for some reason returns **undefined**.  
To get more data about the reason, i have printed in the console the AccessToken and the IdToken of the user just before to call getUser() method. The results in the payload part (decoded in [jwt.io](http://jwt.io)) were the following (some values have been edited for security):

IdToken:  
{  
“sub”: “0000000000000000”,  
“ver”: 1,  
“iss”: “[https://dev-xxxxxx.okta.com/oauth2/default](https://dev-xxxxxx.okta.com/oauth2/default)”,  
“aud”: “xxxxxxxxxxxx”,  
“iat”: 1588721882,  
“exp”: 1588725482,  
“jti”: “ID.erQIsnJUmAwxYcY71C2fGTuVelJqNiE02R1WmAQ6MNU”,  
“amr”: [  
“pwd”  
],  
“idp”: “kkkkkkkkkkkkkkk”,  
“nonce”: “d0NlBxbeCuMa4lNTXAjzO82bfCniPk6ODLw7GEMeJvSzchWOoQO3CDQ3CKvkcqWc”,  
“auth\_time”: 1588717952,  
“at\_hash”: “pQU7VoaxOFTFk3WR2of5NQ”  
}

AccessToken:  
{  
“ver”: 1,  
“jti”: “AT.UKiVqAVDyjRHAQG1kbT2lyDCShuo898GkvGkheYchio”,  
“iss”: “[https://dev-xxxxxx.okta.com/oauth2/default](https://dev-xxxxxx.okta.com/oauth2/default)”,  
“aud”: “api://default”,  
“iat”: 1588721881,  
“exp”: 1588725481,  
“cid”: “xxxxxxxxxxxx”,  
“uid”: “wwwwwwwwwwwwww”,  
“scp”: [  
“openid”  
],  
“sub”: “[jhon@trial.com](mailto:jhon@trial.com)”,  
“groups”: [  
“Everyone”,  
“ContactAdmin”  
]  
}

This give you some clue about how to solve?

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mraible/32/37_2.png) [@mraible](https://devforum.okta.com/u/mraible)\
**Post date:** [May 6, 2020, 1:44pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/3 "2020-05-06T13:44:44Z")

</div>

Make sure you have `profile` in your scopes in your Angular config.

---

<div class="post-metadata">

**Author:** ![swati.goel](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@swati.goel](https://devforum.okta.com/u/swati.goel)\
**Post date:** [September 23, 2020, 8:00pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/4 "2020-09-23T20:00:33Z")

</div>

I am facing same issue . Can you please share the solution

---

<div class="post-metadata">

**Author:** ![swati.goel](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@swati.goel](https://devforum.okta.com/u/swati.goel)\
**Post date:** [September 23, 2020, 8:01pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/5 "2020-09-23T20:01:04Z")

</div>

even after adding profile  
group is not coming

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mraible/32/37_2.png) [@mraible](https://devforum.okta.com/u/mraible)\
**Post date:** [September 23, 2020, 8:15pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/6 "2020-09-23T20:15:18Z")

</div>

Did you add a “groups” claim to your authorization server? You need to add it to the ID token in my experience. [https://developer.okta.com/docs/guides/customize-tokens-groups-claim/add-groups-claim-org-as/](https://developer.okta.com/docs/guides/customize-tokens-groups-claim/add-groups-claim-org-as/)

---

<div class="post-metadata">

**Author:** ![swati.goel](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@swati.goel](https://devforum.okta.com/u/swati.goel)\
**Post date:** [September 23, 2020, 8:33pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/7 "2020-09-23T20:33:43Z")

</div>

yes we have done that and i am getting group under decoded value  
{  
“ver”: 1,  
“jti”: “AT.-Msm6r1n0a\_2PYXvB1KLhZ\_9aYp6\_yLrD1Ypc5jXmN0”,  
“iss”: “[https://dev-988868.okta.com/oauth2/default](https://dev-988868.okta.com/oauth2/default)”,  
“aud”: “api://default”,  
“iat”: 1600891992,  
“exp”: 1600895592,  
“cid”: “0oaw11hdo0cZqeajr4x6”,  
“uid”: “00ux6euger6IiBH2n4x6”,  
“scp”: [  
“profile”,  
“email”,  
“groups”,  
“openid”  
],  
“Group”: [  
“Everyone”,  
“ug\_prt\_admin”,  
“ug\_prt\_users”  
],  
“sub”: “[Swati.Goel@moodys.com](mailto:Swati.Goel@moodys.com)”,  
“groups”: [  
“Everyone”,  
“ug\_prt\_admin”,  
“ug\_prt\_users”  
],  
“email”: “[Swati.Goel@moodys.com](mailto:Swati.Goel@moodys.com)”  
}  
but when i use this code this.user = await this.okta.getUser();  
const result = this.user.groups.find((group) =\> group === ‘ug\_prt\_admin’);  
if (result) {  
return true;  
} else {  
alert(‘User is not authorized to perform this operation’);  
return false;  
}  
I am not able to retrieve groups ☹

---

<div class="post-metadata">

**Author:** ![swati.goel](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@swati.goel](https://devforum.okta.com/u/swati.goel)\
**Post date:** [September 23, 2020, 8:37pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/8 "2020-09-23T20:37:04Z")

</div>

Thanks for your quick reply mraible 🙂

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mraible/32/37_2.png) [@mraible](https://devforum.okta.com/u/mraible)\
**Post date:** [September 23, 2020, 8:45pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/9 "2020-09-23T20:45:21Z")

</div>

If you print out the `user` object that’s retrieved, are there any groups in there? If not, I’d suggest you send an email to [developers@okta.com](mailto:developers@okta.com).

---

<div class="post-metadata">

**Author:** ![swati.goel](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@swati.goel](https://devforum.okta.com/u/swati.goel)\
**Post date:** [September 23, 2020, 9:22pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/10 "2020-09-23T21:22:09Z")

</div>

Hi All,

I am unable to retrieve logged in user group information in angular application

User Object

 ![image001.jpg](https://global.discourse-cdn.com/oktadev/original/2X/3/3c0d5411050c581840c6ef980997d08f4b0a47ea.jpeg)

**Decoded token using [https://jwt.io/ ![image002.jpg](https://global.discourse-cdn.com/oktadev/original/2X/a/ae901aedcca12d30a3747aee7a0494881ea81809.jpeg)](https://jwt.io/#)**

I am not able to retrieve groups when ‘**this.oktaAuth.getUser(\*\*\*\*).groups’** is returning **undefined**.

**Code snippet**

this.user = await this.okta.getUser();  
const result = this.user.groups.find((group) =\> group === ‘ug\_prt\_admin’);  
if (result) {  
return true;  
} else {  
alert(‘User is not authorized to perform this operation’);  
return false;  
}

Please help us in resolving this issue. This is high priority issue.

Thanks and Regards,

Swati

---

<div class="post-metadata">

**Author:** ![swati.goel](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@swati.goel](https://devforum.okta.com/u/swati.goel)\
**Post date:** [September 23, 2020, 9:23pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/11 "2020-09-23T21:23:19Z")

</div>

In user object i am getting below response

 ![image](https://global.discourse-cdn.com/oktadev/original/2X/a/a0ca912c15dd38be17b6f0865270e27d3c9a6464.png)

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mraible/32/37_2.png) [@mraible](https://devforum.okta.com/u/mraible)\
**Post date:** [September 24, 2020, 4:51pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/12 "2020-09-24T16:51:09Z")

</div>

When you create the `groups` claim, there’s the option of putting it in the ID or access token. If you click on ID Token, the “Always” box will turn white, and then you can select **Userinfo / id\_token request** in the dropdown list.

If you select **Userinfo / id\_token request** it will show up in both. If it’s “ID token” and “Always”, then it will only be present in the id token.

 ![Create claims](https://global.discourse-cdn.com/oktadev/original/2X/6/61fd93834a0e2a813ef1c1c12fa414bd441d37ef.png)

---

<div class="post-metadata">

**Author:** ![vipinsaini434](https://avatars.discourse-cdn.com/v4/letter/v/b5e925/32.png) [@vipinsaini434](https://devforum.okta.com/u/vipinsaini434)\
**Post date:** [April 27, 2022, 11:57pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/13 "2022-04-27T23:57:35Z")

</div>

Hi,  
This is not the problem,  
We can see groups in userInfo in dev console.  
Problem is when we try to retrieve it in code using this.oktaAuth.getUser() , we can not find groups in it because getUser() is returning UserClaims object which does not have groups variable.

if anyone know please help how to retrieve it.

Regards,  
Vipin Kumar

---

<div class="post-metadata">

**Author:** ![fmalaj1](https://avatars.discourse-cdn.com/v4/letter/f/ee7513/32.png) [@fmalaj1](https://devforum.okta.com/u/fmalaj1)\
**Post date:** [July 7, 2022, 6:18pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/14 "2022-07-07T18:18:00Z")

</div>

@vipinsaini434 Were you able to resolve this issue? I have run into the same problem.  
If you have already resolved it then can you please share the solution ?

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [February 12, 2024, 9:16pm UTC](https://devforum.okta.com/t/problem-when-find-user-groups-in-angular/9253/15 "2024-02-12T21:16:43Z")

</div>

Closed on February 12, 2024, 9:16pm UTC


