diff options
| author | realtradam <[email protected]> | 2024-07-27 02:00:57 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2024-07-27 02:00:57 -0400 |
| commit | de3d80ce3ea20a869d700c3238020d44059de099 (patch) | |
| tree | ef849326ea4922530990d5dc29cb8ff3532e82fe /backend/src/main/java/com/blog/web/controllers | |
| parent | 6b342f97f6a605b7e1fe34584abbbf962ca39b7c (diff) | |
| download | spring-blog-de3d80ce3ea20a869d700c3238020d44059de099.tar.gz spring-blog-de3d80ce3ea20a869d700c3238020d44059de099.zip | |
working login and auth
Diffstat (limited to 'backend/src/main/java/com/blog/web/controllers')
| -rw-r--r-- | backend/src/main/java/com/blog/web/controllers/ArticleController.java | 26 | ||||
| -rw-r--r-- | backend/src/main/java/com/blog/web/controllers/AuthController.java | 10 |
2 files changed, 17 insertions, 19 deletions
diff --git a/backend/src/main/java/com/blog/web/controllers/ArticleController.java b/backend/src/main/java/com/blog/web/controllers/ArticleController.java index 7ffa2fe..ec8af85 100644 --- a/backend/src/main/java/com/blog/web/controllers/ArticleController.java +++ b/backend/src/main/java/com/blog/web/controllers/ArticleController.java @@ -7,16 +7,14 @@ import com.blog.web.models.UserEntity; import com.blog.web.services.ArticleService; 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.*; import java.time.LocalDateTime; import java.util.HashSet; -import java.util.List; -@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true") +@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true", allowedHeaders = "*") @RestController @RequestMapping("/api/v1") public class ArticleController { @@ -31,7 +29,7 @@ public class ArticleController { @GetMapping("/get") public Article getMethod() { return new Article( - 5, + 5L, "blah", "blah", "blah", @@ -42,12 +40,8 @@ public class ArticleController { } @GetMapping("/articles") - public HashSet<ArticlePublicDto> listArticles(Model model) { + public HashSet<ArticlePublicDto> listArticles() { HashSet<ArticlePublicDto> articles = new HashSet<ArticlePublicDto>(articleService.findAllArticles()); - //UserEntity user = userService.getLoggedInUser().orElse(new UserEntity()); - //model.addAttribute("user", user); - //model.addAttribute("articles", articles); - //return "index"; return articles; } @@ -68,20 +62,18 @@ public class ArticleController { return "articles/new"; }*/ - @PostMapping("/articles/new") - public String saveArticle(@Valid @ModelAttribute("article") ArticleDto articleDto, BindingResult result, Model model) { + @PostMapping("/article/new") + public String saveArticle(@Valid @ModelAttribute("article") ArticleDto articleDto, BindingResult result) { // if non-authenticated in user tries to create an article // redirect them to login page UserEntity user = userService.getLoggedInUser().orElse(null); if (user == null) { - return "redirect:/userlogin"; - } else if (result.hasErrors()) { - model.addAttribute("article", articleDto); - return "articles/new"; - } else { + return "redirect:/login"; + } else if (!result.hasErrors()) { articleService.saveArticle(articleDto); - return "redirect:/articles"; + return "redirect:/"; } + return ""; } @GetMapping("/articles/delete/{articleId}") diff --git a/backend/src/main/java/com/blog/web/controllers/AuthController.java b/backend/src/main/java/com/blog/web/controllers/AuthController.java index a870086..4da346b 100644 --- a/backend/src/main/java/com/blog/web/controllers/AuthController.java +++ b/backend/src/main/java/com/blog/web/controllers/AuthController.java @@ -1,15 +1,15 @@ package com.blog.web.controllers; import com.blog.web.dto.RegistrationDto; +import com.blog.web.dto.UserPublicDto; import com.blog.web.models.UserEntity; import com.blog.web.services.UserService; import jakarta.validation.Valid; import org.apache.commons.lang3.StringUtils; -import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; -@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true") +@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true", allowedHeaders = "*") @RestController @RequestMapping("/api/v1") public class AuthController { @@ -37,4 +37,10 @@ public class AuthController { } return user; } + + @GetMapping("/profile") + public UserPublicDto profile() { + final UserEntity user = userService.getLoggedInUser().orElse(null); + return new UserPublicDto(user); + } } |
