1 | package tech.techharbor.Web;
|
---|
2 |
|
---|
3 | import jakarta.servlet.http.HttpSession;
|
---|
4 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
5 | import org.springframework.stereotype.Controller;
|
---|
6 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
7 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
8 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
9 | import tech.techharbor.Model.CustomerModel;
|
---|
10 | import tech.techharbor.Model.DeliveryManModel;
|
---|
11 | import tech.techharbor.Model.UserTableModel;
|
---|
12 | import tech.techharbor.Repository.CustomerRepository;
|
---|
13 | import tech.techharbor.Repository.DeliveryManRepository;
|
---|
14 | import tech.techharbor.Service.UserService;
|
---|
15 |
|
---|
16 | import java.util.Optional;
|
---|
17 | import java.util.regex.Pattern;
|
---|
18 |
|
---|
19 | @Controller
|
---|
20 | public 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 | }
|
---|