package project.fmo.app.projcetfmo.Web; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import project.fmo.app.projcetfmo.Model.Korisnik; import project.fmo.app.projcetfmo.Model.exception.InvalidUserCredentialsException; import project.fmo.app.projcetfmo.Service.AuthService; import javax.servlet.http.HttpServletRequest; @Controller @RequestMapping("/login") public class LoginController { private final AuthService authService; public LoginController(AuthService authService) { this.authService = authService; } @GetMapping public String getLoginPage(Model model) { model.addAttribute("bodyContent","login"); return "master-template"; } @PostMapping public String login(HttpServletRequest request, Model model) { Korisnik user = null; try{ user = this.authService.login(request.getParameter("username"), request.getParameter("password")); request.getSession().setAttribute("user", user); return "redirect:/home"; } catch (InvalidUserCredentialsException exception) { model.addAttribute("hasError", true); model.addAttribute("error", exception.getMessage()); return "login"; } } }