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.