Compare commits
2
Commits
NCBACK-36_1
...
NCP-2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5fd73e7d79 | ||
|
|
adec554122 |
@@ -3,10 +3,7 @@ package ru.soune.no_copy.controller;
|
|||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import ru.soune.no_copy.dto.AuthResponse;
|
import ru.soune.no_copy.dto.AuthResponse;
|
||||||
import ru.soune.no_copy.dto.LoginRequest;
|
import ru.soune.no_copy.dto.LoginRequest;
|
||||||
import ru.soune.no_copy.dto.LoginResponse;
|
import ru.soune.no_copy.dto.LoginResponse;
|
||||||
@@ -14,6 +11,8 @@ import ru.soune.no_copy.dto.RegisterRequest;
|
|||||||
import ru.soune.no_copy.entity.AuthToken;
|
import ru.soune.no_copy.entity.AuthToken;
|
||||||
import ru.soune.no_copy.service.AuthService;
|
import ru.soune.no_copy.service.AuthService;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/auth")
|
@RequestMapping("/api/auth")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@@ -36,4 +35,13 @@ public class AuthController {
|
|||||||
return ResponseEntity.ok(new LoginResponse(true, login.getUser().getEmail(),
|
return ResponseEntity.ok(new LoginResponse(true, login.getUser().getEmail(),
|
||||||
login.getToken(),login.getExpiresAt().toString()));
|
login.getToken(),login.getExpiresAt().toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/logout")
|
||||||
|
public ResponseEntity<?> logout(@RequestHeader("Authorization") String tokenHeader) {
|
||||||
|
String token = tokenHeader.replace("Bearer ", "");
|
||||||
|
|
||||||
|
authService.logout(token);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(Map.of("success", true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,4 +10,5 @@ import java.util.Optional;
|
|||||||
@Repository
|
@Repository
|
||||||
public interface AuthTokenRepository extends JpaRepository<AuthToken, Long> {
|
public interface AuthTokenRepository extends JpaRepository<AuthToken, Long> {
|
||||||
Optional<AuthToken> findByTokenAndExpiresAtAfter(String token, LocalDate expiresAtAfter);
|
Optional<AuthToken> findByTokenAndExpiresAtAfter(String token, LocalDate expiresAtAfter);
|
||||||
|
Optional<AuthToken> findByToken(String token);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,6 +78,10 @@ public class AuthService {
|
|||||||
return authTokenRepository.save(authToken);
|
return authTokenRepository.save(authToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void logout(String token) {
|
||||||
|
authTokenRepository.findByToken(token)
|
||||||
|
.ifPresent(authTokenRepository::delete);
|
||||||
|
}
|
||||||
|
|
||||||
private String generateAuthToken() {
|
private String generateAuthToken() {
|
||||||
byte[] bytes = new byte[32];
|
byte[] bytes = new byte[32];
|
||||||
|
|||||||
Reference in New Issue
Block a user