38 lines
1.1 KiB
Java
38 lines
1.1 KiB
Java
package ru.soune.nocopy.controller;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import ru.soune.nocopy.entity.AuthToken;
|
|
import ru.soune.nocopy.exception.TokenNotFoundException;
|
|
import ru.soune.nocopy.repository.AuthTokenRepository;
|
|
import ru.soune.nocopy.service.AuthService;
|
|
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
|
|
@RestController
|
|
@RequestMapping("v1/api/auth")
|
|
@RequiredArgsConstructor
|
|
public class AuthController {
|
|
|
|
private final AuthService authService;
|
|
|
|
private final AuthTokenRepository authTokenRepository;
|
|
|
|
@PostMapping("/logout")
|
|
public ResponseEntity<?> logout(@RequestHeader("Authorization") String tokenHeader) {
|
|
String token = tokenHeader.replace("Bearer ", "");
|
|
|
|
Optional<AuthToken> opToken = authTokenRepository.findByToken(token);
|
|
|
|
if (opToken.isEmpty()) {
|
|
throw new TokenNotFoundException("Token not found");
|
|
}
|
|
|
|
authService.logout(token);
|
|
|
|
return ResponseEntity.ok(Map.of("success", true));
|
|
}
|
|
}
|