JimmyH
Posts: 0
Joined: Sun Jan 25, 2015 5:01 am

Reset Password Server script issues

Hi there,

I have had the password recovery function working, however recently it is not working correctly.

What i have tested:

1) If you enter all the correct parameters (username,newPassword,secretCode) it works correctly.

2) If you enter only a correct username you get the the response of:

Status 200

{
"message": "Your password was successfully changed. A confirmation email has been sent to you"
}

This is incorrect as no secret code or password was entered.

2) If you enter the correct username and wrong secretCode get the same response of

Status 200

{
"message": "Your pasword was successfully changed. A confirmation email has been sent to you"
}

Which is also incorrect, as the wrong secret code was entered.

Below is the sever code script that I am using (it is copied from your tutorial though I made the change as suggested for encodeURIComponent):

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 = "5XXXXXXXXXXb";
var masterKey = "dXXXXXXXXX";

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

Code: Select all

 // If the user exists, get his email, id and secret code from response 
 if (XHRResponse.body.length) { 

     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": "JaXXXXXn", 
                 "api_key": "cXXXXXX", 
                 "to": email, 
                 "toname": "", 
                 "subject": "Password Recovery Complete", 
                 "text": "You have successfully changed your password", 
                 "from": "a href="mailto:admin@XXXXXXX.com" rel="nofollow"admin@XXXXXXX.com/a" 
             } 
         }); 

         if (XHRResponse.status == 200) { 
             // If the email was successfully sent, inform the user about it 
             responseBody.message = "Your password 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 password 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
}

Please help this is driving me crazy.

thanks
Jimmy

JimmyH
Posts: 0
Joined: Sun Jan 25, 2015 5:01 am

Reset Password Server script issues

I think there may be an error in your script.

https://devcenter.appery.io/tutorials...

Line 51 should NOT have a }

it should be placed at line 70 after the first } so that line 70 reads

} } else {

Can you please confirm.

thank
James

JimmyH
Posts: 0
Joined: Sun Jan 25, 2015 5:01 am

Reset Password Server script issues

Hello, can someone please confirm whether this is an error.

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Reset Password Server script issues

Hello Jimmy,

Sorry for the radio silence here, we passed your issue to our development team and will get back to you with the update.

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Reset Password Server script issues

Jimmy,

We updated the tutorial, please copy the code from it and paste it to your app, should be working now. Please don't forget to paste also MasterKey, DbId и Sendgrid credentials.

JimmyH
Posts: 0
Joined: Sun Jan 25, 2015 5:01 am

Reset Password Server script issues

Thanks for that Evgene, I'll check it out.

Micheal Singleton
Posts: 0
Joined: Tue Apr 19, 2016 2:30 pm

Reset Password Server script issues

Everytime I use this script i get "message": "Database error". Did the team ever run across that problem or am i just entering a credential incorrectly?

***I can get it to say things like user not found if i type in a user that isn't in my database, but if the user is in my database i get an error.

***It looks like the secret code is updating in my database, but i'm still getting the database error.

***It also says on my sendgrid dashboard that I am not receiving any requests. So none were delivered / opened / etc... Is there some sort of error in this part of the script?
pre&#47;&#47; If secret code was successfully updated, send email with the code to user with Sendgrid API if (XHRResponse&#46;status == 200 && email) { var XHRResponse = XHR&#46;send("POST", "https:&#47;&#47;api&#46;sendgrid&#46;com/api/mail&#46;send&#46;json", { "parameters": { "api_user": "micheal&#46;singleton@coxoilco&#46;com", "api_key": "L1ttLe70", "to": email, "toname": "", "subject": "Password Recovery", "text": "Your recovery code is: " + secretCode + "&#46; Copy and this code into the field provided&#46;", "from": "micheal&#46;singleton@coxoilco&#46;com" } }); ******Nevermind&#46;&#46;&#46;I used a capitol E for email in my database column name&#46; Thank You/pre

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Reset Password Server script issues

Hello Micheal,

Thank you for update! Glad it works now!

Return to “Issues”