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"];
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
For IE and older browsers use indexOf
:
const groups = user.signInUserSession.accessToken.payload["cognito:groups"];
groups.indexOf('admin') !== -1; // true
Happy days!
Top comments (0)