Skip to content

Commit

Permalink
Resolve Issue 72 User Input Validation (#85)
Browse files Browse the repository at this point in the history
Added failsafes for user registration
  • Loading branch information
maxcurkovic authored Dec 3, 2023
1 parent 6cab2be commit b88e1b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ public int createUser(HttpServletRequest request) throws IOException {
HashMap<String, String> userData = objectMapper.readValue(jsonData, new TypeReference<HashMap<String, String>>() {});
String username = userData.get("username");
String password = userData.get("password");
for (AppUser appUser: userRepo.findAll()){
if (appUser.getUsername().equals(username)){
return 401;
}
}
AppUser appUser = new AppUser(username, password);
userRepo.save(appUser);
System.out.println(appUser);
Expand Down
22 changes: 18 additions & 4 deletions src/main/resources/static/scripts/registerUser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
const submitButton = $("#registerUser-submit");

const errorMessage = $("#error-message");
/**
* The JavaScript AJAX call for when a new user is registered (i.e. the submit button is clicked).
*/

submitButton.click((e) => {
e.preventDefault();
const dataDictionary = {};
dataDictionary["username"] = $("#username").val();
dataDictionary["password"] = $("#password").val();
if (!$("#username").val()) {
alert("No username inputted. Please provide a username!")
return;
}
else {
dataDictionary["username"] = $("#username").val();
}
if (!$("#password").val()) {
alert("No password inputted. Please provide a username!")
return;
}
else {
dataDictionary["password"] = $("#password").val();
}
var formData = JSON.stringify(dataDictionary);

console.log(formData);
Expand All @@ -21,7 +34,8 @@ submitButton.click((e) => {
contentType: "application/json",
success: function (res) {
console.log('User registered successfully');
if (res === 200) window.location.href = "/";
if (res === 200) {window.location.href = "/"}
else if (res === 401) errorMessage.text("This username already exists!")
},
error: function (xhr, status, error) {
// error handling
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/registerUser.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ <h1>Register a User</h1>
<input type="text" id="username"/>
<p>Password:</p>
<input type="password" id="password"/>
<p id="error-message"></p>
<button type="submit" id="registerUser-submit" class="btn">Register</button>
<a href="/loginUser">Login</a>
</form>
Expand Down

0 comments on commit b88e1b3

Please sign in to comment.