示例
var app = {};
app.signUp = function(){
app.userName = $('#userName').val();
app.password = $('#password').val();
app.email = $('#form-email').val();
app.phoneNumber = $('#form-phone').val();
app.emailRegex = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
/*
Put the User input validation logic here.
*/
if (!app.userName) {
alert("Please provide a user name");
return;
}
if (!app.password) {
alert("Please provide a password");
return;
}
if (!app.email) {
alert("Please provide an Email address");
return;
}
if(!app.emailRegex.test(app.email)){
alert("Please provide a valid Email address");
return;
}
if (!app.phoneNumber) {
alert("Please provide a Phone Number");
return;
}
AWS.config.region = 'us-east-1'; //地区
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: '...' //您的身份池ID在这里
});
AWSCognito.config.region = 'us-east-1';
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: '...' //您的身份池ID在这里
});
//需要提供占位符密钥,除非为用户池启用了未经授权的用户访问
AWSCognito.config.update({accessKeyId: 'anything', secretAccessKey: 'anything'})
var poolData = {
UserPoolId : APP_CONSTANT.USER_POOL_ID,
ClientId : APP_CONSTANT.CLIENT_ID
};
userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var attributeList = [];
var dataEmail = {
Name : 'email',
Value :app.email//确认码将发送到的电子邮件ID。
};
var dataPhoneNumber = {
Name : 'phone_number',
Value : app.phoneNumber
};
var attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
//确认号码格式后,请在下面取消注释
/*var attributePhoneNumber = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);*/
attributeList.push(attributeEmail);
/* attributeList.push(attributePhoneNumber);*/
//将收集自用户的用户ID和密码放在下面进行注册
userPool.signUp(app.userName, app.password, attributeList, null, function(err, result){
if (err) {
alert(err);
return;
}
cognitoUser = result.user;
//用户注册后返回用户名,仍然需要用确认码确认发送到邮件。
$("#form-confirmCode").css("display", "block");
alert('user name is ' + cognitoUser.getUsername());
//现在,由于用户已注册并等待确认,请禁用所有以前的输入,但禁用确认代码。
$("#userName").prop("readonly", true);
$("#password").prop("readonly", true);
$("#form-email").prop("readonly", true);
$("#form-phone").prop("readonly", true);
$("#signUpBtn").hide();
$("#confirm-block").show();
var confirmationCode = prompt("Hello "+cognitoUser.getUsername+" Enter the confirmation code sent to your email address.","Confirmation code here");
cognitoUser.confirmRegistration(confirmationCode,true,function(err,result){
if(err){
alert(err);
return;
}
console.log('Call Result: '+result);
});
return;
});
};