我正在尝试建立一个使用AWS Cognito SDK注册/登录/确认/认证用户的Node应用程序。
由于代码似乎是异步运行的,因此我目前无法从signUp()方法中获得响应。
我尝试定义一个async函数register_user(...),并将所需参数传递给一个单独的register(...)函数来等待signUp响应,然后在register_user(...)内部继续执行。
导入语句
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');
注册函数
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."
}
用户注册
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."
}
};
我发现即使我已经指定了register函数为await,其行为仍然是异步的。
有没有办法在register_user(...)函数中强制同步运行signUp方法?非常感谢。