【问题标题】:AWS Cognito js: getCurrentUser() returns nullAWS Cognito js:getCurrentUser() 返回 null
【发布时间】:2017-06-19 10:54:30
【问题描述】:

使用他们github page 上的示例构建一个简单的应用程序。我可以使用 Cognito 登录我的应用程序。我不能做的是注销,因为无论我尝试什么,我都无法获得用户对象。我对其他各种调用无济于事(在他们的 API 页面上找到here)。我发现的唯一other post on SO 不适用,因为我没有使用联合身份。我使用的代码几乎是 github 页面上的逐字记录,但为方便起见,将在此处发布:

登录代码:

        var userName = $('#user_name_login').val();
    var userPassword = $('#user_password_login').val();

    var userData = {Username: userName, Pool : userPool};
    var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);

    var authenticationData = {Username : userName, Password : userPassword};
    var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {

            // now that we've gotten our identity credentials, we're going to check in with the federation so we can
            // avail ourselves of other amazon services
            //

            // critical that you do this in this manner -- see https://github.com/aws/amazon-cognito-identity-js/issues/162
            // for details
            var loginProvider = {};
            loginProvider[cognitoCredentialKey] = result.getIdToken().getJwtToken();

            AWS.config.credentials = new AWS.CognitoIdentityCredentials({                   
                IdentityPoolId: identityPoolId,
                Logins: loginProvider,
            }); 

            // //AWS.config.credentials = AWSCognito.config.credentials;
            // AWSCognito.config.credentials = AWS.config.credentials;

            // //call refresh method in order to authenticate user and get new temp credentials
            // AWS.config.credentials.refresh((error) => {
            //     if (error) {
            //         alert(error);
            //     } else {
            //         console.log('Successfully logged in!');
            //     }
            // });

            // this is the landing page once a user completes the authentication process. we're getting a
            // temporary URL that is associated with the credentials we've created so we can access the
            // restricted area of the s3 bucket (where the website is, bruah).
            var s3 = new AWS.S3();
            var params = {Bucket: '********.com', Key: 'restricted/pages/user_logged_in_test.html'};
            s3.getSignedUrl('getObject', params, function (err, url) {
                if (err) { 
                    alert(err); 
                    console.log(err);

                }
                else {
                    console.log("The URL is", url);
                    window.location = url;
                }

            });

        },

        mfaRequired: function(session){
            new MFAConfirmation(cognitoUser, 'login');
        },

        onFailure: function(err) {
            alert("err: " + err);
        },

    });

我正在尝试通过执行注销:

userPool.getCurrentUser().signOut();

请注意,userPool 等是在另一个文件中定义的,因此被初始化:

    var poolData = {
    UserPoolId : '*****', 
    ClientId : '*****' 
};

var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);

那么我如何让我的用户退出应用程序?

【问题讨论】:

  • 就在您尝试退出之前,如果您在浏览器的控制台中输入localStorage.getItem('CognitoIdentityServiceProvider.<CLIENT_ID>.LastAuthUser'),它会显示当前用户名吗?
  • 不,它显示为空。我已验证 CLIENT_ID 正确

标签: amazon-web-services aws-cognito


【解决方案1】:

如此处所述,将其作为问题结束,结果证明是一个红鲱鱼。如果您正在执行我在上面尝试执行的操作,即使用 cognito 生成签名的 url 以访问位于存储桶中受限“文件夹”中的 html 文件,并且您希望能够从该新窗口位置注销,请制作确保签名的 url 与您的登录页面属于同一域。

例如,如果您登陆 foo.com 是因为您设置了 A 或 CNAME DNS 记录,这样您的用户就不必点击愚蠢的云端或 s3 生成的 url 即可访问您的网站,您需要确保还生成具有相同域名的签名 URL。否则您将无法访问存储桶。此外 - 您将无法访问您的用户对象,因为会话对象与您当前所在的域名不同。

请参阅this,了解有关如何指定签名网址的域的信息。

另外请注意,如果您使用第三方域名注册商,可能会遇到很多麻烦。因为那个,我刚刚烧掉了两个星期的时间:-/

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 2018-03-23
    • 2017-04-21
    • 2018-06-11
    • 2019-06-02
    • 2018-04-24
    • 2019-05-03
    • 2023-04-03
    • 2022-11-24
    相关资源
    最近更新 更多