简体   繁体   English

Parse.com 重新发送验证电子邮件

[英]Parse.com resend verification email

I am using the email verification feature that Parse offers and would like my users to be able to resend the email verification if it fails to send or they cannot see it.我正在使用 Parse 提供的电子邮件验证功能,并希望我的用户能够在发送失败或看不到它时重新发送电子邮件验证。 Last I saw, Parse does not offer an intrinsic way to do this (stupid) and people have been half-hazzerdly writing code to change the email and then change it back to trigger a re-send.最后我看到,Parse 没有提供一种内在的方式来做到这一点(愚蠢),人们一直在半心半意地编写代码来更改电子邮件,然后将其改回以触发重新发送。 Has there been any updates to this or is changing the email from the original and back still the only way?对此是否有任何更新,或者将电子邮件从原始电子邮件更改回来仍然是唯一的方法? Thanks谢谢

You should only need to update the email to its existing value.您应该只需要将电子邮件更新为其现有值。 This should trigger another email verification to be sent.这应该会触发另一个要发送的电子邮件验证。 I haven't been able to test the code, but this should be how you do it for the various platforms.我无法测试代码,但这应该是您在各种平台上进行测试的方式。

// 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();

You have to set the email address to a fake one save and then set it back to the original and then parse will trigger the verification process.您必须将电子邮件地址设置为伪造的保存,然后将其设置回原始地址,然后解析将触发验证过程。 Just setting it to what it was will not trigger the process.只是将它设置为它的原样不会触发该过程。

iOS 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()
                    }
                })
            }

Poking around the source code for Parse server, there doesn't seem to be any public api to manually resend verification emails.浏览解析服务器的源代码,似乎没有任何公共 api 来手动重新发送验证电子邮件。 However I was able to find 2 undocumented ways to access the functionality.但是,我能够找到 2 种未公开的方法来访问该功能。

The first would be to use the internal UserController on the server (for instance from a Cloud function) like this:第一种方法是使用服务器上的内部UserController (例如来自 Cloud 函数),如下所示:

import { AppCache } from 'parse-server/lib/cache'

Cloud.define('resendVerificationEmail', async request => {
    const userController = AppCache.get(process.env.APP_ID).userController
    await userController.resendVerificationEmail(
        request.user.get('username')
    )

    return true
})

The other is to take advantage of an endpoint that is used for the verification webpage:另一种是利用用于验证网页的端点:

curl -X "POST" "http://localhost:5000/api/apps/press-play-development/resend_verification_email" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{ "username": "7757429624" }'

Both are prone to break if you update Parse and internals get changed, but should be more reliable than changing the users email and then changing it back.如果您更新 Parse 并且内部结构发生变化,两者都容易中断,但应该比更改用户电子邮件然后再改回来更可靠。

We were setting emails to an empty string, but found that there was a race condition where 2 users would hit it at the same time and 1 would fail because Parse considered it to be a duplicate of the other blank email.我们将电子邮件设置为空字符串,但发现存在竞争条件,其中 2 个用户会同时点击它,而 1 个用户会失败,因为 Parse 认为它是另一个空白电子邮件的副本。 In other cases, the user's network connection would fail between the 2 requests and they would be stuck without an email.在其他情况下,用户的网络连接将在 2 个请求之间失败,并且他们将在没有电子邮件的情况下卡住。

Now, with Parse 3.4.1 that I'm testing, you can do (for Javascript):现在,使用我正在测试的 Parse 3.4.1,您可以执行以下操作(对于 Javascript):

Parse.User.requestEmailVerification(Parse.User.current().get("email"));

BUT NOTE that it will throw error if user is already verified.但请注意,如果用户已经过验证,它将引发错误。

Just curious also, are people still using the open-sourced Parse nowadays?也很好奇,现在人们还在使用开源 Parse 吗? Because when I searched online, I kept getting old posts from stackoverflow.因为当我在网上搜索时,我不断地从 stackoverflow 获取旧帖子。

Reference: http://parseplatform.org/Parse-SDK-JS/api/3.4.1/Parse.User.html#.requestEmailVerification参考: http : //parseplatform.org/Parse-SDK-JS/api/3.4.1/Parse.User.html#.requestEmailVerification

To resend the verification email, as stated above, you have to modify then reset the user email address.要重新发送验证电子邮件,如上所述,您必须修改然后重置用户电子邮件地址。 To perform this operation in secure and efficient way, you can use the following cloud code function:为了安全高效地执行此操作,您可以使用以下云代码功能:

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");
}

}); });

After that, you just need to call the cloud code function from your client code.之后,您只需要从您的客户端代码中调用云代码功能。 From Android client, you can use the following code (Kotlin):从 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()

            }
        })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM