javascript - 如何将 aws cognito 与 Web 联合和 javascript 结合使用

标签 javascript amazon-web-services amazon-cognito login-with-amazon

我正在尝试将 AWS Cognito 与身份提供商(使用亚马逊登录)一起使用,为我的 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 为您的应用程序提供登录功能。

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

  • 用户池
  • 身份池

用户池允许您向 Web 或移动应用程序添加身份验证,而身份池允许经过验证/未经验证的用户访问身份池设置中 IAM Angular 色中指定的一组 AWS 资源。 Cognito Identity Pools 不充当身份验证器,而是充当授权器,并在后端运行 get-credentials-for-identity API 调用后出售临时 AWS 凭证。

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

 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>
        }; 

在开发您的应用程序时,您可以引用此示例应用程序[3],因为它具有相同的用例。

希望这个回答能帮到你。

引用资料

[1]。 https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html

[2]。 https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-social-idp.html

[3]。 https://github.com/aws/amazon-cognito-auth-js/tree/master/sample

关于javascript - 如何将 aws cognito 与 Web 联合和 javascript 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53136200/

相关文章:

amazon-web-services - 当我尝试一起使用/和子函数时出现约束问题

python - 在 AWS S3 中指定 IAM 角色的权限

javascript - 如何从 cognito 异步函数返回响应并将其显示在页面上?

javascript - 使用带有括号表示法的 hasOwnProperty 的 Mdn 引用

javascript - 使用 Ember.js,我如何手动将模板和 View 范围限定到其相应的 Controller ?

javascript - 如何在 jquery 中单击特定的 list_item 时显示特定的图像?

amazon-web-services - 有人可以用硬编码的 "AWS-cognito-identity-poolID"侵入我的 s3 吗?

javascript - Js中如何将输入的text-value转换为对象的属性值?

php - 从 PHP Web 应用程序根据 Azure Active Directory 对用户进行身份验证

javascript - 调用InitiateAuth操作时发生错误(InvalidLambdaResponseException): Unrecognizable lambda output