del front and add auth
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,7 +24,7 @@ public class ChatController {
|
||||
@MessageMapping("/chat")
|
||||
public void send(@Payload ChatMessage message, Principal principal) {
|
||||
|
||||
Optional<User> sender = userRepository.findByEmail(principal.getName());
|
||||
Optional<User> sender = userRepository.findByLogin(principal.getName());
|
||||
Message saved = new Message();
|
||||
if(sender.isPresent()){
|
||||
saved = service.send(message.getReceiverId(), message.getContent());
|
||||
|
||||
Reference in New Issue
Block a user