Permalink
Browse files

Merge pull request #62 from stevetraut/master

Adding a check to see if a username already exists.
  • Loading branch information...
2 parents 4580409 + 1c9bce9 commit 33ec0c495b468d8a96f6700480b0bcf750a83c32 @r3b r3b committed Feb 5, 2014
Showing with 36 additions and 11 deletions.
  1. +5 −2 samples/usersAndGroups/index.html
  2. +31 −9 samples/usersAndGroups/js/index.js
View
7 samples/usersAndGroups/index.html
@@ -51,7 +51,7 @@
User to Group</a>
</form>
</div>
-
+
<div data-role="popup" id="messagePopup">
<p></p>
</div>
@@ -68,6 +68,8 @@
<label for="fld_user_name"> Username </label>
<input name="" id="fld_user_name" placeholder="" value="" type="text" />
</div>
+ <div id="usernameMessageText">
+ </div>
<div data-role="fieldcontain" data-controltype="textinput">
<label for="fld_name"> Name </label>
<input name="" id="fld_name" placeholder="" value="" type="text" />
@@ -80,7 +82,7 @@
<label for="fld_password"> Password </label>
<input name="" id="fld_password" placeholder="" value="" type="password" />
</div>
- <a data-role="button" href="#page_view_users_list" id="btn_add_user"> Add User</a>
+ <a data-role="button" id="btn_add_user"> Add User</a>
</form>
</div>
</div>
@@ -155,5 +157,6 @@
</ul>
</div>
</div>
+
</body>
</html>
View
40 samples/usersAndGroups/js/index.js
@@ -129,22 +129,44 @@ function getGroups() {
* Adds a new user to the database.
*/
function addUser() {
+ $('#usernameMessageText').empty();
+
// Variable to collect data to send with the request.
var userName = $("#fld_user_name").val(); // Must be unique.
var name = $("#fld_name").val();
var email = $("#fld_email").val();
var password = $("#fld_password").val();
- // Call an SDK method to create a new user with
- // data collected from the form.
- apigeeClient.signup(userName, password, email,
- name, function (error, entity, data) {
+ // Options representing the new user to add.
+ var options = {
+ 'username':userName,
+ 'type':'users'
+ }
+ // Call an SDK method to get an entity representing
+ // the user. If no user is returned, then add one.
+ apigeeClient.getEntity(options, function(error, entity, data){
if (error) {
- var message = "Unable to add a user. " + data;
- apigeeClient.logError({tag:"addUser", logMessage:message})
+ var errorMessage = data["error"];
+ if (errorMessage == "service_resource_not_found"){
+ // Call an SDK method to create a new user with
+ // data collected from the form.
+ apigeeClient.signup(userName, password, email,
+ name, function (error, entity, data) {
+ if (error) {
+ var message = "Unable to add a user. " + data;
+ apigeeClient.logError({tag:"addUser", logMessage:message})
+ } else {
+ // Refresh the user list to include the new user.
+ getUsers();
+ }
+ });
+ }
+ // Go back to the user list.
+ $.mobile.navigate('#page_view_users_list');
} else {
- // Refresh the user list to include the new user.
- getUsers();
+ // Display a message because there's already a user with that username.
+ var message = "<p style='color:red;font-size:12px'>That username is taken. Please choose another.</p>";
+ $('#usernameMessageText').append(message);
}
});
}
@@ -318,14 +340,14 @@ function displayAddUserToGroup(){
}
}
-
/**
* Displays a popup.
*/
function displayPopup(message){
var options = {
"transition" : "slideup"
}
+
$('#messagePopup').empty();
$('#messagePopup').append(
'<p>' + message + '</p>'

0 comments on commit 33ec0c4

Please sign in to comment.