2017-04-05 3 views
2

사용자 풀과 함께 AWS Cognito를 사용하여 API를 통해 인증을 받으려고합니다. AWS Cognito User Pool을 통해 생성 된 사용자가 있는데 사용자와 로그인하려고합니다.AWS - nodejs SDK - CognitoIdentityServiceProvider.initiateAuth - CredentialsError : 설정에 자격 증명이 누락되었습니다.

내가받는 오류는 CredentialsError: Missing credentials in config입니다. 특히 그것은 IdentityPoolId이 필요하다는 것을 나에게 말하고 있었다. 하지만 내 AWS 콘솔에서이 IdentityPoolId을 찾지 못했습니다. 도데체 어디에서 내 사용자 풀을 얻을 수 있습니까? 내가보기에는 풀 ID와 풀 ARN 만 있습니다.

enter image description here

관련 소스 코드 : authParams 객체의 경우

var aws = require('aws-sdk'); 
aws.config.update({ 
    region: 'us-east-1', 
    credentials: new aws.CognitoIdentityCredentials({ 
     IdentityPoolId: '???' 
    }) 
}); 


var authUser = function(params, callback) 
{ 
    if (!params || !params.Email || !params._password) 
    { 
     callback(new Error('Invalid parameters.')); 
     return false; 
    } 

    var cognito = new aws.CognitoIdentityServiceProvider(); 

    var authParams = { 
     AuthFlow: 'USER_SRP_AUTH', // not sure what this means... 
     ClientId: conf.AWSConfig.ClientId, 
     AuthParameters: { 
      Username: params.Email, 
      Password: params._password 
     } 
    }; 

    cognito.initiateAuth(authParams, function(err, data) 
    { 
     if (err) 
     { 
      console.log('Error details: ' + util.inspect(err)); 
      callback(err); 
      return false; 
     } 
     callback(null, {success: true, data: data}); 
    }); 
} 

, 나는 AuthFlow 될해야하는지 모르겠어요. http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#initiateAuth-property을 보면 USER_SRP_AUTH과 같은 것 같습니다.

편집 :

나는 나의 IdentityPoolId을 얻기 위해 어디 발견 할 수 있습니다 생각합니다. 내 Federated Identities 섹션을보고 ID 풀을 편집하는 동안 Authentication Providers 섹션 아래에 적절한 User Pool을 추가했습니다.

enter image description here

나는 그 사용자 풀의 User Pool IDApp Client ID를 입력하여 만들어 내 사용자 풀에 Cognito에 대한 Authentication Provider을 관련. 이제 같은 코드를 사용하여 Identity pool ID 나는 CredentialsError: Missing credentials in config 오류가 나타납니다. 그것은 Unauthorized access is not supported for this identity pool라고합니다. 확인 ... 사용자를 인증하려고합니다 ... 인증되지 않은 사용자가 인증 할 수 있도록 인증되지 않은 역할을 만들어야합니까? 그게 내가해야 할 일이라면 어리석은 것처럼 보입니다.

편집 2 :

은 내가 로그인 자바 스크립트 SDK를 (하지 nodejs)를 사용하여뿐만 아니라 IdTokenRefreshTokenAccessToken를 얻을 수 있었던 것 또한주의해야한다. 나는 IdentityPoolId의 필요없이 이것을했다. 내가 필요한 유일한 것들은 UserPoolIdClientId입니다.

var authenticateUser = function(onSuccessCallback) 
{ 
    var authData = { 
     Username: getUserName(), // gets username from an html text field 
     Password: getPassword() // gets password from an html password field 
    }; 

    var authDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authData); 

    var cognitoUser = getCognitoUser(); 

    cognitoUser.authenticateUser(authDetails, 
    { 
     onSuccess: function(result) 
     { 
      console.log('access token: ' + result.getAccessToken().getJwtToken()); 
      console.log('idToken: ' + result.idToken.jwtToken); 
      console.log(result); 

      if (onSuccessCallback && typeof(onSuccessCallback) == 'function') 
      { 
       onSuccessCallback(cognitoUser); 
      } 
     }, 
     onFailure: function(err) 
     { 
      // UserNotConfirmedException: User is not confirmed. 
      console.log('authError'); 
      alert(err); 
     } 
    }); 
} 

답변

2

initiateAuth 내가 일을 할 원하는 것을 전혀 아니라고 밝혀졌습니다. 또한 amazon-cognito-identity-js이라는 npm 패키지가 누락되었습니다. 설치 후 다음과 같이 코드를 업데이트했습니다.

var authUser = function(params, callback) 
{ 
    if (!params || !params.Email || !params._password) 
    { 
     callback(new Error('Invalid parameters.')); 
     return false; 
    } 

    var poolData = { 
     UserPoolId: conf.AWSConfig.UserPoolId, 
     ClientId: conf.AWSConfig.ClientId 
    }; 

    var userPool = new aws.CognitoIdentityServiceProvider.CognitoUserPool(poolData); 
    var authData = { 
     Username: params.Email, 
     Password: params._password 
    }; 

    var authDetails = new aws.CognitoIdentityServiceProvider.AuthenticationDetails(authData); 
    var userData = { 
     Username: params.Email, 
     Pool: userPool 
    }; 

    var cognitoUser = new aws.CognitoIdentityServiceProvider.CognitoUser(userData); 

    cognitoUser.authenticateUser(authDetails, { 
     onSuccess: function(result) 
     { 
      callback(null, {success: true, data: result}); 
     }, 
     onFailure: function(err) 
     { 
      console.log('authUser error: ' + util.inspect(err)); 
      callback(err); 
     } 
    }); 
} 

이제 토큰을 사용하여 성공적으로 응답하고 있습니다! 만세!

관련 문제