2017-09-14 108 views
0

如何在不使用身份池/ IdentityPoolId的情況下對用戶進行身份驗證,僅使用用戶池憑據? https://github.com/aws/amazon-cognito-identity-js使用Cognito用戶池憑證對用戶進行身份驗證

實施例4中鏈接以上僅適用於/與身份池, 當我打電話例如方法

cognitoUser.changePassword('oldPassword', 'newPassword', function(err, result) {} 

retrun我從CognitoUser.js誤差,串602-604

if (!(this.signInUserSession != null && this.signInUserSession.isValid())) { 
     return callback(new Error('User is not authenticated'), null); 
    } 

但是,當我打電話

cognitoUser.getSession(function(err, session) {if (err) { 
       alert(err); 
       return; 
      } 
      console.log('session validity: ' + session.isValid()); 

它給我會話令牌。

如何,我想驗證用戶:

const logins = {}; 
logins['cognito-idp.' + environment.region + '.amazonaws.com/' + environment.UserPoolId] = session.getIdToken().getJwtToken(); 
// Add the User's Id Token to the Cognito credentials login map. 
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
    Logins: logins 
}); 

,讓我錯誤

Argument of type '{ Logins: {}; }' is not assignable to parameter of type 'CognitoIdentityOptions'. 

我想要實現:

1)如何理解cognito用戶池用戶身份驗證沒有身份池?

2)如何認證用戶?

3)對象CognitoUser有2個propreties:

  • 會議
  • signInUserSession

又是爲了什麼? 如何正確使用它們?

P.S.當即時通訊使用這種方式,一切工作正常,但我需要達到它沒有身份池

const creds = new AWS.CognitoIdentityCredentials({ 
IdentityPoolId: environment.IdentityPoolId, // your identity pool id here 
Logins: { 
// Change the key below according to the specific region your user pool is in. 
[`cognito-idp.${environment.region}.amazonaws.com/${environment.UserPoolId}`]: session.getIdToken().getJwtToken()}}, 
{ 
region: environment.region 
}); 
AWS.config.credentials = creds; 

回答

0

你想在第一個地方做什麼?身份池,身份池ID在Cognito聯合身份的上下文中使用。通過與Facebook,Google或Cognito用戶池等不同身份提供商進行聯合,Cognito聯合身份驗證可用於賒購AWS Credentials。

您指出的SDK是Cognito用戶池的SDK。用戶池可以被視爲可用於身份驗證的用戶數據目錄,並且是Cognito聯合身份的身份提供者。

您正在調用changePassword方法,該方法是用戶池(用戶需要進行身份驗證)上下文中的身份驗證方法。 GetSession只是從本地存儲中檢索當前用戶。你的用例究竟是什麼,你想達到什麼目的?

+0

感謝解釋,問題更新, 的getSession - >給我會話令牌idToken:CognitoIdToken,refreshToken:CognitoRefreshToken,的accessToken:CognitoAccessToken) getCurrentUser() - 給我的用戶從localStorage的 –

0

我覺得對於您的登錄地圖,您有一個包含數組的地圖,而示例只有一個如下圖的地圖。

  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() 
      } 
+0

沒有問題不在這個 –

相關問題