gpt4 book ai didi

reactjs - 将 AWS Cognito 与 Google 和 React 自定义登录集成的步骤

转载 作者:行者123 更新时间:2023-12-03 14:27:47 24 4
gpt4 key购买 nike

嗨,谁能告诉我集成 AWS cognito 以进行自定义(用户名/密码)和谷歌登录的步骤吗?我能够毫无问题地让自定义登录正常工作。我能够毫无问题地配置用户池。我只是在关注客户端的变化。

我使用 amazon-cognito-identity-js 和 aws-sdk 软件包。

以下是我使用的一些代码片段。这里我使用一个参数来决定验证器。如果你们能提供任何反馈,那就太好了。谢谢。

export function getAwsCredentials(userToken, type) {
let authenticator = '';


if (type == 'CUSTOM') {
authenticator = `cognito-idp.${config.cognito
.REGION}.amazonaws.com/${config.cognito.USER_POOL_ID}`;
}
else if (type == 'GOOGLE') {
authenticator = 'accounts.google.com';
}

AWS.config.update({ region: config.cognito.REGION });

AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: config.cognito.IDENTITY_POOL_ID,
Logins: {
[authenticator]: userToken

}
});

return AWS.config.credentials.getPromise();
}

最佳答案

当涉及到联合身份池时,您确实可以一视同仁地对待所有 token 。您只需更改登录映射中的[authenticator]

据推测,调用 getAwsCredentials 函数的代码会知道 token 来自何处,并且它可以传入参数,就像您在上面所做的那样。

我想说的是,我认为这不一定是一个糟糕的方法。我可能会直接传递 [authenticator] 值来代替 type,但这是一件相对较小的事情。我的主要观点是,将了解 token 来自何处的责任留给调用代码,而不是调用的函数,可能会更好。

使用 token 确定身份提供者

当然,有同样有效的方法可能希望或需要将身份提供者从 token 本身中提取出来。

当你 integrate with Google directly ,您从 Google 获得一个 token ,然后将其传递给 Cognito(如上面的代码所示)。如果你解码 token ,你会发现

"iss": "accounts.google.com",

在那儿的某个地方。

同样,当您直接与用户池集成时,您从用户池获取的 token (稍后将其提供给身份池)包含以下内容:

"iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_xxxxxxxxx",

这样您就可以通过检查用户的 token 来了解用户如何登录。

有关 Cognito 托管 UI 的说明

如果您使用 Cognito 的托管 UI 登录 Google,您将从 Cognito 获得一个 access_token 和一个 id_token(通过重定向)。在这种情况下,所有 token 都来自同一个位置 (Cognito),这意味着您不能依赖任何调用代码来知道谁是真正的身份提供商.

在这种情况下,如果您的应用程序关心 IDP 是谁,您就必须破解 token 。 id_token 的这一部分可能是您想要的:

  "identities": [
{
"userId": "100000000000000000000",
"providerName": "Google",
"providerType": "Google",
"issuer": null,
"primary": "true",
"dateCreated": "1507749926267"
}
],
"token_use": "id",
"auth_time": 1509303233,
"name": "Your User",
"exp": 1509306833,
"iat": 1509303233,
"email": "youruser@gmail.com"

关于reactjs - 将 AWS Cognito 与 Google 和 React 自定义登录集成的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46889501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com