If you mean to list groups for a specific user in the backend and return them as an API, here's a complete example using golang's aws sdk:
func sessionWithRegion(region *string) *session.Session {
sess := Session.Copy()
if v := aws.StringValue(sess.Config.Region); len(v) == 0 {
sess.Config.Region = region
}
return sess
}
func getGroupsForUser(region, userPoolId, userName *string, limit int64, nextToken ...*string) (*cognitoidentityprovider.AdminListGroupsForUserOutput, error) {
sess := sessionWithRegion(region)
svc := cognitoidentityprovider.New(sess)
input := &cognitoidentityprovider.AdminListGroupsForUserInput{
UserPoolId: userPoolId,
Username: userName,
Limit: &limit,
}
if nextToken != nil && len(nextToken) > 0 {
input.NextToken = nextToken[0]
}
return svc.AdminListGroupsForUser(input)
}
nextToken
is used to support pagination to load more groups in case we still have users to return from cognito to return/fetch, in other words, is to fetch next page. here is a simple example of how your calls should look like
func listGroups(pageNo *string) ([]*cognitoidentityprovider.GroupType, *string, error) {
page, err := getGroupsForUser(aws.String("us-east-1"), aws.String("xxx"), aws.String("myuserName"), 60, map[bool]*string{true: pageNo, false: nil}[pageNo != nil])
if err != nil {
return nil, nil, err
}
return page.Groups, page.NextToken, nil
}
The above code snippets were just meant to make it easy for you and other colleagues from the community to understand the idea and how to implement it but it may need some tweaks. please feel free to leave a comment in case you need more help.