AWS Cognito,使用放大检查经过身份验证的用户是在组中还是管理员

AWS Cognito, checking if authenticated user is in group or is admin using amplify

我试图通过检查它是否属于特定组来确定经过身份验证的用户是否是管理员。 我在我的 React 应用程序中使用 amplify 并尝试了几种方法,例如 Auth.currentUserInfo()Auth.currentAuthenticatedUser() 并且还获取了 jwt 令牌以查看它是否以某种方式在令牌中返回,但我没有找到任何有关的信息。 我看到有人说在 token 中存在 payload cognito:groups,但这可能会改变,因为在我返回的 token 中它不存在。

我认为可行的另一件事是 jwt (aws.cognito.signin.user.admin) 中的范围,但似乎每个使用 amplify 创建的用户都返回此范围。

是否可以检查经过身份验证的用户是否属于某个组,或者它是否是来自 Cognito 的管理员用户?

您可以从会话中获取用户组。它位于 user.signInUserSession.accessToken.payload["cognito:groups"] 中,它将包含用户所有组的数组。

这是一个简短的例子:

import { Auth } from 'aws-amplify';

const user =  await Auth.currentAuthenticatedUser();

// the array of groups that the user belongs to
user.signInUserSession.accessToken.payload["cognito:groups"]