0

I have this controller

    @Controller
    @RequestMapping("/")
    public class UserController {

        @Autowired
        private UserServices userServices;

        public String em;

        @GetMapping("/settings")
        public String showProfilePage(Model model, @ModelAttribute User user){
            em=user.getEmail();
            model.addAttribute("user",user);
            return "editprofile";
        }

        @PostMapping("/settings")
        public String updateProfile(Model model,@ModelAttribute User user,BindingResult bindingResult,Principal principal){
            if(bindingResult.hasErrors()){
                return "/editprofile";
            }
            if(userServices.isUserPresentUpdate(user.getEmail(),em)){
                model.addAttribute("exist",true);
                return "/editprofile";
            }
            userServices.update(user);
            return "redirect:/settings";
        }

        @GetMapping("/settings/password")
        public String passwordEdit(Model model, @ModelAttribute User user){
            model.addAttribute("user",user);
            return "editprofilepassword";
        }

        @PostMapping("/settings/password")
        public String passwordUpdate(@ModelAttribute User user, BindingResult bindingResult){
            if(bindingResult.hasErrors()){
                return "editprofilepassword";
            }
            userServices.updateHeslo(user);
            return "redirect:/settings";
        }

        @ModelAttribute("user")
        public User getUser() {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            UserDetails userDetail = (UserDetails) auth.getPrincipal();
            User u = userServices.findByEmail(userDetail.getUsername());
            return  u.getId() !=null ? userServices.findOne(u.getId()) : new User();
        }
    }

I'm looking for a method that changes principal name or any other solutions. Every time I change the email in /settings, the email gets changed, but the principal name is still the same and it breaks.

darkmnemic
  • 272
  • 3
  • 13
joachim
  • 47
  • 8

0 Answers0