You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断AWS Cognito用户的登录方式(第三方/用户名密码)?

如何判断AWS Cognito用户的登录方式(第三方/用户名密码)

要区分Cognito用户是通过Apple/Google/Facebook等第三方账号登录,还是普通用户名密码方式,核心是查看用户的identity_provider属性——这是Cognito自动为用户维护的关键标识:

一、核心判断逻辑

  • 第三方登录用户:identity_provider属性值为对应服务商的标识,比如Facebook、Google、SignInWithApple
  • 普通用户名密码用户:该属性要么不存在,要么值为Cognito(表示用户池自身作为身份源)

二、具体实现方式

1. 通过Cognito控制台直接查看

  • 登录AWS控制台,进入目标Cognito用户池
  • 在“用户和组”中找到目标用户,点击进入详情页
  • 滚动到“用户属性”区域,查找identity_provider字段,根据值判断登录类型

2. 通过API/SDK查询

AWS CLI 示例

执行以下命令(替换占位符):

aws cognito-idp admin-get-user --user-pool-id 你的用户池ID --username 目标用户名

在返回的UserAttributes数组中,找到Name: "identity_provider"的条目,查看其Value即可判断。

JavaScript SDK 示例

import { CognitoIdentityProviderClient, AdminGetUserCommand } from "@aws-sdk/client-cognito-identity-provider";

const client = new CognitoIdentityProviderClient({ region: "你的AWS区域" });
const getUserCommand = new AdminGetUserCommand({
  UserPoolId: "你的用户池ID",
  Username: "目标用户名"
});

try {
  const response = await client.send(getUserCommand);
  const idpAttr = response.UserAttributes.find(attr => attr.Name === "identity_provider");
  
  if (!idpAttr || idpAttr.Value === "Cognito") {
    console.log("该用户为普通用户名密码登录");
  } else {
    // 若用户关联了多个身份源,值会用逗号分隔,比如"Google,Cognito"
    const thirdPartyIds = idpAttr.Value.split(",").filter(idp => idp !== "Cognito");
    console.log(`用户通过${thirdPartyIds.join(",")}第三方登录`);
  }
} catch (err) {
  console.error("查询用户信息失败:", err);
}

补充说明

如果用户后期关联了多个身份源(比如先注册了用户名密码,之后又绑定了Google账号),identity_provider的值会用逗号分隔多个身份源标识,此时只需过滤掉Cognito值,剩余的就是第三方服务商标识。

内容的提问来源于stack exchange,提问作者Karl Taylor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 14:25:19