0

我在我的 lambda 中使用了来自 cognito nodejs sdk 的登录功能来验证用户身份,但在我测试它时它不起作用。它在 lambda 控制台中以未知错误响应。

但是,它在我的静态网站上的代码中有效。

请帮忙。

这是我的 nodejs lambda 函数。

global.fetch = require('node-fetch')
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');

/**
params:
  userPoolId (string): aws cognito user pool id
  clientId (string): aws cognito client id
  username (string): aws cognito user username belonging to same userPool
  password (string): aws cognito user password belonging to same userPool
**/

exports.handle = function(event, context, callback) {
  //aliasing to make code readable
  var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
  var AuthenticationDetails = AmazonCognitoIdentity.AuthenticationDetails;
  var CognitoUser = AmazonCognitoIdentity.CognitoUser;
  var AuthenticationDetails = AmazonCognitoIdentity.AuthenticationDetails;

  console.log(event);

  //aws credentials
  var poolData = {
      UserPoolId : event.userPoolId,//'us-east-2_57jpQupfc', // Your user pool id here
      ClientId : event.clientId//'7ec9h7hhtchermdsceg3v2p5ar' // Your client id here
  };

  var username = event.username;
  var password = event.password;

  var authenticationData = {
      Username : username,
      Password : password
  };

  var authenticationDetails = new AuthenticationDetails(authenticationData);

  var userPool = new CognitoUserPool(poolData);

  var userData = {
      Username : username,
      Pool : userPool
  };

  var cognitoUser = new CognitoUser(userData);

  //sign in
  cognitoUser.authenticateUser(authenticationDetails, {
      onSuccess: function (result) {
          var tokens = {};
          var accessToken = result.getAccessToken().getJwtToken();
          var idToken = result.getIdToken().getJwtToken();

          tokens.accessToken = accessToken;
          tokens.idToken = idToken;
          /*
          //POTENTIAL: Region needs to be set if not already set previously elsewhere.
          AWS.config.region = '<region>';

          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()
              }
          });

          //refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
          AWS.config.credentials.refresh((error) => {
              if (error) {
                   console.error(error);
              } else {
                   // Instantiate aws sdk service objects now that the credentials have been updated.
                   // example: var s3 = new AWS.S3();
                   console.log('Successfully logged!');
              }
          });*/
          callback(null, tokens);
      },

      onFailure: function(err) {
          //callback(err.message || JSON.stringify(err));
          callback(JSON.stringify(err), event);
      },

  });


}

这是我的 package.json

{
  "name": "sign-in",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "json-loader": "^0.5.7",
    "webpack": "^4.8.1",
    "webpack-cli": "^2.1.3"
  },
  "dependencies": {
    "amazon-cognito-identity-js": "^2.0.3"
  }
}

这是我的 webpack.config

var path = require('path');

module.exports = {
  entry: './index.js',
  // Place output files in `./dist/my-app.js`
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'my-app.js'
  },
  module: {
    rules: [
      {
        test: /\.json$/,
        loader: 'json-loader'
      }
    ]
  }
};
4

1 回答 1

0

要回答为什么您的 lambda 在它似乎工作时报告错误,是因为 的第一个参数callback不为空。

callback("hello12", tokens);"hello12"由于第一个参数为空或不为空,将使 Lambda 报告错误。请参阅文档以查看回调的签名定义。尝试设置callback(null, tokens)

但是,您可能还希望将 Lambda 的回调放在刷新的函数回调中,如下所示。

//refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
      AWS.config.credentials.refresh((error) => {
          if (error) {
               console.error(error);
               callback(error); <-- here
          } else {
               console.log('Successfully logged!');
               callback(null, tokens); // <-- and here
          }
      });*/
于 2018-05-16T00:43:43.097 回答