57 lines
1.8 KiB
Java
57 lines
1.8 KiB
Java
package com.krylov.refound.controller;
|
|
|
|
import com.krylov.refound.dto.AuthRequest;
|
|
import com.krylov.refound.dto.AuthResponse;
|
|
import com.krylov.refound.dto.RegisterRequest;
|
|
import com.krylov.refound.entity.User;
|
|
import com.krylov.refound.repository.UserRepository;
|
|
import java.util.Map;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/v1/auth")
|
|
@Slf4j
|
|
@RequiredArgsConstructor
|
|
public class AuthController {
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
@PostMapping("/register")
|
|
public ResponseEntity<?> register(@RequestBody RegisterRequest request) {
|
|
|
|
// Проверка что логин не занят
|
|
if (userRepository.findByLogin(request.getLogin()).isPresent()) {
|
|
return ResponseEntity
|
|
.badRequest()
|
|
.body(Map.of("message", "Логин уже занят"));
|
|
}
|
|
|
|
// Создание пользователя
|
|
User user = new User();
|
|
user.setName(request.getName());
|
|
user.setLogin(request.getLogin());
|
|
user.setPassword(request.getPassword()); // позже добавить BCrypt
|
|
|
|
userRepository.save(user);
|
|
|
|
String token = "fake-jwt-token";
|
|
|
|
return ResponseEntity.ok(new AuthResponse(token, user));
|
|
}
|
|
|
|
@GetMapping("/check-login")
|
|
public ResponseEntity<?> checkLogin(@RequestParam String login) {
|
|
boolean exists = userRepository.existsByLogin(login);
|
|
return ResponseEntity.ok(Map.of("exists", exists));
|
|
}
|
|
|
|
}
|