javascript - How to force AWS Cognito: signUp() to execute synchronously (nodejs) - Stack Overflow

I am trying to set up a node application that uses the AWS cognito sdk to registerloginconfirmauthen

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.

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.

Share Improve this question asked May 6, 2019 at 14:59 ezennnnezennnn 1,4371 gold badge12 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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)
    });
  });
}

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
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742365400a4430194.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信