In this blog, we will talk about a very special case, where the user changes his password to his current one only, in other words, the user enters the same password in both current password and new password. This case is now being handled by SUSI.AI server.
Considering the example of SUSI.AI Web Chat, we have following dialog when the user tries to change his/her password:
Here the user can add his/her current password and new password. When the new password meets the minimum conditions (minimum 6 characters), then the user can press CHANGE button.
We make ajax call to the server with the following endpoint:
BASE_URL+'/aaa/changepassword.json?'+ 'changepassword=' + email + '&password=' + this.state.passwordValue + '&newpassword=' + this.state.newPasswordValue + '&access_token='+cookies.get('loggedIn');
Here we have 4 parameters:
- changepassword: This takes the email of the current user
- password: This is the password of the current user, which is saved in the state named “passwordValue”
- newpassword: This is the new password which the user enters
- access_token: These are access tokens which are fetched from cookies. These are defined on login and are deleted on logout.
This is now handled on the server by a file named PasswordChangeService.java. Here we have to check whether the newpassword and password matches or not.
In this file, we have a function named serviceImpl with return type ServiceResponse and takes in an argument: Query post (Query is the return type). The query is not the only argument, Please read from the file from resources mentioned below for all the argument. To handle our case we just need to work with the post.
We extract the password, newpassword and email as follows:
String useremail = post.get("changepassword", null); String password = post.get("password", null); String newpassword = post.get("newpassword",null);
So to simply handle the case where password and newpassword matches, we define an if block in java and compare these two parameters as follows:
if(password.equals(newpassword)){ result.put("message", "Your current password and new password matches"); result.put("accepted", false); return new ServiceResponse(result); }
Here we put the message as “Your current password and new password matches” and make the accepted flag of result JSON as false. After this, we return the ServiceResponse.
Now in our web chat client, the ajax call is as follows:
$.ajax({ url: changePasswordEndPoint, dataType: 'jsonp', crossDomain: true, timeout: 3000, async: false, statusCode: { 422: function() { let msg = 'Invalid Credentials. Please check your Email or Password.'; let state = this.state; state.msg = msg; this.setState(state); } }, success: function (response) { let msg = response.message+'\n Please login again.'; let state = this.state; state.msg = msg; state.success = true; state.msgOpen = true; this.setState(state); }.bind(this), error: function(jqXHR, textStatus, errorThrown) { let msg = 'Failed. Try Again'; if (status === 'timeout') { msg = 'Please check your internet connection'; } let state = this.state; state.msg = msg; state.msgOpen = true; this.setState(state); }.bind(this) });
In our success method of ajax call, we receive the JSON response in a variable named response and store this in the state in variable msg and set the state of success equal to true. We then use the state and message to handle accordingly.
Our JSON object when both password and new password are same:
So this is how clients can handle accordingly to the message received from the server instead of doing this on their own end.
Resources
- PasswordChangeService.java file on server: https://github.com/fossasia/susi_server/blob/development/src/ai/susi/server/api/aaa/PasswordChangeService.java
- Ajax’s guide on MDN: https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started
- States in React: https://facebook.github.io/react-native/docs/state.html