最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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
    }
发布评论

评论列表(0)

  1. 暂无评论