Skip to content

Instantly share code, notes, and snippets.

@jeffhuangtw
Created October 2, 2015 17:03
Show Gist options
  • Save jeffhuangtw/ef31f0b5b35b34ce2173 to your computer and use it in GitHub Desktop.
Save jeffhuangtw/ef31f0b5b35b34ce2173 to your computer and use it in GitHub Desktop.
Resend Parse User Verification Email Cloud Code
// resend verify email
Parse.Cloud.define("ResendVerifyEmail", function(request, response) {
var user = Parse.User.current();
if (!user) {
response.error("INVALID_USER");
return;
}
var email = request.params.email;
var query = new Parse.Query(Parse.User);
Parse.Cloud.useMasterKey();
query.equalTo('objectId', user.id);
query.first().then(function(userObj) {
if (userObj != undefined) {
console.log("ResendVerifyEmail:" + user.id + " update email from:" + userObj.get("email") + " to:" + email);
userObj.unset("email"); // set empty
return userObj.save();
} else {
return Parse.Promise.error("INVALID_USER");
}
}).then(function(updatedObj) {
updatedObj.set("email", email); // set email to trigger resend verify Email
return updatedObj.save();
}).then(function(obj) {
response.success('ok');
}, function(error) {
response.error(error);
});
});
@marxxt
Copy link

marxxt commented Sep 27, 2016

Here's a skinnier version...

Parse.Cloud.define("ResendVerifyEmail", function(request, response) {
var user = request.user;
if (!user) {
response.error("INVALID_USER");
return;
}
Parse.Cloud.useMasterKey();
var email = request.params.email;

user.set("email", email);
user.save(null,{
    success: function(userSaved){
        response.success('ok');
    },
    error: function(err){
        response.error(err);
    }
})

});

@jeffhuangtw
Copy link
Author

Parse.Cloud.useMasterKey() deprecated for a long time.
parse-community/parse-server#37

I think it should be as following

user.set("email", email);
user.save(null,{ useMasterKey: true }).then(function (savedUser) {
  response.success('ok');
}, function (error) {
  response.error(err);
});

Btw, In our current design, I do the follwing things.

  1. provide a custom /ResetPassword REST API
  2. user can trigger the this API from APP
  3. /ResetPassword will check if there is user mapping with the request email.
    If there is user matched, server will trigger an reset email with mailgun/mandril with forgot password email template
    generate the encrypted token in the email link
// refer to https://stackoverflow.com/questions/20460087/issues-running-crypto-js-on-parse-cloud-code-for-ios-application/27497184#27497184
let encrypted_token = ciphering.encrypt("_SOME_PREFIX_ " + user.id + " " + user.get("email") + " " + Date.now());
  1. User can check the email and get the reset password email by the link in email.
  2. The forgot password email link can bring user to a custom web page, this web page will verify the token of the link.
    If the token is verified, show the reset password UI
  3. User Reset their password on Web UI, the token is passed in again to verify this action is valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment