From 3ea4cd2f9b3808ef645092816d888de406580e6d Mon Sep 17 00:00:00 2001 From: realtradam Date: Sat, 13 Jul 2024 00:28:07 -0400 Subject: implement user registration --- .../blog/web/controllers/ArticleController.java | 6 +- .../com/blog/web/controllers/AuthController.java | 64 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/blog/web/controllers/AuthController.java (limited to 'src/main/java/com/blog/web/controllers') diff --git a/src/main/java/com/blog/web/controllers/ArticleController.java b/src/main/java/com/blog/web/controllers/ArticleController.java index 0cdff15..fc18dd7 100644 --- a/src/main/java/com/blog/web/controllers/ArticleController.java +++ b/src/main/java/com/blog/web/controllers/ArticleController.java @@ -20,7 +20,7 @@ public class ArticleController { this.articleService = articleService; } - @GetMapping("/") + @GetMapping("/articles") public String listArticles(Model model) { List articles = articleService.findAllArticles(); model.addAttribute("articles", articles); @@ -85,8 +85,8 @@ public class ArticleController { return "index"; } - @GetMapping("/articles") + @GetMapping("/") public String getArticles() { - return "redirect:/"; + return "redirect:/articles"; } } diff --git a/src/main/java/com/blog/web/controllers/AuthController.java b/src/main/java/com/blog/web/controllers/AuthController.java new file mode 100644 index 0000000..bb7cb0d --- /dev/null +++ b/src/main/java/com/blog/web/controllers/AuthController.java @@ -0,0 +1,64 @@ +package com.blog.web.controllers; + +import com.blog.web.dto.RegistrationDto; +import com.blog.web.models.UserEntity; +import com.blog.web.services.UserService; +import jakarta.validation.Valid; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; + +@Controller +public class AuthController { + private UserService userService; + + public AuthController(UserService userService) { + this.userService = userService; + } + + @GetMapping("/login") + public String loginPage() { + return "auth/login"; + } + + @GetMapping("/register") + public String getRegisterForm(Model model) { + RegistrationDto user = new RegistrationDto(); + model.addAttribute("user", user); + return "auth/register"; + } + + @PostMapping("/register/save") + public String register(@Valid @ModelAttribute("user")RegistrationDto user, + BindingResult result, + Model model) { + UserEntity existingUserEmail = userService.findByEmail(user.getEmail()); + if( + existingUserEmail != null && + existingUserEmail.getEmail() != null && + !existingUserEmail.getEmail().isEmpty() + ) { + result.rejectValue("email", "There is already a user with this email"); + } + + UserEntity existingUsername = userService.findByUsername(user.getUsername()); + if( + existingUsername != null && + existingUsername.getUsername() != null && + !existingUsername.getUsername().isEmpty() + ) + { + result.rejectValue("username", "There is already a user with this username"); + } + + if(result.hasErrors()) { + model.addAttribute("user", user); + return "register"; + } + userService.saveUser(user); + return "redirect:/articles?success"; + } +} -- cgit v1.2.3