2017-06-07 62 views
0

github pageの例を使用して簡単なアプリケーションを構築します。 Cognitoを使用してアプリケーションにログインできます。私が何をしようとしても、私はユーザオブジェクトを保持できないので、私ができないことはログアウトです。私は無駄に(APIページでhereが見つかりました)さまざまな他の呼び出しで周りにdorkedしました。私が見つけたother post on SOは、私が連合アイデンティティを使用していないため適用されません。私が使用しているコードは、githubのページに何があるかほとんどそのままですが、便宜上ここに掲載します:AWS Cognito js:getCurrentUser()がnullを返す

ログインコード:

 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と、そのような別のファイルで定義されている、そしてthusly初期化されていることを注意:

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

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

どうすればアプリケーションからユーザーにサインすることができますか?

+0

ログアウトする直前に、ブラウザのコンソールに 'localStorage.getItem( 'CognitoIdentityServiceProvider。 .LastAuthUser')と入力すると、それが表示されます。現在のユーザー名ですか? –

+0

いいえ、nullを示します。 CLIENT_IDが正しいことを確認しました –

答えて

0

これを問題としてクローズすると、ここに記載されているように、赤いニシンが判明しました。バグの制限された「フォルダ」にあるhtmlファイルにアクセスするための署名付きURLを生成するためにcognitoを使用して上でやろうとしていることを実行していて、その新しいウィンドウの場所からログアウトできるようにしたい場合は、署名されたURLがリンク先ページと同じドメインであることを確認してください。

たとえば、あなたがfoo.comに着陸した場合、あなたはあなたの洞窟に到達するためにdoofy cloudfrontやs3 generated urlを叩く必要がないように、AまたはCNAME DNSレコードが設定されているので同じドメイン名を持つ署名付きURLを生成する必要があります。そうしないと、バケットにアクセスできなくなります。さらに、セッションオブジェクトが現在のドメイン名とは異なるドメイン名に設定されているため、ユーザーオブジェクトにアクセスすることができなくなります。

thisを参照して、署名付きURLのドメインを指定する方法を確認してください。

また、サードパーティのドメイン登録者を使用している場合は、多くの問題が発生することにご注意ください。 -/