There is a problem with my password recovery and I'm not sure what it is. I followed the instructions to the letter in the documentation and no email is being sent from sendGrid. When I go to the sendGrid account it shows there was no requests for an email to be sent.
When I test the 'sendEmail' service it says test successful, but when I try to create response parameters from the test response it shows an error.
Here is my server script:
sendEmail:
var responseBody = {},
requestParams = {},
paramKeys = request.keys();
for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}
// Declare database ID and Master key
var dbId = "53bdbe9be4b04e2d7216e2c9";
var masterKey = "fca1c6aa-e77a-41a3-9910-b74ee46f50ed";
// Get username from request parameters
var username = requestParams['username'];
// Generate a random secret code
var secretCode = Math.random().toString(36).slice(-12);
console.log(secretCode);
try {
// Get the user with a given username from the database
var XHRResponse = XHR.send("GET", "https://api.appery.io/rest/1/db/users/", {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey
},
"parameters": {
"where": '{"username": "' + encodeURIComponent(username) + '"}'
}
});
// If the user exists update user's secret code with the generated value
if (XHRResponse.body.length) {
var userId = XHRResponse.body[0]["_id"];
var email = XHRResponse.body[0]["email"];
Code: Select all
var XHRResponse = XHR.send("PUT", "[url=https://api.appery.io/rest/1/db/users/]https://api.appery.io/rest/1/db/users/[/url]" + userId, {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey,
"Content-Type": "application/json"
},
"body": {
"secret_code": secretCode
}
});
// If secret code was successfully updated, send email with the code to user with Sendgrid API
if (XHRResponse.status == 200 && email) {
var XHRResponse = XHR.send("POST", "[url=https://api.sendgrid.com/api/mail.send.json]https://api.sendgrid.com/api/mail.sen...[/url]", {
"parameters": {
"api_user": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a",
"api_key": "XXX(taken out because of public forum",
"to": email,
"toname": "",
"subject": "Password Recovery",
"text": "Your recovery code is: " + secretCode + ". Copy and this code into the field provided.",
"from": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a"
}
});
// If the email was successfully sent, inform the user about it
if (XHRResponse.status == 200) {
responseBody.message = "An email with the recovery code has been sent to you. Please follow the instructions to reset your password";
} else {
responseBody.message = "An error occured while sending the email";
}
} else {
responseBody.message = "Database error";
}
} else {
responseBody.message = "User not found";
}
response.success(responseBody, "application/json");
} catch (e) {
response.success("message: " + e.message + "\ncode: " + e.code); //If something goes wrong, error message appears
}
sendPassword:
var responseBody = {},
requestParams = {},
paramKeys = request.keys();
for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}
// Declare database ID and Master key
var dbId = "53bdbe9be4b04e2d7216e2c9";
var masterKey = "fca1c6aa-e77a-41a3-9910-b74ee46f50ed";
// Get username, new password and secret code from request parameters
var username = requestParams['username'];
var newPassword = requestParams['newPassword'];
var secretCode = requestParams['secretCode'];
try {
// Get the user with a given username from the database
var XHRResponse = XHR.send("GET", "https://api.appery.io/rest/1/db/users/", {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey
},
"parameters": {
"where": '{"username": "' + encodeURIComponent(username) + '"}'
}
});
// If the user exists, get his email, id and secret code from response
if (XHRResponse.body.length) {
Code: Select all
var email = XHRResponse.body[0]["email"];
var userId = XHRResponse.body[0]["_id"];
var secretCodeDB = XHRResponse.body[0]["secret_code"];
// If the secret code from the database matches the secret code received from user,
// update the password with the new value
if (secretCode == secretCodeDB) {
var XHRResponse = XHR.send("PUT", "[url=https://api.appery.io/rest/1/db/users/]https://api.appery.io/rest/1/db/users/[/url]" + userId, {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey,
"Content-Type": "application/json"
},
// Update user's password with a new value
"body": {
"password": newPassword
}
});
}
// If the password update was successful, send an email to the user
if (XHRResponse.status == 200) {
var XHRResponse = XHR.send("POST", "[url=https://api.sendgrid.com/api/mail.send.json]https://api.sendgrid.com/api/mail.sen...[/url]", {
"parameters": {
"api_user": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a",
"api_key": "XXX",
"to": email,
"toname": "",
"subject": "Password Recovery Complete",
"text": "You have successfully changed your password",
"from": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a"
}
});
if (XHRResponse.status == 200) {
// If the email was successfully sent, inform the user about it
responseBody.message = "Your pasword was successfully changed. A confirmation email has been sent to you";
} else {
// Email was not sent, but the password reset was still successful
responseBody.message = "Your pasword was successfully changed";
}
} else {
responseBody.message = "Database error";
}
} else {
responseBody.message = "User not found";
}
response.success(responseBody, "application/json");
} catch (e) {
response.success("message: " + e.message + "\ncode: " + e.code); //If something goes wrong error message will appear
}
I've attached several screenshots for your review. Thanks!