Skip to content

Commit

Permalink
Merge pull request #98 from tukcomCD2024/feat#58/set-fcm
Browse files Browse the repository at this point in the history
Feat#58/set fcm
  • Loading branch information
youngseo3 authored Jun 5, 2024
2 parents 2ac75b1 + 70a64a1 commit 84935fa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public class LoginRequest {
private String token;
@Schema(description = "인증서버타입, google, kakao 가능")
private SocialType socialType;
@Schema(description = "사용자의 기기 fcm 토큰")
private String fcmToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
Expand All @@ -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);

Expand All @@ -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());
}
}

0 comments on commit 84935fa

Please sign in to comment.