如何使用AWS Cognito SDK使用NodeJS从REST服务验证用户?

时间:2017-01-28 20:00:09

标签: javascript node.js amazon-web-services asynchronous amazon-cognito

所有!

我目前遇到的问题是使用使用ClaudiaJS + Claudia-API-Builder构建的lambda函数从我的Cognito UserPool验证用户,并通过“/ api / auth”在API网关上公开。我似乎找不到成功调用cognitoUser.authenticateUser的方法。

到目前为止我找到的所有资源都是通过浏览器调用此API,但我找不到在后端执行此操作的任何示例。

我已经尝试将响应作为常规回调,promises和对象侦听器处理,但它们似乎都没有工作,我总是得到一条{“message”:“Internal server error”}消息来自我的REST服务的响应。

同样重要的是,我对Node和Async编程还是一个新手,但是你能不能回顾一下这段代码并告诉我这样做的正确方法,如果这个SDK被设计为从后端调用而不是前端?

我的App.js

var ApiBuilder = require('claudia-api-builder');
var api = new ApiBuilder();

//Routes
var usuario = require('./routes/usuario');
var auth = require('./routes/auth');

//Caminho default da API
var API_ROOT = "simc/api";
//User Routes
api.get(API_ROOT + "/f1", usuario.f1);
api.get(API_ROOT + "/f2", usuario.f2);

//Auth Routes
api.get(API_ROOT + "/login", auth.login);
module.exports = api;

我的身份验证路线

"use strict"
var AWS = require('aws-sdk');
var AWSCognito = require('amazon-cognito-identity-js');

var authAPI = {};


authAPI.login = function() {
     var authenticationData = {
          Username: 'xxxxxx',
          Password: 'xxxxxx',
     };
     var authenticationDetails = new AWSCognito.AuthenticationDetails(authenticationData);
     var poolData = {
          UserPoolId: 'us-east-1_xxxxxx',
          ClientId: 'xxxxxx'
     };


     var userPool = new AWSCognito.CognitoUserPool(poolData);
     var userData = {
          Username: 'xxxxxx',
          Pool: userPool
     };

     //Trying to use Promises
     var cognitoUser = new AWSCognito.CognitoUser(userData);
     return cognitoUser.authenticateUser(authenticationDetails)
          .then(function(result) {
               console.log('access token + ' + result.getAccessToken().getJwtToken());
               //Use the idToken for Logins Map when Federating User Pools with Cognito Identity or when passing through an Authorization Header to an API Gateway Authorizer
               console.log('idToken + ' + result.idToken.jwtToken);
               return result;
          })
          .catch(function(err) {
               console.log(err);
               return err;
          });

     //Trying to use Object Listeners
     var request = cognitoUser.authenticateUser(authenticationDetails).promise();
     return request.on('success', function(response) {
          console.log('access token + ' + response.getAccessToken().getJwtToken());
          //Use the idToken for Logins Map when Federating User Pools with Cognito Identity or when passing through an Authorization Header to an API Gateway Authorizer
          console.log('idToken + ' + response.idToken.jwtToken);
          return response;
     }).
     on('failure', function(err) {
          console.log("Error!");
          return err;
     }).send();

};


module.exports = authAPI;

我修改了原始处理程序,因为它似乎也无法正常工作。

cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            console.log('access token + ' + result.getAccessToken().getJwtToken());
            /*Use the idToken for Logins Map when Federating User Pools with Cognito Identity or when passing through an Authorization Header to an API Gateway Authorizer*/
            console.log('idToken + ' + result.idToken.jwtToken);
            return 'idToken + ' + result.idToken.jwtToken;
        },

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

    });

有没有人知道我做错了什么?

提前致谢, 恩里科贝加莫

1 个答案:

答案 0 :(得分:1)

var authenticationDetails = new AWS.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

使用此功能包含该功能。