diff --git a/src/main/java/com/example/daemawiki/domain/auth/api/AuthController.java b/src/main/java/com/example/daemawiki/domain/auth/api/AuthController.java index b517065a..416dedc9 100644 --- a/src/main/java/com/example/daemawiki/domain/auth/api/AuthController.java +++ b/src/main/java/com/example/daemawiki/domain/auth/api/AuthController.java @@ -5,8 +5,8 @@ import com.example.daemawiki.domain.auth.dto.response.TokenResponse; import com.example.daemawiki.domain.auth.dto.request.SignupRequest; import com.example.daemawiki.domain.auth.service.Login; +import com.example.daemawiki.domain.auth.service.Reissue; import com.example.daemawiki.domain.auth.service.Signup; -import com.example.daemawiki.global.security.Tokenizer; import jakarta.validation.Valid; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; @@ -17,12 +17,12 @@ public class AuthController { private final Login loginService; private final Signup signupService; - private final Tokenizer tokenizer; + private final Reissue reissue; - public AuthController(Login loginService, Signup signupService, Tokenizer tokenizer) { + public AuthController(Login loginService, Signup signupService, Reissue reissue) { this.loginService = loginService; this.signupService = signupService; - this.tokenizer = tokenizer; + this.reissue = reissue; } @PostMapping("/login") @@ -38,7 +38,7 @@ public Mono signup(@Valid @RequestBody SignupRequest request) { @PostMapping("/reissue") public Mono reissue(@Valid @RequestBody ReissueRequest request) { - return tokenizer.reissue(request); + return reissue.execute(request); } } diff --git a/src/main/java/com/example/daemawiki/domain/auth/service/Reissue.java b/src/main/java/com/example/daemawiki/domain/auth/service/Reissue.java new file mode 100644 index 00000000..d2b9064b --- /dev/null +++ b/src/main/java/com/example/daemawiki/domain/auth/service/Reissue.java @@ -0,0 +1,24 @@ +package com.example.daemawiki.domain.auth.service; + +import com.example.daemawiki.domain.auth.dto.request.ReissueRequest; +import com.example.daemawiki.domain.auth.dto.response.TokenResponse; +import com.example.daemawiki.global.security.Tokenizer; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Mono; + +@Service +public class Reissue { + private final Tokenizer tokenizer; + + public Reissue(Tokenizer tokenizer) { + this.tokenizer = tokenizer; + } + + public Mono execute(ReissueRequest request) { + return tokenizer.reissue(request.token()) + .map(token -> TokenResponse.builder() + .token(token) + .build()); + } + +} diff --git a/src/main/java/com/example/daemawiki/global/security/Tokenizer.java b/src/main/java/com/example/daemawiki/global/security/Tokenizer.java index 6fa86121..370a310b 100644 --- a/src/main/java/com/example/daemawiki/global/security/Tokenizer.java +++ b/src/main/java/com/example/daemawiki/global/security/Tokenizer.java @@ -72,22 +72,18 @@ public Authentication getAuthentication(String token) { details, null, details.getAuthorities()); } - public Mono reissue(ReissueRequest request) { + public Mono reissue(String token) { try { - Claims claims = parseClaims(request.token()); + Claims claims = parseClaims(token); String user = claims.getSubject(); String newToken = tokenize(user); - return Mono.just(TokenResponse.builder() - .token(newToken) - .build()); + return Mono.justOrEmpty(newToken); } catch (ExpiredJwtException e) { String user = e.getClaims().getSubject(); String newToken = tokenize(user); - return Mono.just(TokenResponse.builder() - .token(newToken) - .build()); + return Mono.justOrEmpty(newToken); } catch (JwtException e) { throw InvalidTokenException.EXCEPTION; }