blackjack
blackjack

Reputation: 1131

How to authenticate users using Lambda functions and AWS Cognito?

I'm trying to authenticate users using a Lambda function written in Node.js in AWS. I'm stuck now. The following code doesn't work as expected:

var AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
    AWS.config.region = 'us-east-1';
      var authenticationData = {
        Username : 'username',
        Password : 'password',
    };
    var authenticationDetails = new AWS.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
    var poolData = { UserPoolId : 'My-Pool-Id',
        ClientId : 'My-Client-Id'
    };
    var userPool = new AWS.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    var userData = {
        Username : 'username',
        Pool : userPool
    };
    var cognitoUser = new AWS.CognitoIdentityServiceProvider.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            console.log('access token + ' + result.getAccessToken().getJwtToken());
           
            console.log('idToken + ' + result.idToken.jwtToken);
        },

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

    });

};

The code above is provided in Cognito Documentation page

But all I got is the following error:

TypeError: AWS.CognitoIdentityServiceProvider.AuthenticationDetails is not a function

Does anyone have some idea about what I should do now ? Thanks

Upvotes: 0

Views: 1754

Answers (1)

doorstuck
doorstuck

Reputation: 2308

You are using AWSCognito but that is not defined. Change it to AWS.

Are you trying to write a wrapper for cognito? You could do this authentication in the client instead of in a Lambda.

Upvotes: 1

Related Questions