package org.example.sweater.controller; import jakarta.servlet.http.HttpServletRequest; import org.example.sweater.domain.Role; import org.example.sweater.domain.User; import org.example.sweater.repos.UsersRepository; import org.springframework.security.web.csrf.CsrfToken; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import java.util.Collections; import java.util.Map; @Controller public class AuthorizationController { public AuthorizationController(UsersRepository usersRepository) { this.usersRepository = usersRepository; } private final UsersRepository usersRepository; @GetMapping("/login") public String login() { return "login"; } @GetMapping("/registration") public String registration() { return "registration"; } @PostMapping("/registration") public String addUser(User user, Map model) { User userFromStorage = usersRepository.findByUsername(user.getUsername()); if (userFromStorage != null) { model.put("message", "User exists!"); return "registration"; } user.setActive(true); user.setRoles(Collections.singleton(Role.USER)); usersRepository.save(user); return "redirect:/login"; } }