2025-11-24 10:26:19 +07:00
|
|
|
package ru.soune.no_copy.controller;
|
|
|
|
|
|
2025-11-24 12:29:32 +07:00
|
|
|
import jakarta.validation.Valid;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
2025-11-24 10:26:19 +07:00
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
2025-11-24 12:29:32 +07:00
|
|
|
import ru.soune.no_copy.dto.AuthResponse;
|
2025-11-24 23:36:26 +07:00
|
|
|
import ru.soune.no_copy.dto.LoginRequest;
|
|
|
|
|
import ru.soune.no_copy.dto.LoginResponse;
|
2025-11-24 12:29:32 +07:00
|
|
|
import ru.soune.no_copy.dto.RegisterRequest;
|
|
|
|
|
import ru.soune.no_copy.entity.AuthToken;
|
|
|
|
|
import ru.soune.no_copy.service.AuthService;
|
2025-11-24 10:26:19 +07:00
|
|
|
|
|
|
|
|
@RestController
|
2025-11-24 21:04:50 +07:00
|
|
|
@RequestMapping("/api/auth")
|
2025-11-24 12:29:32 +07:00
|
|
|
@RequiredArgsConstructor
|
2025-11-24 10:26:19 +07:00
|
|
|
public class AuthController {
|
2025-11-24 12:29:32 +07:00
|
|
|
|
|
|
|
|
private final AuthService authService;
|
|
|
|
|
|
|
|
|
|
@PostMapping("/register")
|
|
|
|
|
public ResponseEntity<AuthResponse> register(@Valid @RequestBody RegisterRequest registerRequest) {
|
|
|
|
|
AuthToken authToken = authService.register(registerRequest);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(new AuthResponse(true, "success.user.register", authToken.getToken(),
|
|
|
|
|
authToken.getExpiresAt()));
|
|
|
|
|
}
|
2025-11-24 23:36:26 +07:00
|
|
|
|
|
|
|
|
@PostMapping("/login")
|
2025-11-25 11:59:37 +07:00
|
|
|
public ResponseEntity<LoginResponse> login(@Valid @RequestBody LoginRequest request) {
|
|
|
|
|
AuthToken login = authService.login(request);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(new LoginResponse(true, login.getUser().getEmail(),
|
|
|
|
|
login.getToken(),login.getExpiresAt().toString()));
|
2025-11-24 23:36:26 +07:00
|
|
|
}
|
2025-11-24 10:26:19 +07:00
|
|
|
}
|