source: src/main/java/project/fmo/app/projcetfmo/Web/LoginController.java

main
Last change on this file was d14176d, checked in by HristijanMitic00 <hristijan.mitic.01@…>, 12 months ago

First commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1package project.fmo.app.projcetfmo.Web;
2
3
4import org.springframework.stereotype.Controller;
5import org.springframework.ui.Model;
6import org.springframework.web.bind.annotation.GetMapping;
7import org.springframework.web.bind.annotation.PostMapping;
8import org.springframework.web.bind.annotation.RequestMapping;
9import project.fmo.app.projcetfmo.Model.Korisnik;
10import project.fmo.app.projcetfmo.Model.exception.InvalidUserCredentialsException;
11import project.fmo.app.projcetfmo.Service.AuthService;
12
13import javax.servlet.http.HttpServletRequest;
14
15@Controller
16@RequestMapping("/login")
17public 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}
Note: See TracBrowser for help on using the repository browser.