diff --git a/src/main/java/org/ros/chatto/controller/RegistrationController.java b/src/main/java/org/ros/chatto/controller/RegistrationController.java index cbc395f..e7be335 100644 --- a/src/main/java/org/ros/chatto/controller/RegistrationController.java +++ b/src/main/java/org/ros/chatto/controller/RegistrationController.java @@ -40,6 +40,7 @@ public class RegistrationController { @Autowired private final CaptchaService captchaService; + // FIXME must change this to a timeout base cache otherwise memory leak! private final Map captchaMap = new ConcurrentHashMap<>(); @GetMapping("/registration") @@ -61,32 +62,31 @@ public class RegistrationController { BindingResult bindingResult) { if (bindingResult.hasErrors()) { log.warn("Registration input has errors!"); - return "registration"; + return "redirect:registration?error"; + } + if (userService.getUser(userRegistrationDTO.getUserName()).isPresent()) { + return "redirect:registration?error&duplicate=true"; } - log.debug("Captcha text from user input = {}", - userRegistrationDTO.getCaptchaInput()); - log.debug("Captcha text from captcha map = {}", - captchaMap.get(userRegistrationDTO.getCaptchaID())); - if (userRegistrationDTO.getCaptchaInput() - .equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) { + + log.debug("Captcha text from user input = {}", userRegistrationDTO.getCaptchaInput()); + log.debug("Captcha text from captcha map = {}", captchaMap.get(userRegistrationDTO.getCaptchaID())); + if (userRegistrationDTO.getCaptchaInput().equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) { log.info("Registration captcha equal success"); userService.createUser(userRegistrationDTO); return "redirect:registration?success"; } else { log.warn("Registration captcha equal fail"); - return "redirect:registration?error"; + return "redirect:registration?error&captchaError=true"; } } @GetMapping(value = "/img/captcha/{image_id}", produces = MediaType.IMAGE_PNG_VALUE) - public ResponseEntity getImage( - @PathVariable("image_id") Long imageId) throws IOException { + public ResponseEntity getImage(@PathVariable("image_id") Long imageId) throws IOException { final String captchaText = captchaMap.get(imageId); final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); - BufferedImage captchaBufferedImage = captchaService - .createCaptchaImage(captchaText); + BufferedImage captchaBufferedImage = captchaService.createCaptchaImage(captchaText); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(captchaBufferedImage, "png", baos); diff --git a/src/main/resources/static/js/login.js b/src/main/resources/static/js/login.js index 6e737c0..4841351 100644 --- a/src/main/resources/static/js/login.js +++ b/src/main/resources/static/js/login.js @@ -11,19 +11,19 @@ function storeCredentials() { }, }); //this section is executed when the server responds with no error - jqxhr.done(function() { + jqxhr.done(function () { let authToken = jqxhr.getResponseHeader('X-AUTH-TOKEN'); localStorage.setItem('authToken', authToken); authToken = localStorage.getItem('authToken') - console.log("getting header " + authToken); + // console.log("getting header " + authToken); // secondClick = true; $('#loginForm').submit(); }); //this section is executed when the server responds with error - jqxhr.fail(function() { + jqxhr.fail(function () { log.error('Error retrieving auth token'); - alertify.error('Error retrieving auth token. Please log in again') + alertify.error('Error retrieving auth token. Please try again') secondClick = false; // setTimeout(() => location.reload(), 2000) }) @@ -31,7 +31,7 @@ function storeCredentials() { } -$('#loginForm').on('submit', function(e) { +$('#loginForm').on('submit', function (e) { if (!secondClick) { secondClick = true; e.preventDefault(); diff --git a/src/main/resources/templates/registration.html b/src/main/resources/templates/registration.html index 2663ef3..6b055a9 100644 --- a/src/main/resources/templates/registration.html +++ b/src/main/resources/templates/registration.html @@ -6,7 +6,8 @@ Registration - + @@ -37,7 +38,20 @@

Register

-
+ +
+ An error occured while creating your account. Please try again. +
+ User with the given name already exists. Please use another name. +
+
+ Invalid captcha entered. Please try again. +
+
+
+ Registration was successful. You may now login. +