For users with Email/Password credential in brainCloud, it's quite common to have a forgot-password feature in your app, it can be achieved by using brainCloud's webhook. This article will show your the steps.
Create a cloud code script with a name
webhook_forgotPassword
, paste the following code into the script."use strict";
function main() {
var response = {};
bridge.logDebugJson("Script inputs", data);
var userEmail = data.parameters.email;
response.stringResponse = "webhook is processing user email reseting..." + data.parameters.email;
// validating the user email
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(userEmail)) {
// checking if the email is existed in brainCloud users
var context = {
"pagination": {
"rowsPerPage": 20,
"pageNumber": 1
},
"searchCriteria": {
"emailAddress": userEmail
},
"sortCriteria": {
"playerName": 1
}
};
var userProxy = bridge.getUserServiceProxy();
var postResult = userProxy.sysGetPage(context);
if (postResult.data.results.count > 0) {
var userProfileID = postResult.data.results.items[0].profileId;
// sending password reset email
postResult = userProxy.sysSendPasswordResetEmail(userProfileID);
if (postResult.status == 200) {
response.stringResponse = "password reset email has been sent to user's email:" + userEmail;
} else {
response.stringResponse = "system error with sending email...";
}
} else {
response.stringResponse = "user doesn't exist with the email you provided";
}
} else {
response.stringResponse = "user's email is invalid.";
}
return response;
}
main();Open Design | Cloud Code | WebHooks page, and create a WebHook link to the script you created from the previous step to it. Copy down the
WebHook URL
for the next step.Now, you have completed the work from brainCloud. Next, open your client app editor, define the
WebHook URL
as a variable, and link it to your forgotEmail object (could be a link or button in your app) onClick event, making it trigger this URL when the user clicks the object ( assuming you are using Unity, you can go withApplication.OpenURL
command). Grab user inputted email and append it as an email parameter to the URL as follows.const string passwordResetWebhookUrl = "https://portal.braincloudservers.com/webhook/12832/forgotPassword/6dc675bb-f8b4-495f-ac5e-1f0658bfe09c";
Application.OpenURL(passwordResetWebhookUrl + "?email=" + userEmail);Run your app to test, you should get a similar pop-up window as follows once a user clicks the forgotEmail object
Done, check the reset email received in your test email account...