3

I am trying to set up a node application that uses the AWS cognito sdk to register/login/confirm/authenticate a user.

I am currently unable to get the response from the signUp() method as the code seems to be running asynchronously.

I have tried defining an async function register_user(...) and passing the needed parameters to a separate register(...) function to await the signUp response, before continuing inside register_user(...).

IMPORT STATEMENTS

const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
const CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
const AWS = require('aws-sdk');
const request = require('request');
const jwkToPem = require('jwk-to-pem');
const jwt = require('jsonwebtoken');
global.fetch = require('node-fetch');

REGISTER FUNCTION

function register(userPool, email, password, attribute_list){

    let response;

    userPool.signUp(email, password, attribute_list, null, function(err, result){
        console.log("inside")
        if (err){
            console.log(err.message);
            response = err.message;
            return response;
        } 
        cognitoUser = result.user;
    });

    return "User succesfully registered."

}

REGISTER USER

var register_user = async function(reg_payload){

    email = reg_payload['email']
    password = reg_payload['password']
    confirm_password = reg_payload['confirm_password']

    // define pool data
    var poolData = {
      UserPoolId : cognitoUserPoolId,
      ClientId : cognitoUserPoolClientId
    };

    var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

    var attribute_list = [];

    // define fields needed
    var dataEmail = {
        Name : 'email',
        Value : email
    };

    var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);

    attribute_list.push(attributeEmail);

    if (password === confirm_password){

        console.log("here")

        var result = await register(userPool, email, password, attribute_list);

        console.log(result)

        console.log("here2")

    } else {
        return "Passwords do not match."
    }
};

I found that even when i've defined specified the register function to await, the behavior is still asynchronous.

Is there any way to force the signUp method to run synchronously within the register_user(...) function? Thank you very much.

ezennnn
  • 1,239
  • 11
  • 13

2 Answers2

6

You will need to change your register function to return a Promise if you want to await it in your register_user function

function register(userPool, email, password, attribute_list) {
  return new Promise((resolve, reject) => {
    userPool.signUp(email, password, attribute_list, null, (err, result) => {
      console.log('inside');
      if (err) {
        console.log(err.message);
        reject(err);
        return;
      }
      cognitoUser = result.user;
      resolve(cognitoUser)
    });
  });
}
SAndriy
  • 670
  • 2
  • 15
  • 25
djheru
  • 3,525
  • 2
  • 20
  • 20
  • If everything OK, you get the cognitoUser in the result of the call, but how do you track the error in any ? – Eric May 12 '20 at 21:54
2

don't forget to put await in try and catch like

 try {
        var result = await register(userPool, email, password, attribute_list);

        console.log(result);
    } catch (e) {
        console.error(e); // 30
    }
Salman Memon
  • 21
  • 1
  • 1