0

So, I am using (or at least trying to) Amazon Cognito with Lambda functions for auth. Here's the flow: I send request, it goes to API Gateway, which directs it to a specific Lambda function. I am using Node JS with amazon-cognito-identity-js library. I am able to register user. The thing is that, Cognito sends email with the confirmation code after the registration. I am unable to create another Lambda (API endpoint) function for confirmation, since it requires CognitoUser object (which you receive after registering or login). Here is the code from AWS documentation:

cognitoUser.changePassword('oldPassword', 'newPassword', function(err, result) {
        if (err) {
            alert(err);
            return;
        }
        console.log('call result: ' + result);
    });

So basically, it's not designed for Lambda functions, since it requires to save the state - the user after the registration. Am I getting it wrong? Is there a way?

funtik
  • 1,688
  • 3
  • 11
  • 27

1 Answers1

3

Oh, ok, my bad. Apparently you can create a CognitoUser object using only username and user pool:

const poolData = {    
    UserPoolId : process.env.COGNITO_USER_POOL_ID,
    ClientId : process.env.COGNITO_CLIENT_ID
};
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
...
const userData = { 
        Username : email,
        Pool : userPool
};

and then you can call

cognitoUser.confirmRegistration(confirmationCode, true, function(err, result) {
            if (err) {
                alert(err);
                return;
            }
            alert(result);
        });
funtik
  • 1,688
  • 3
  • 11
  • 27
  • how do you know the email of the user? the login happens all at a cognito login page and you just receive a code!? in theory I must validate the code againts the Cognito API and get a token WITH the user data (including the email address) – Ari Waisberg May 14 '23 at 14:11