summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2024-07-25 10:57:06 -0400
committerrealtradam <[email protected]>2024-07-25 10:57:06 -0400
commitc01264b60b7ad8bb3eb3dcf4d3ec0b77bcd4c3d1 (patch)
tree4b2cc7a77927b51cef81309e92ab9584c1b9b96e
parent054e25bb66269d1d99ee0b0afa3b26abee2db80f (diff)
downloadspring-blog-c01264b60b7ad8bb3eb3dcf4d3ec0b77bcd4c3d1.tar.gz
spring-blog-c01264b60b7ad8bb3eb3dcf4d3ec0b77bcd4c3d1.zip
start work on registration
-rw-r--r--backend/src/main/java/com/blog/web/controllers/ArticleController.java6
-rw-r--r--backend/src/main/java/com/blog/web/controllers/AuthController.java24
-rw-r--r--frontend/src/pages/Register.tsx33
3 files changed, 48 insertions, 15 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 6bd1abe..7ffa2fe 100644
--- a/backend/src/main/java/com/blog/web/controllers/ArticleController.java
+++ b/backend/src/main/java/com/blog/web/controllers/ArticleController.java
@@ -16,9 +16,9 @@ import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
-@RequestMapping("/api/v1")
+@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
@RestController
-@Controller
+@RequestMapping("/api/v1")
public class ArticleController {
private ArticleService articleService;
private UserService userService;
@@ -41,7 +41,6 @@ public class ArticleController {
);
}
- @CrossOrigin
@GetMapping("/articles")
public HashSet<ArticlePublicDto> listArticles(Model model) {
HashSet<ArticlePublicDto> articles = new HashSet<ArticlePublicDto>(articleService.findAllArticles());
@@ -52,7 +51,6 @@ public class ArticleController {
return articles;
}
- @CrossOrigin
@GetMapping("/article/{articleId}")
public ArticlePublicDto showArticle(@PathVariable("articleId") long articleId, Model model) {
ArticlePublicDto articlePublicDto = articleService.findArticlePublicById(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 efb3672..1f854c3 100644
--- a/backend/src/main/java/com/blog/web/controllers/AuthController.java
+++ b/backend/src/main/java/com/blog/web/controllers/AuthController.java
@@ -5,14 +5,13 @@ 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.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;
+import org.springframework.web.bind.annotation.*;
-@Controller
+@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
+@RestController
+@RequestMapping("/api/v1")
public class AuthController {
private final UserService userService;
@@ -27,15 +26,18 @@ public class AuthController {
return "auth/login";
}
+ /*
@GetMapping("/register")
public String getRegisterForm(Model model) {
final 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) {
+ //@PostMapping("/register/save")
+ @PostMapping("/register")
+ public RegistrationDto register(@Valid @ModelAttribute("user") RegistrationDto user, BindingResult result) {
UserEntity existingUserEmail = userService.findByEmail(user.getEmail()).orElse(null);
if (existingUserEmail != null && StringUtils.isBlank(existingUserEmail.getEmail())) {
result.rejectValue("email", "There is already a user with this email");
@@ -47,10 +49,12 @@ public class AuthController {
}
if (result.hasErrors()) {
- model.addAttribute("user", user);
- return "register";
+ //model.addAttribute("user", user);
+ //return "register";
+ return user;
}
userService.saveUser(user);
- return "redirect:/articles?success";
+ //return "redirect:/articles?success";
+ return user;
}
}
diff --git a/frontend/src/pages/Register.tsx b/frontend/src/pages/Register.tsx
index 7030842..786d252 100644
--- a/frontend/src/pages/Register.tsx
+++ b/frontend/src/pages/Register.tsx
@@ -1,10 +1,41 @@
+import { FormEvent } from "react";
+import { useNavigate } from 'react-router-dom';
export default function Register () {
+ const navigate = useNavigate();
+
+const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
+ e.preventDefault(); //stops submit from happening
+
+ const target = e.target as typeof e.target & {
+ username: { value: string };
+ email: { value: string };
+ password: { value: string };
+ };
+
+ const formData = new FormData();
+ formData.append('user[username]', target.username.value);
+ formData.append('user[email]', target.email.value);
+ formData.append('user[password]', target.password.value);
+
+ const response = await fetch(`${import.meta.env.VITE_API_TITLE}/api/v1/register`, {
+ credentials: 'include',
+ method: 'post',
+ body: formData,
+ });
+ if(response.ok) {
+ navigate("/login");
+ }
+ else {
+ alert("error");
+ }
+ };
+
return(
<>
<div className="flex flex-col items-center justify-center bg-white p-12">
<div className="text-xl w-full text-center mb-8 p-4 bg-black text-red-500">Username or Email already exists</div>
- <form role="form" method="post" className="w-full max-w-lg">
+ <form onSubmit={handleSubmit} method="post" className="w-full max-w-lg">
<div className="flex flex-wrap -mx-3 mb-6">
<div className="w-full md:w-1/2 px-3 mb-6 md:mb-0">
<label className="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"