如何将aws cognito与web联合和javascript一起使用

如何将aws cognito与web联合和javascript一起使用,javascript,amazon-web-services,amazon-cognito,login-with-amazon,Javascript,Amazon Web Services,Amazon Cognito,Login With Amazon,我试图使用AWS Cognito和身份提供者(使用amazon登录)为我的javascript应用程序提供登录功能。经过大量的搜索,我不得不问你们:你们能给我指一个描述这些步骤的好教程吗?我知道有很多文档,但要么是文档不完整,要么是没有使用cognito的文档。 到目前为止,我得到的是: 我已经登记了一份申请 我已使用从(1)获得的应用程序Id创建了一个标识池 我尝试了以下方法来创建具有功能的登录按钮,但这不起作用: <!DOCTYPE html> <html>

我试图使用AWS Cognito和身份提供者(使用amazon登录)为我的javascript应用程序提供登录功能。经过大量的搜索,我不得不问你们:你们能给我指一个描述这些步骤的好教程吗?我知道有很多文档,但要么是文档不完整,要么是没有使用cognito的文档。 到目前为止,我得到的是:

  • 我已经登记了一份申请
  • 我已使用从(1)获得的应用程序Id创建了一个标识池
我尝试了以下方法来创建具有功能的登录按钮,但这不起作用:

<!DOCTYPE html>
<html>
    <head>
        <script src="aws-sdk.js" type="text/javascript"></script>
        <script>
            // Initialize the Amazon Cognito credentials provider
            AWS.config.region = 'eu-west-1'; // Region
            AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                IdentityPoolId: 'xxx',
            });

        </script>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>TODO write content</div>
        <a href id="LoginWithAmazon">
            <img border="0" alt="Login with Amazon"
                 src="https://images-na.ssl-images-amazon.com/images/G/01/lwa/btnLWA_gold_156x32.png"
                 width="156" height="32" />
        </a>
<script>
document.getElementById('LoginWithAmazon').onclick = function() {
    options = { scope : 'profile' };
    amazon.Login.authorize(options, 'MY_REDIRECT_URL');
    return false;
  };
</script>
    </body>
</html>

//初始化Amazon Cognito凭据提供程序
AWS.config.region='eu-west-1';//区域
AWS.config.credentials=新的AWS.CognitoIdentityCredentials({
IdentityPoolId:'xxx',
});
提供头衔
TODO写入内容
document.getElementById('LoginWithAmazon')。onclick=function(){
选项={scope:'profile'};
amazon.Login.authorize(选项“MY_REDIRECT_URL”);
返回false;
};
我没有得到重定向,也没有看到登录弹出窗口。
提前感谢。

您在问题中提到希望使用Amazon Cognito为应用程序提供登录功能

Amazon Cognito支持两个本质上高度解耦的实体:

  • 用户池
  • 身份池
用户池允许您向web或移动应用程序添加身份验证,而身份池允许已验证/未验证的用户访问身份池设置中IAM角色中指定的一组AWS资源。Cognito标识池不充当身份验证器,而是充当授权者,在后端运行get credentials for Identity API调用后,它提供临时AWS凭据

根据我从您的用例中了解到的情况,您希望在JavaScript Web应用程序中有一个“使用Amazon登录”按钮,在成功验证JWT[1]后,该按钮会将您带到一个网页。 为了实现这个用例,应该使用amazoncognito用户池。首先,您需要将Amazon作为身份提供者集成到您创建的用户池中[2]。 之后,您需要在代码中通过为您的用户池启动一个auth对象来提及这一点:

 function initCognitoSDK() {
        var authData = {
            ClientId : '<TODO: your app client ID here>', // Your client id here
            AppWebDomain : '<TODO: your app web domain here>', // Exclude the "https://" part. 
            TokenScopesArray : <TODO: your scope array here>, // like ['openid','email','phone']...
            RedirectUriSignIn : '<TODO: your redirect url when signed in here>',
            RedirectUriSignOut : '<TODO: your redirect url when signed out here>',
            IdentityProvider : '<TODO: your identity provider you want to specify here>', 
                    UserPoolId : '<TODO: your user pool id here>', 
                    AdvancedSecurityDataCollectionFlag : <TODO: boolean value indicating whether you want to enable advanced security data collection>
        }; 
函数initCognitoSDK(){
var authData={
ClientId:“”,//此处是您的客户端id
AppWebDomain:“”,//排除“https://”部分。
TokenScopesArray:,//像['openid'、'email'、'phone']。。。
重定向urisignin:“”,
重定向urisignout:“”,
标识提供程序:“”,
用户池ID:“”,
AdvancedSecurityDataCollectionFlag:
}; 
在开发应用程序时,您可以参考此示例应用程序[3],因为它具有相同的用例

我希望这个答案对你有所帮助

工具书类 [1]

[2]


[3].

您试图用Cognito实现的场景是什么?是集成Amazon登录进行身份验证吗?您使用的是Cognito用户池还是联合身份?是的,我尝试集成Amazon登录进行身份验证。我使用联合身份。非常感谢。这是我需要理解的解释。简短的总结一下用户池和身份池之间的差异非常有帮助。谢谢