source: src/main/java/tech/techharbor/Web/SingInRegisterController.java@ f4b4afa

main
Last change on this file since f4b4afa was f4b4afa, checked in by Nikola Todoroski <nikola.todoroski@…>, 6 months ago

Pushed whole project, original project location on github:https://github.com/hehxd/Tech-Harbor

  • Property mode set to 100644
File size: 3.6 KB
Line 
1package tech.techharbor.Web;
2
3import jakarta.servlet.http.HttpSession;
4import org.springframework.security.crypto.password.PasswordEncoder;
5import org.springframework.stereotype.Controller;
6import org.springframework.web.bind.annotation.GetMapping;
7import org.springframework.web.bind.annotation.PostMapping;
8import org.springframework.web.bind.annotation.RequestParam;
9import tech.techharbor.Model.CustomerModel;
10import tech.techharbor.Model.DeliveryManModel;
11import tech.techharbor.Model.UserTableModel;
12import tech.techharbor.Repository.CustomerRepository;
13import tech.techharbor.Repository.DeliveryManRepository;
14import tech.techharbor.Service.UserService;
15
16import java.util.Optional;
17import java.util.regex.Pattern;
18
19@Controller
20public class SingInRegisterController {
21
22 private final UserService userService;
23 private final PasswordEncoder passwordEncoder;
24
25 private final DeliveryManRepository deliveryManRepository;
26
27 private final CustomerRepository customerRepository;
28
29 public SingInRegisterController(UserService userService, PasswordEncoder passwordEncoder, DeliveryManRepository deliveryManRepository, CustomerRepository customerRepository) {
30 this.userService = userService;
31 this.passwordEncoder = passwordEncoder;
32 this.deliveryManRepository = deliveryManRepository;
33 this.customerRepository = customerRepository;
34 }
35
36 @GetMapping("/register")
37 public String registerUser() {
38
39 return "register";
40 }
41
42 @PostMapping("/register")
43 public String registerUser(@RequestParam String name,
44 @RequestParam String username,
45 @RequestParam String email,
46 @RequestParam String phoneNumber,
47 @RequestParam String password,
48 @RequestParam String confirmPassword) {
49
50 String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
51
52 // Check if email is valid
53 if (!Pattern.matches(emailRegex, email))
54 throw new IllegalArgumentException("Invalid email address");
55
56
57 if (password.equals(confirmPassword)) {
58 userService.create(name, username, email, passwordEncoder.encode(password), phoneNumber);
59 }
60 return "redirect:/login";
61 }
62
63 @GetMapping("/login")
64 public String loginUser() {
65
66 return "signIn";
67 }
68
69 @PostMapping("/login")
70 public String loginUser(@RequestParam String username,
71 @RequestParam String password,
72 HttpSession session) {
73
74 UserTableModel user = this.userService.findByUsername(username);
75 if (user == null)
76 throw new IllegalArgumentException("Invalid username");
77
78 Optional<DeliveryManModel> deliveryMan = deliveryManRepository.findById(user.getUserId());
79
80 if (passwordEncoder.matches(password, user.getPassword())) {
81 if (deliveryMan.isEmpty()) {
82 CustomerModel customer = new CustomerModel(user.getUserId());
83 customerRepository.save(customer);
84 session.setAttribute("user", user);
85 } else {
86 DeliveryManModel deliveryManUser = new DeliveryManModel(deliveryMan.get().getUserId());
87 deliveryManRepository.save(deliveryManUser);
88 session.setAttribute("deliveryMan", user);
89 }
90 return "redirect:/";
91 }
92 return "redirect:/login";
93 }
94
95 @GetMapping("/logout")
96 public String logoutUser(HttpSession session) {
97 session.invalidate();
98 return "redirect:/";
99 }
100
101
102}
Note: See TracBrowser for help on using the repository browser.