Files
no-copy/src/main/java/ru/soune/no_copy/controller/AuthController.java
T

38 lines
1.1 KiB
Java
Raw Normal View History

2025-11-24 10:26:19 +07:00
package ru.soune.no_copy.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
2025-11-25 14:20:28 +07:00
import org.springframework.web.bind.annotation.*;
import ru.soune.no_copy.entity.AuthToken;
2025-11-27 18:01:02 +07:00
import ru.soune.no_copy.exception.TokenNotFoundException;
import ru.soune.no_copy.repository.AuthTokenRepository;
import ru.soune.no_copy.service.AuthService;
2025-11-24 10:26:19 +07:00
2025-11-25 14:20:28 +07:00
import java.util.Map;
2025-11-27 18:01:02 +07:00
import java.util.Optional;
2025-11-25 14:20:28 +07:00
2025-11-24 10:26:19 +07:00
@RestController
2025-12-05 11:45:51 +07:00
@RequestMapping("v1/api/auth")
@RequiredArgsConstructor
2025-11-24 10:26:19 +07:00
public class AuthController {
private final AuthService authService;
2025-11-27 18:01:02 +07:00
private final AuthTokenRepository authTokenRepository;
2025-11-25 14:20:28 +07:00
@PostMapping("/logout")
public ResponseEntity<?> logout(@RequestHeader("Authorization") String tokenHeader) {
String token = tokenHeader.replace("Bearer ", "");
2025-11-27 18:01:02 +07:00
Optional<AuthToken> opToken = authTokenRepository.findByToken(token);
if (opToken.isEmpty()) {
throw new TokenNotFoundException("Token not found");
}
2025-11-25 14:20:28 +07:00
authService.logout(token);
return ResponseEntity.ok(Map.of("success", true));
}
2025-11-24 10:26:19 +07:00
}