1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- 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<String, Object> 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";
- }
- }
|