source: src/main/java/mk/ukim/finki/busngo/web/LoginController.java@ b101b69

Last change on this file since b101b69 was b101b69, checked in by ppaunovski <paunovskipavel@…>, 6 months ago

initial classes, no inheritance yet v2

  • Property mode set to 100644
File size: 1.5 KB
Line 
1package mk.ukim.finki.busngo.web;
2
3import jakarta.servlet.http.HttpServletRequest;
4import mk.ukim.finki.busngo.model.entities.Korisnik;
5import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
6import mk.ukim.finki.busngo.service.AuthService;
7import org.springframework.stereotype.Controller;
8import org.springframework.ui.Model;
9import org.springframework.web.bind.annotation.GetMapping;
10import org.springframework.web.bind.annotation.PostMapping;
11import org.springframework.web.bind.annotation.RequestMapping;
12
13@Controller
14@RequestMapping("/login")
15public 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
Note: See TracBrowser for help on using the repository browser.