使用AWS-Cognito-Auth SDK注册用户时出错

时间:2019-06-10 04:04:43

标签: javascript amazon-cognito

导入最新的稳定的Cognito Auth SDK时出现以下错误

  

未捕获的语法错误:意外的标识符

在第22行:import CognitoTokenScopes from './CognitoTokenScopes';

当尝试注册新用户时,尽管该用户不存在,但出现“未知错误”,接着是“用户已存在”。用户最终被注册,但是错误使我担心。当引发“用户已经存在”错误时,我也注意到控制台中出现400错误:

  

POST https://cognito-idp.us-west-2.amazonaws.com/ 400

这是我正在使用的代码(如果未配置用户池,则该代码段将无法工作)

var username,
    password,
    personalName,
    poolData

function registerUser() {
    	personalName =  document.getElementById("personalnameRegister").value;	
		email = document.getElementById("emailInputRegister").value;
        username = document.getElementById("usernameRegister").value;
		
		if (document.getElementById("passwordInputRegister").value != document.getElementById("confirmationpassword").value) {
			alert("Passwords Do Not Match!")
			throw "Passwords Do Not Match!"
		} else {
			password =  document.getElementById("passwordInputRegister").value;	
		}
    poolData = {
        UserPoolId : _config.cognito.userPoolId,
        ClientId: _config.cognito.clientId
    };
    var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

    var attributeList =  [];

    var dataEmail = {
        Name : 'email',
        Value : email
    };

    var dataPersonalName = {
        Name: 'name',
        Value: personalName
    };

    var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
    var attributePersonalName = new AmazonCognitoIdentity.CognitoUserAttribute(dataPersonalName);

    attributeList.push(attributeEmail,
                      attributePersonalName);

    userPool.signUp(username, password, attributeList, null, function(err, result) {
        if (err) {
            alert(err.message || JSON.stringify(err));
            return;
        }
        cognitoUser = result.user;
        console.log("user name is " + cognitoUser.getusername());
        document.getElementById("title-header").innerHTML = "Check your email for a verification.";
    });
}

document.getElementById('registerBtn').addEventListener('click', registerUser);
<html lang="en">
    <head>
        <!--Jquery-->
        <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
        <!--Load Registration Scripts-->
        <script src="amazon-cognito-auth.js"></script>
        <script src="https://sdk.amazonaws.com/js/aws-sdk-2.410.0.min.js"></script>
        <script src="amazon-cognito-identity.min.js"></script>
        <script src="config.js"></script>
        
    </head>
    <body>
        <h1 id="titleheader">Register an Account</h1>
        
        	<input type="personalname" class="form-control" id="personalnameRegister" placeholder="Name" pattern=".*" required>
            <input type="username" class="form-control" id="usernameRegister" placeholder="username" pattern=".*" required>
            <input type="email" class="form-control" id="emailInputRegister" placeholder="Email" pattern=".*" required>
            <input type="password" class="form-control" id="passwordInputRegister" placeholder="Password" pattern=".*" required>
            <input type="password" class="form-control" id="confirmationpassword" placeholder="Confirm Password" pattern=".*" required>
            <button id="registerBtn" class="btn btn-lg btn-primary btn-block" type="button" onclick="registerUser()" >Register</button>
        
        <footer>
            <script src="register.js"></script>    
        </footer>        
    </body>
    
    

</html>

0 个答案:

没有答案