Yan Yi
Posts: 0
Joined: Sat Jan 25, 2014 7:38 pm

Database service to change username not working

Following the tutorial at http://docs.appery.io/tutorials/build..., I modified the code so that it would change the username. When I tested, the code successfully sends out email and a success alert - but the username is not changed.

The code also sends out email when the password is wrong and there are no errors in the console.

My changeUserService accepts three request params, username, newUsername and password.
code
var responseBody = {},
requestParams = {},
paramKeys = request.keys();

for (var key = 0; key < paramKeys&#46;length; key++) {
requestParams[paramKeys[key]] = request&#46;get(paramKeys[key]);
}

&#47;&#47; Declare database ID and Master key
var dbId = "***********&quot
var masterKey = "*************&quot

&#47;&#47; Get username, new password and secret code from request parameters
var username = requestParams['username'];
var newUsername = requestParams['newUsername'];
var password = requestParams['password'];

try {
&#47;&#47; Get the user with a given username from the database
var XHRResponse = XHR&#46;send("GET", "https:&#47;&#47;api&#46;appery&#46;io/rest/1/db/users/", {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey
},
"parameters": {
"where": '{"username":"' + username + '"}'
}
});

&#47;&#47; If the user exists, get his id and password from response
if (XHRResponse&#46;body&#46;length) {

Code: Select all

 var userId = XHRResponse&#46;body[0]["_id"]; 
 var passwordDB = XHRResponse&#46;body[0]["password"]; 

 &#47;&#47; If the password from the database matches the password received from user, 
 &#47;&#47; update the username with the new value 
 if (password == passwordDB) { 
   var XHRResponse = XHR&#46;send("PUT", "https:&#47;&#47;api&#46;appery&#46;io/rest/1/db/users/" + userId, { 
     "headers": { 
       "X-Appery-Database-Id": dbId, 
       "X-Appery-Master-Key": masterKey, 
       "Content-Type": "application/json" 
     }, 
     &#47;&#47; Update user's username with a new value 
     "body": { 
       "username": newUsername 
     } 
   }); 
 } 

 &#47;&#47; If the username update was successful, send an email to the user 
 if (XHRResponse&#46;status == 200) { 
   var XHRResponse = XHR&#46;send("POST", "https:&#47;&#47;api&#46;sendgrid&#46;com/api/mail&#46;send&#46;json", { 
     "parameters": { 
       "api_user": "*****", 
       "api_key": "*****", 
       "to": "*****", 
       "toname": "", 
       "subject": "Username Change Complete", 
       "text": "You have successfully changed your username ", 
       "from": "no-reply@test&#46;com" 
     } 
   }); 

   if (XHRResponse&#46;status == 200) { 
     &#47;&#47; If the email was successfully sent, inform the user about it 
     responseBody&#46;message = "Your username was successfully changed&#46; A confirmation email has been sent to your address&quot 
   } else { 
     &#47;&#47; Email was not sent, but the user change was still successful 
     responseBody&#46;message = "Your username was successfully changed&quot 
   } 
 } else { 
   responseBody&#46;message = "Database error&quot 
 } 

} else {
responseBody&#46;message = "User not found&quot
}
response&#46;success(responseBody, "application/json");
} catch (e) {
response&#46;success("message: " + e&#46;message + "\ncode: " + e&#46;code); &#47;&#47;If something goes wrong error message will appear
}

/code

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Database service to change username not working

Hi Yan!

In accordance with user security you can not get user password for any reasons (compare it, store it, etc).

Cause of password not stored in DB as direct string.

You can read more about it here: http://arr.gr/blog/2012/01/storing-pa...

So if you need to be sure user has rights to change the account here is right way:

1 "login" this user with parameter userName and password.

2 Get token (returns on user login request) and change the username or password or other attributes directly from this user.

Besides this way you don't need you to have db masterkey inside. (it's more preferably).

So please read this links links to know how to work with users:

Login: http://docs.appery.io/documentation/b...

User updating: http://docs.appery.io/documentation/b...

If you have any problems with implementation please post screen shot's with what you have tried out and describe what you have problem with.

Return to “Issues”