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