Skip to content

Commit

Permalink
Fix #3: Fixing Refreshing Token feature
Browse files Browse the repository at this point in the history
- Deleting existing token and refreshing all token
- Refreshing token error fix

Fixes: #9
  • Loading branch information
404-not-foundl committed Mar 27, 2024
1 parent 43e87f4 commit 3ace9dd
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/main/java/S10P22D204/authentication/common/jwt/JwtManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,27 @@ public Mono<Void> createRefreshToken(String internalId, ServerWebExchange exchan
public Mono<String> checkAccessToken(ServerWebExchange exchange) {
return Mono.justOrEmpty(exchange.getRequest().getCookies().getFirst("ACCESS_TOKEN"))
.flatMap(cookie -> tokenRepository.getToken(cookie.getValue())
.switchIfEmpty(checkRefreshToken(exchange).mapNotNull(internalId -> "null")))
.switchIfEmpty(Mono.defer(() -> checkRefreshToken(exchange)))
)
.defaultIfEmpty("null");
}

public Mono<String> checkRefreshToken(ServerWebExchange exchange) {
return Mono.justOrEmpty(exchange.getRequest().getCookies().getFirst("REFRESH_TOKEN"))
.flatMap(cookie -> tokenRepository.getToken(cookie.getValue())
.flatMap(internalId -> regenerateToken(internalId, exchange).thenReturn(internalId)))
.flatMap(internalId ->
tokenRepository.deleteToken(cookie.getValue())
.then(regenerateToken(internalId, exchange))
.thenReturn(internalId))
)
.defaultIfEmpty("null");
}


public Mono<Void> regenerateToken(String internalId, ServerWebExchange exchange) {
return createAccessToken(internalId, exchange)
return Mono.justOrEmpty(exchange.getRequest().getCookies().getFirst("REFRESH_TOKEN"))
.flatMap(cookie -> tokenRepository.deleteToken(cookie.getValue()))
.then(createAccessToken(internalId, exchange))
.then(createRefreshToken(internalId, exchange));
}

Expand Down

0 comments on commit 3ace9dd

Please sign in to comment.