1 | package finki.paw5.web.controllers;
|
---|
2 |
|
---|
3 | import finki.paw5.model.entities.User;
|
---|
4 | import finki.paw5.model.exceptions.NonExistingArgumentsException;
|
---|
5 | import org.springframework.stereotype.Controller;
|
---|
6 | import org.springframework.ui.Model;
|
---|
7 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
8 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
9 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
10 |
|
---|
11 | import finki.paw5.service.AuthService;
|
---|
12 | import jakarta.servlet.http.HttpServletRequest;
|
---|
13 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
14 |
|
---|
15 | import java.util.Objects;
|
---|
16 |
|
---|
17 | @Controller
|
---|
18 | @RequestMapping("/login")
|
---|
19 | public class LoginController {
|
---|
20 |
|
---|
21 | private final AuthService authService;
|
---|
22 |
|
---|
23 | public LoginController(AuthService authService) {
|
---|
24 | this.authService = authService;
|
---|
25 | }
|
---|
26 |
|
---|
27 | @GetMapping
|
---|
28 | public String register(){
|
---|
29 | return "/login";
|
---|
30 | }
|
---|
31 | @PostMapping
|
---|
32 | public String register(@RequestParam String email, @RequestParam String password, HttpServletRequest request, Model model) throws NonExistingArgumentsException {
|
---|
33 |
|
---|
34 | if(email==null || email.equals("") || Objects.equals(password, "") || password==null){
|
---|
35 | throw new NonExistingArgumentsException();
|
---|
36 | }
|
---|
37 | User user = authService.login(email,password);
|
---|
38 |
|
---|
39 | if(user!=null){
|
---|
40 | request.getSession().setAttribute("user",user); //go smestuvam korisnikot vo sesija
|
---|
41 | return "redirect:/home";
|
---|
42 | }
|
---|
43 | else{
|
---|
44 | return "redirect:/login";
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | }
|
---|
49 | } |
---|