1 | package mk.ukim.finki.busngo.web;
|
---|
2 |
|
---|
3 | import jakarta.servlet.http.HttpServletRequest;
|
---|
4 | import mk.ukim.finki.busngo.model.entities.Korisnik;
|
---|
5 | import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
|
---|
6 | import mk.ukim.finki.busngo.service.AuthService;
|
---|
7 | import org.springframework.stereotype.Controller;
|
---|
8 | import org.springframework.ui.Model;
|
---|
9 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
10 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
11 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
12 |
|
---|
13 | @Controller
|
---|
14 | @RequestMapping("/login")
|
---|
15 | public class LoginController {
|
---|
16 |
|
---|
17 | private final AuthService authService;
|
---|
18 |
|
---|
19 | public LoginController(AuthService authService) {
|
---|
20 | this.authService = authService;
|
---|
21 | }
|
---|
22 |
|
---|
23 | @GetMapping
|
---|
24 | public String getLoginPage(Model model) {
|
---|
25 | model.addAttribute("bodyContent", "login");
|
---|
26 | return "master-template";
|
---|
27 | }
|
---|
28 |
|
---|
29 | @PostMapping
|
---|
30 | public String login(HttpServletRequest request, Model model) {
|
---|
31 | Korisnik user = null;
|
---|
32 |
|
---|
33 | try {
|
---|
34 | user = authService.login(request.getParameter("username"), request.getParameter("password"));
|
---|
35 | } catch (InvalidCredentialsException exception) {
|
---|
36 | model.addAttribute("bodyContent", "login");
|
---|
37 | model.addAttribute("hasError", true);
|
---|
38 | model.addAttribute("error", exception.getMessage());
|
---|
39 | return "master-template";
|
---|
40 | }
|
---|
41 |
|
---|
42 | request.getSession().setAttribute("user", user);
|
---|
43 | return "redirect:/home";
|
---|
44 | }
|
---|
45 |
|
---|
46 | }
|
---|
47 |
|
---|