DEV Community

Cover image for Check if a user is part of a Cognito group in AWS Amplify
Beez Fedia
Beez Fedia

Posted on

10 2

Check if a user is part of a Cognito group in AWS Amplify

If you're looking to understand if an authenticated user is in a specific group the following method may help:

import { Auth } from 'aws-amplify';

const user =  await Auth.currentAuthenticatedUser();

// Returns an array of groups
const groups = user.signInUserSession.accessToken.payload["cognito:groups"];
Enter fullscreen mode Exit fullscreen mode

user.signInUserSession.accessToken.payload["cognito:groups"] returns an array of all the groups the user belongs to.

To discover if a user is part of the admin group use the array includes method:

const groups = user.signInUserSession.accessToken.payload["cognito:groups"];

groups.includes('admin'); // true
Enter fullscreen mode Exit fullscreen mode

For IE and older browsers use indexOf:

const groups = user.signInUserSession.accessToken.payload["cognito:groups"];

groups.indexOf('admin') !== -1; // true
Enter fullscreen mode Exit fullscreen mode

Happy days!

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay