-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from szymonpoltorak/logout
Logout
- Loading branch information
Showing
33 changed files
with
283 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
social-app-backend/src/main/java/razepl/dev/socialappbackend/auth/LogoutService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package razepl.dev.socialappbackend.auth; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.web.authentication.logout.LogoutHandler; | ||
import org.springframework.stereotype.Service; | ||
import razepl.dev.socialappbackend.auth.jwt.JwtToken; | ||
import razepl.dev.socialappbackend.auth.jwt.interfaces.TokenRepository; | ||
|
||
import static razepl.dev.socialappbackend.config.constants.Headers.*; | ||
|
||
/** | ||
* Service class for logging user out. | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class LogoutService implements LogoutHandler { | ||
private final TokenRepository tokenRepository; | ||
|
||
@Override | ||
public final void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) { | ||
String authHeader = request.getHeader(AUTH_HEADER); | ||
|
||
if (authHeader == null || !authHeader.startsWith(TOKEN_HEADER)) { | ||
return; | ||
} | ||
String jwt = authHeader.substring(TOKEN_START_INDEX); | ||
JwtToken token = tokenRepository.findByToken(jwt).orElse(null); | ||
|
||
if (token == null) { | ||
return; | ||
} | ||
token.setExpired(true); | ||
token.setRevoked(true); | ||
tokenRepository.save(token); | ||
|
||
SecurityContextHolder.clearContext(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...al-app-backend/src/main/java/razepl/dev/socialappbackend/config/constants/CorsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package razepl.dev.socialappbackend.config.constants; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Configuration for Cross-Origin Resource Sharing (CORS) settings. | ||
*/ | ||
public final class CorsConfig { | ||
|
||
/** | ||
* List of allowed HTTP request methods. | ||
*/ | ||
public static final List<String> ALLOWED_REQUESTS = List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"); | ||
|
||
/** | ||
* List of allowed frontend server addresses. | ||
*/ | ||
public static final List<String> FRONTEND_ADDRESS = List.of("http://localhost:4200"); | ||
|
||
/** | ||
* HTTP header for specifying the content type of request or response. | ||
*/ | ||
public static final String CONTENT_TYPE_HEADER = "Content-Type"; | ||
|
||
/** | ||
* API pattern for CORS configuration. | ||
*/ | ||
public static final String API_PATTERN = "/api/**"; | ||
|
||
/** | ||
* Private constructor to prevent instantiation of this class. | ||
*/ | ||
private CorsConfig() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...p-backend/src/main/java/razepl/dev/socialappbackend/exceptions/NullArgumentException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
.../src/app/core/interfaces/AuthInterface.ts → ...app/core/interfaces/auth/AuthInterface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.