source: src/main/java/com/example/baziproekt/web/LoginController.java@ 0e4d807

Last change on this file since 0e4d807 was 0e4d807, checked in by Ivona <ivonatapshanovska@…>, 10 months ago

Initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
1package com.example.baziproekt.web;
2
3import com.example.baziproekt.model.Korisnici;
4import com.example.baziproekt.model.exceptions.InvalidUserCredentialsException;
5import com.example.baziproekt.service.KorisniciService;
6
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;
12import org.springframework.web.bind.annotation.RequestParam;
13
14import javax.servlet.http.HttpServletRequest;
15
16
17@Controller
18@RequestMapping("")
19public class LoginController {
20
21
22
23 private final KorisniciService service;
24
25 public LoginController(KorisniciService service) {
26 this.service = service;
27 }
28
29 @GetMapping("/login")
30 public String getLoginPage( @RequestParam(required = false) String error, Model model)
31 {
32 if(error!=null&&!error.isEmpty())
33 {model.addAttribute("hasError",true);
34 model.addAttribute("error",error);}
35 return "login";
36 }
37
38 @PostMapping("/login")
39 public String login(HttpServletRequest request, Model model)
40 {
41
42 try
43 {
44 Korisnici korisnik=this.service.login(request.getParameter("korisnicko_ime"), request.getParameter("lozinka"));
45 request.getSession().setAttribute("korisnik",korisnik);
46 return "redirect:/home";
47
48 }
49 catch (InvalidUserCredentialsException e)
50 {
51 return "redirect:/login?hasError=true&error=" + e.getMessage();
52 }
53 }
54}
Note: See TracBrowser for help on using the repository browser.