Skip to content

Commit

Permalink
[fix] add fcmToken in logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tkdwns414 committed Jun 7, 2024
1 parent 75b4e81 commit b87e588
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class AuthConstant {
public static final String USER_ID_CLAIM_NAME = "uid";
public static final String BEARER_PREFIX = "Bearer ";
public static final String AUTHORIZATION_HEADER = "Authorization";

public static final String FCM_TOKEN_HEADER = "FcmToken";
public static final String[] AUTH_WHITELIST = {
"/api/v1/auth/login/kakao",
"/api/v1/test/**",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public class AuthController {

@PostMapping("/auth/login/kakao")
public ResponseEntity<JwtTokensDto> KakaoLogin(
@NotNull @RequestHeader(AuthConstant.AUTHORIZATION_HEADER) final String kakaoToken
@NotNull @RequestHeader(AuthConstant.AUTHORIZATION_HEADER) final String kakaoToken,
@RequestHeader(name=AuthConstant.FCM_TOKEN_HEADER, required = false) final String fcmToken
) {
return ResponseEntity.ok(authService.login(kakaoToken));
return ResponseEntity.ok(authService.login(kakaoToken, fcmToken));
}

@DeleteMapping("/auth/logout")
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/telepigeon/server/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public class User {
private User(
String name,
String email,
// String fcmToken,
String fcmToken,
String serialId,
String provider
) {
this.name = name;
this.email = email;
// this.fcmToken = fcmToken;
this.fcmToken = fcmToken;
this.serialId = serialId;
this.provider = provider;
this.createdAt = LocalDateTime.now();
Expand All @@ -49,10 +49,10 @@ private User(
public static User create(
String name,
String email,
// TODO: String fcmToken,
String fcmToken,
String serialId,
String provider) {
return new User(name, email, serialId, provider);
return new User(name, email, fcmToken, serialId, provider);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class AuthService {
private final JwtUtil jwtUtil;

@Transactional
public JwtTokensDto login(final String token){
public JwtTokensDto login(final String token, final String fcmToken){
SocialUserInfoDto socialUserInfo = kakaoService.getUserInfo(token);
User user = loadOrCreateKakaoUser(socialUserInfo);
User user = loadOrCreateKakaoUser(socialUserInfo, fcmToken);
JwtTokensDto tokens = jwtUtil.generateTokens(user.getId());
tokenSaver.save(Token.create(user.getId(), tokens.refreshToken()));
return tokens;
Expand Down Expand Up @@ -58,7 +58,7 @@ public JwtTokensDto reissue(final String Authorization) {
return tokens;
}

private User loadOrCreateKakaoUser(final SocialUserInfoDto socialUserInfo) {
private User loadOrCreateKakaoUser(final SocialUserInfoDto socialUserInfo, final String fcmToken) {
boolean isRegistered = userRetreiver.existBySerialIdAndProvider(
socialUserInfo.serialId(),
"kakao"
Expand All @@ -68,6 +68,7 @@ private User loadOrCreateKakaoUser(final SocialUserInfoDto socialUserInfo) {
User newUser = User.create(
socialUserInfo.name(),
socialUserInfo.email(),
fcmToken,
socialUserInfo.serialId(),
"kakao"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class KakaoLoginTest {
void setUp() throws Exception {
token = "validAccessToken";
socialUserInfo = SocialUserInfoDto.of("123456", "테스트", "[email protected]");
user = User.create("123456", "테스트", "kakao", "[email protected]");
user = User.create("123456", "테스트", "fcmToken", "kakao", "[email protected]");

idField = User.class.getDeclaredField("id"); // private인 id를 설정하지 못해 리플렉션 사용
idField.setAccessible(true);
Expand Down Expand Up @@ -97,7 +97,7 @@ void kakaoRegisterSuccessTest() {
);

// when
JwtTokensDto jwts = authService.login(token);
JwtTokensDto jwts = authService.login(token, "fcmToken");
Claims claims = jwtUtil.getTokenBody(jwts.accessToken());

// then
Expand All @@ -113,7 +113,7 @@ void kakaoLoginSuccessTest() {
given(userRetriever.findBySerialIdAndProvider("123456", "kakao")).willReturn(user);

// when
JwtTokensDto jwts = authService.login(token);
JwtTokensDto jwts = authService.login(token, "fcmToken");
Claims claims = jwtUtil.getTokenBody(jwts.accessToken());

// then
Expand All @@ -128,7 +128,7 @@ void invalidKakaoTokenTest() {
String invalidToken = "invalidToken";

// when
Throwable thrown = Assertions.catchThrowable(() -> authService.login(invalidToken));
Throwable thrown = Assertions.catchThrowable(() -> authService.login(invalidToken, "fcmToken"));

// then
Assertions.assertThat(thrown).isInstanceOf(UnAuthorizedException.class);
Expand Down

0 comments on commit b87e588

Please sign in to comment.