2016-12-06 13 views
3

ブラウザでは、Facebookログイン後にstatusChangeCallbackが呼び出されます。すべてが成功する。 CognitoはIdentity IDを返します。ただし、userPool.getCurrentUser()はnullを返します。 Cognitoは、認証されたユーザーがいるとは考えていません。どうすれば修正できますか?ありがとう。AWS Cognito Facebook経由の認証は成功しましたが、getCurrentUserはnullを返します

function statusChangeCallback(response) { 
    if(response.status == 'connected' && response.authResponse) { 
     testAPI() 

     console.log("FB statusChangeCallback", JSON.stringify(response)) 

     AWSCognito.config.credentials = new AWSCognito.CognitoIdentityCredentials({ 
      IdentityPoolId : '<%=process.env.AWS_USERPOOLGUID%>', // your identity pool id here 
      Logins : { 
       'graph.facebook.com': response.authResponse.accessToken 
      } 
     }); 
     console.log(JSON.stringify(AWSCognito.config.credentials)) 


     AWSCognito.config.region = '<%= process.env.AWS_REGION%>' 

     AWSCognito.config.credentials.refresh(function(error) { 
      if (error) { 
       console.error("AWSCognito.config.credentials.get", error); 
      } else { 
       console.log("Cognito Identity Id", AWSCognito.config.credentials.identityId); 
       console.log('Successfully logged!'); 
       var cognitoUser = userPool.getCurrentUser(); 
       console.log('cognitoUser', cognitoUser); 

      } 
     }); 
    } 
} 

答えて

0
userPool.getCurrentUser(); 

特定のユーザプールに関して認証されたユーザを指します。上記のコードでは、FacebookのIDを使用してAWS資格情報を取得しています。ただし、現在のユーザーは、ユーザープールの最後に認証されたユーザーを参照します。認証に成功すると、ローカルストレージに保存されます。したがって、以下のコードのように、最初に認証する必要があります。あなたはこれに持っているものから、あなたのAWSCognito.config.credentials を変更する必要があるよう

var authenticationData = { 
    Username : 'username', 
    Password : 'password', 
}; 
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData); 
var poolData = { 
    UserPoolId : '...', // Your user pool id here 
    ClientId : '...' // Your client id here 
}; 
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); 
var userData = { 
    Username : 'username', 
    Pool : userPool 
}; 
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); 
cognitoUser.authenticateUser(authenticationDetails, { 
    onSuccess: function (result) { 
     console.log('access token + ' + result.getAccessToken().getJwtToken()); 

     AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
      IdentityPoolId : '...', // your identity pool id here 
      Logins : { 
       // Change the key below according to the specific region your user pool is in. 
       'cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>' : result.getIdToken().getJwtToken() 
      } 
     }); 

     // Instantiate aws sdk service objects now that the credentials have been updated. 
     // example: var s3 = new AWS.S3(); 

    }, 

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

}); 
+2

Facebookログインボタンをクリックした後に認証されたセッションとユーザーを保護する方法については説明していませんが、Username + Passwordの組み合わせログインについては説明していません。 –

0

はルックス:

// Add the Facebook access token to the Cognito credentials login map. 
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
    IdentityPoolId: 'IDENTITY_POOL_ID', 
    Logins: { 
    'graph.facebook.com': response.authResponse.accessToken 
    } 
}); 

// Obtain AWS credentials 
AWS.config.credentials.get(function(){ 
    // Access AWS resources here. 
}); 

注意IdentityPoolId: 'IDENTITY_POOL_ID'、ありませんIdentityPoolId: '<%= process.env.AWS_USERPOOLGUID%>'、//ここにIDプールID

ユーザープールにアクセスしていて、IDENTITYプールにアクセスしていないようです。

FacebookのユーザーはFacebookサーバーのフェデレーションユーザーであるため、ユーザーはIDプールに存在します。

+0

また、AWS.config.credentials.getはAWS.config.credentials.refreshに似ています –

関連する問題