Created
October 2, 2015 17:03
-
-
Save jeffhuangtw/ef31f0b5b35b34ce2173 to your computer and use it in GitHub Desktop.
Resend Parse User Verification Email Cloud Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); | |
}); |
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.
- provide a custom /ResetPassword REST API
- user can trigger the this API from APP
- /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());
- User can check the email and get the reset password email by the link in email.
- 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 - 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
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;
});