1 | package com.example.baziproekt.web;
|
---|
2 |
|
---|
3 | import com.example.baziproekt.model.Korisnici;
|
---|
4 | import com.example.baziproekt.model.exceptions.InvalidUserCredentialsException;
|
---|
5 | import com.example.baziproekt.service.KorisniciService;
|
---|
6 |
|
---|
7 | import org.springframework.stereotype.Controller;
|
---|
8 | import org.springframework.ui.Model;
|
---|
9 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
10 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
11 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
12 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
13 |
|
---|
14 | import javax.servlet.http.HttpServletRequest;
|
---|
15 |
|
---|
16 |
|
---|
17 | @Controller
|
---|
18 | @RequestMapping("")
|
---|
19 | public 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 | }
|
---|