diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/auth/dto/LoginRequest.java b/backend/memetory/src/main/java/com/example/memetory/domain/auth/dto/LoginRequest.java index 32e8390..90fa79e 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/auth/dto/LoginRequest.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/auth/dto/LoginRequest.java @@ -17,4 +17,6 @@ public class LoginRequest { private String token; @Schema(description = "인증서버타입, google, kakao 가능") private SocialType socialType; + @Schema(description = "사용자의 기기 fcm 토큰") + private String fcmToken; } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/auth/service/AuthService.java b/backend/memetory/src/main/java/com/example/memetory/domain/auth/service/AuthService.java index 81d6813..788dbed 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/auth/service/AuthService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/auth/service/AuthService.java @@ -13,6 +13,7 @@ import com.example.memetory.global.security.jwt.service.JwtService; import jakarta.servlet.http.HttpServletResponse; +import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; @Service @@ -22,9 +23,11 @@ public class AuthService { private final OAuth2ProviderService oAuth2ProviderService; private final JwtService jwtService; + @Transactional public void authenticateOrRegisterUser(LoginRequest loginRequest, HttpServletResponse response) { OAuth2UserInfo userInfo = oAuth2ProviderService.getUserInfo(loginRequest); Member member = findOrElseRegisterMember(userInfo, loginRequest.getSocialType()); + member.updateFcmToken(loginRequest.getFcmToken()); jwtService.sendAccessAndRefreshToken(response, member.getEmail()); } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/entity/Member.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/entity/Member.java index a49bb9c..6a5de30 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/entity/Member.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/entity/Member.java @@ -31,6 +31,7 @@ public class Member extends BaseEntity { private String nickname; private String name; private String imageUrl; + private String fcmToken; @Enumerated(EnumType.STRING) private Role role; @@ -41,12 +42,13 @@ public class Member extends BaseEntity { private String socialId; @Builder - public Member(String email, String nickname, String name, String imageUrl, Role role, SocialType socialType, - String socialId) { + public Member(String email, String nickname, String name, String imageUrl, String fcmToken, Role role, + SocialType socialType, String socialId) { this.email = email; this.nickname = nickname; this.name = name; this.imageUrl = imageUrl; + this.fcmToken = fcmToken; this.role = role; this.socialType = socialType; this.socialId = socialId; @@ -57,4 +59,8 @@ public void update(MemberServiceDto memberServiceDto) { this.nickname = memberServiceDto.getNickname(); this.imageUrl = memberServiceDto.getImageUrl(); } + + public void updateFcmToken(String fcmToken) { + this.fcmToken = fcmToken; + } } diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthFixture.java b/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthFixture.java index d34639b..b963654 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthFixture.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthFixture.java @@ -4,7 +4,6 @@ import com.example.memetory.domain.auth.dto.LoginRequest; import com.example.memetory.domain.member.entity.SocialType; -import com.example.memetory.global.security.jwt.dto.TokenResponse; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -16,18 +15,11 @@ public static String JSON_GOOGLE_LOGIN_REQUEST() throws JsonProcessingException } public static LoginRequest GOOGLE_LOGIN_REQUEST() { - return new LoginRequest("google login token", SocialType.GOOGLE); + return new LoginRequest("google login token", SocialType.GOOGLE, "fcmToken"); } - public static String JSON_TOKEN_RESPONSE() throws JsonProcessingException { - return mapper.writeValueAsString(TOKEN_RESPONSE()); - } - - public static TokenResponse TOKEN_RESPONSE() { - return TokenResponse.builder() - .accessToken("access token") - .refreshToken("refresh token") - .build(); + public static LoginRequest GOOGLE_LOGIN_REQUEST_OTHER_FCM_TOKE() { + return new LoginRequest("google login token", SocialType.GOOGLE, "fcmToken"); } public static String JSON_GOOGLE_OAUTH2_RESPONSE() throws JsonProcessingException { diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthIntegrationTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthIntegrationTest.java index 0c54278..18dee52 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthIntegrationTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthIntegrationTest.java @@ -10,6 +10,7 @@ import org.springframework.http.MediaType; import com.example.memetory.domain.auth.dto.LoginRequest; +import com.example.memetory.domain.member.entity.Member; import com.example.memetory.global.integration.BaseIntegrationTest; import com.example.memetory.global.integration.MockServer; import com.example.memetory.global.security.jwt.dto.TokenResponse; @@ -51,6 +52,10 @@ void Given_LoginRequest_When_login_Then_CreateMember() throws JsonProcessingExce TokenResponse result = response.as(TokenResponse.class, ObjectMapperType.JACKSON_2); // then + checkTokenResponse(result); + } + + private void checkTokenResponse(TokenResponse result) { assertTrue(jwtService.isTokenValid(result.getAccessToken())); assertTrue(jwtService.isTokenValid(result.getRefreshToken())); } @@ -59,7 +64,7 @@ void Given_LoginRequest_When_login_Then_CreateMember() throws JsonProcessingExce @Test void Given_LoginRequest_When_login_Then_Return_HttpStatus_OK() throws JsonProcessingException { // given - LoginRequest loginRequest = GOOGLE_LOGIN_REQUEST(); + LoginRequest loginRequest = GOOGLE_LOGIN_REQUEST_OTHER_FCM_TOKE(); MockServer.startOauth2GoogleServerFromLoginRequest(loginRequest); @@ -80,7 +85,12 @@ void Given_LoginRequest_When_login_Then_Return_HttpStatus_OK() throws JsonProces TokenResponse result = response.as(TokenResponse.class, ObjectMapperType.JACKSON_2); // then - assertTrue(jwtService.isTokenValid(result.getAccessToken())); - assertTrue(jwtService.isTokenValid(result.getRefreshToken())); + checkMemberFcmTokenUpdate(loginRequest); + checkTokenResponse(result); + } + + private void checkMemberFcmTokenUpdate(LoginRequest loginRequest) { + Member updateMember = memberRepository.findById(member.getId()).get(); + assertEquals(updateMember.getFcmToken(), loginRequest.getFcmToken()); } }