我正在使用Parse提供的电子邮件验证功能,希望我的用户能够在发送失败或看不到它时重新发送电子邮件验证。上次我看到,Parse没有提供执行此操作的固有方法(愚蠢的),人们一直在半信半疑地编写代码来更改电子邮件,然后将其更改回以触发重新发送。对此是否有任何更新,还是更改电子邮件原件和回寄的唯一方法?谢谢
您只需要将电子邮件更新为现有值即可。这应该触发另一个电子邮件验证发送。我还无法测试代码,但这应该是您在各种平台上执行的方式。
// Swift
PFUser.currentUser().email = PFUser.currentUser().email
PFUser.currentUser().saveInBackground()
// Java
ParseUser.getCurrentUser().setEmail(ParseUser.getCurrentUser().getEmail());
ParseUser.getCurrentUser().saveInBackground();
// JavaScript
Parse.User.current().set("email", Parse.User.current().get("email"));
Parse.User.current().save();
您必须将电子邮件地址设置为一个假的保存,然后将其设置回原始地址,然后解析将触发验证过程。仅将其设置为原来的值将不会触发该过程。
iOS
if let email = PFUser.currentUser()?.email {
PFUser.currentUser()?.email = email+".verify"
PFUser.currentUser()?.saveInBackgroundWithBlock({ (success, error) -> Void in
if success {
PFUser.currentUser()?.email = email
PFUser.currentUser()?.saveEventually()
}
})
}
如上所述,要重新发送验证电子邮件,您必须先修改然后重置用户电子邮件地址。要以安全有效的方式执行此操作,可以使用以下云代码功能:
Parse.Cloud.define("resendVerificationEmail", async function(request, response) {
var originalEmail = request.params.email;
const User = Parse.Object.extend("User");
const query = new Parse.Query(User);
query.equalTo("email", originalEmail);
var userObject = await query.first({useMasterKey: true});
if(userObject !=null)
{
userObject.set("email", "tmp_email_prefix_"+originalEmail);
await userObject.save(null, {useMasterKey: true}).catch(error => {response.error(error);});
userObject.set("email", originalEmail);
await userObject.save(null, {useMasterKey: true}).catch(error => {response.error(error);});
response.success("Verification email is well resent to the user email");
}
});
之后,您只需要从客户端代码中调用云代码功能。在Android客户端中,您可以使用以下代码(Kotlin):
fun resendVerificationEmail(email:String){
val progress = ProgressDialog(this)
progress.setMessage("Loading ...")
progress.show()
val params: HashMap<String, String> = HashMap<String,String>()
params.put("email", email)
ParseCloud.callFunctionInBackground("resendVerificationEmail", params,
FunctionCallback<Any> { response, exc ->
progress.dismiss()
if (exc == null) {
// The function executed, but still has to check the response
Toast.makeText(baseContext, "Verification email is well sent", Toast.LENGTH_SHORT)
.show()
} else {
// Something went wrong
Log.d(TAG, "$TAG: ---- exeception: "+exc.message)
Toast.makeText(
baseContext,
"Error encountered when resending verification email:"+exc.message,
Toast.LENGTH_LONG
).show()
}
})
}