Skip to content

Commit

Permalink
Merge pull request #27 from coffee-meet/feat/#26
Browse files Browse the repository at this point in the history
Feat/#26 인증 토큰 갱신 기능 구현
  • Loading branch information
yumyeonghan authored Oct 23, 2023
2 parents d3be467 + 5ad2aa2 commit 42cb6ff
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 45 deletions.
24 changes: 12 additions & 12 deletions src/main/java/coffeemeet/server/auth/controller/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import coffeemeet.server.auth.dto.SignupRequest;
import coffeemeet.server.auth.service.AuthService;
import coffeemeet.server.auth.utils.AuthTokens;
import coffeemeet.server.common.annotation.Login;
import coffeemeet.server.user.domain.OAuthProvider;
import coffeemeet.server.user.dto.AuthInfo;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -34,21 +36,19 @@ public ResponseEntity<Void> redirectAuthCodeRequestUrl(@PathVariable OAuthProvid
}

@PostMapping("/sign-up")
public ResponseEntity<AuthTokens> signup(@RequestBody SignupRequest request) {
public ResponseEntity<AuthTokens> signup(@Valid @RequestBody SignupRequest request) {
return ResponseEntity.ok(authService.signup(request));
}

@GetMapping("/login/{oAuthProvider}")
public ResponseEntity<?> login(@PathVariable OAuthProvider oAuthProvider,
@RequestParam String authCode, HttpServletResponse response) throws IOException {
Optional<AuthTokens> authTokens = authService.login(oAuthProvider, authCode);
if (authTokens.isPresent()) {
return ResponseEntity.ok(authTokens.get());
}

String signupUrl = "/oauth2/auth/sign-up";
response.sendRedirect(signupUrl);
return new ResponseEntity<>(HttpStatus.FOUND);
public ResponseEntity<AuthTokens> login(@PathVariable OAuthProvider oAuthProvider,
@RequestParam String authCode) {
return ResponseEntity.ok(authService.login(oAuthProvider, authCode));
}

@PostMapping("/renew-token")
public ResponseEntity<AuthTokens> renew(@Login AuthInfo authInfo) {
return ResponseEntity.ok(authService.renew(authInfo.userId(), authInfo.refreshToken()));
}

}
19 changes: 5 additions & 14 deletions src/main/java/coffeemeet/server/auth/dto/SignupRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,18 @@

import coffeemeet.server.interest.domain.Keyword;
import coffeemeet.server.user.domain.OAuthProvider;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;

public record SignupRequest(
@NotBlank
String nickname,
@NotNull
List<Keyword> keywords,
@NotBlank
String authCode,
OAuthProvider oAuthProvider
) {

public static SignupRequest of(
String nickname,
List<Keyword> keywords,
String authCode,
OAuthProvider oAuthProvider
) {
return new SignupRequest(
nickname,
keywords,
authCode,
oAuthProvider
);
}

}
40 changes: 22 additions & 18 deletions src/main/java/coffeemeet/server/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import coffeemeet.server.auth.dto.SignupRequest;
import coffeemeet.server.auth.utils.AuthTokens;
import coffeemeet.server.auth.utils.AuthTokensGenerator;
import coffeemeet.server.auth.utils.JwtTokenProvider;
import coffeemeet.server.interest.domain.Interest;
import coffeemeet.server.interest.domain.Keyword;
import coffeemeet.server.interest.repository.InterestRepository;
Expand All @@ -16,21 +17,23 @@
import coffeemeet.server.user.repository.UserRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AuthService {

private static final String EXPIRED_REFRESH_TOKEN_MESSAGE = "리프레시 토큰이 만료되었습니다. 다시 로그인해 주세요.";
private static final String ALREADY_REGISTERED_MESSAGE = "이미 가입된 사용자입니다.";
private static final String USER_NOT_REGISTERED_MESSAGE = "해당 아이디(%s)와 로그인 타입(%s)의 유저는 회원가입되지 않았습니다.";

private final AuthCodeRequestUrlProviderComposite authCodeRequestUrlProviderComposite;
private final OAuthMemberClientComposite oauthMemberClientComposite;
private final UserRepository userRepository;
private final InterestRepository interestRepository;
private final AuthTokensGenerator authTokensGenerator;
private final JwtTokenProvider jwtTokenProvider;

public String getAuthCodeRequestUrl(OAuthProvider oAuthProvider) {
return authCodeRequestUrlProviderComposite.provide(oAuthProvider);
Expand All @@ -42,19 +45,9 @@ public AuthTokens signup(SignupRequest request) {
checkDuplicateUser(response);
String profileImage = checkProfileImage(response.profileImage());

User user = new User(
new OAuthInfo(
response.oAuthProvider(),
response.oAuthProviderId()
),
Profile.builder()
.name(response.name())
.nickname(request.nickname())
.email(response.email())
.profileImageUrl(profileImage)
.birth(response.birth())
.build()
);
User user = new User(new OAuthInfo(response.oAuthProvider(), response.oAuthProviderId()),
Profile.builder().name(response.name()).nickname(request.nickname()).email(response.email())
.profileImageUrl(profileImage).birth(response.birth()).build());

User newUser = userRepository.save(user);

Expand All @@ -63,11 +56,22 @@ public AuthTokens signup(SignupRequest request) {
return authTokensGenerator.generate(newUser.getId());
}

public Optional<AuthTokens> login(OAuthProvider oAuthProvider, String authCode) {
public AuthTokens login(OAuthProvider oAuthProvider, String authCode) {
OAuthInfoResponse response = oauthMemberClientComposite.fetch(oAuthProvider, authCode);
Optional<User> foundUser = userRepository.getUserByOauthInfoOauthProviderAndOauthInfoOauthProviderId(
response.oAuthProvider(), response.oAuthProviderId());
return foundUser.map(user -> authTokensGenerator.generate(user.getId()));
User foundUser = userRepository.getUserByOauthInfoOauthProviderAndOauthInfoOauthProviderId(
response.oAuthProvider(), response.oAuthProviderId()).orElseThrow(
() -> new IllegalArgumentException(
String.format(USER_NOT_REGISTERED_MESSAGE, response.oAuthProviderId(),
response.oAuthProvider())));
return authTokensGenerator.generate(foundUser.getId());
}

public AuthTokens renew(Long userId, String refreshToken) {
if (jwtTokenProvider.isExpiredRefreshToken(refreshToken)) {
throw new IllegalArgumentException(EXPIRED_REFRESH_TOKEN_MESSAGE);
} else {
return authTokensGenerator.reissueAccessToken(userId, refreshToken);
}
}

private void checkDuplicateUser(OAuthInfoResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public AuthTokens generate(Long userId) {
);
}

public AuthTokens refreshJwtToken(Long userId, String refreshToken) {
public AuthTokens reissueAccessToken(Long userId, String refreshToken) {
long now = (new Date()).getTime();
Date accessTokenExpiredAt = new Date(now + accessTokenExpireTime);

Expand Down

0 comments on commit 42cb6ff

Please sign in to comment.