New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement API to fetch a User's Cognito Group #413
Comments
@rommank Currently, I do not believe Cognito exposes a non-Admin API to get a user's groups. However, they are included in the idtoken. I am marking this as a feature request so that we can possible implement a solution on our end , but in the interim you can get these values with a jwt library such as jwt_decode. (This isn't necessarily an endorsement of that library over others- it's simply for the purposes of example.) import 'package:jwt_decode/jwt_decode.dart';
...
void _fetchSession() async {
try {
CognitoAuthSession res = await Amplify.Auth.fetchAuthSession(
options: CognitoSessionOptions(getAWSCredentials: true));
// Grab the idtoken from the response
String token = res.userPoolTokens.idToken;
// Parse the JWT
Map<String, dynamic> payload = Jwt.parseJwt(token);
// Access the groups
List groups = payload['cognito:groups'];
print(groups);
} on AmplifyException catch (e) {;
print(e);
}
} |
My raw jwt token does not contain the group:
|
Have you assigned a Cognito Group to your user? Because, if not, your payload is not going to have "cognito:groups". |
Thanks for the solution! |
Seem I rebuilt the environment at some point and was without a group :) cheers |
I support the new feature from the API. @haverchuck With the new Amplify version i'm getting the next error: |
@SalahAdDin if you are seeing that in the following assignment CognitoAuthSession res = await Amplify.Auth.fetchAuthSession(
options: CognitoSessionOptions(getAWSCredentials: true)); I believe you just need to cast it to a CognitoAuthSession res = await Amplify.Auth.fetchAuthSession(
options: CognitoSessionOptions(getAWSCredentials: true)) as CognitoAuthSession; The documentation shows this cast as well. I am not sure if this changed at some point in time. Apologies if it did. |
Please see this discussion for how to do this in the current stable ( final session =
await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
final idToken = session.userPoolTokens!.idToken;
final userGroups = idToken.groups; At the moment, we have no plans to add further APIs for retrieving the user groups. |
Is there another way to find out user's group?
The text was updated successfully, but these errors were encountered: