Skip to content

Commit

Permalink
[feat] 소셜 로그인 API 관련 수정 작업 (fcm Token 추가) (#48)
Browse files Browse the repository at this point in the history
* [feat] #46 add fcmToken in userInfo entity and update when signin

* [feat] #46 modify response type to add user name
  • Loading branch information
chaewonni authored Jul 12, 2024
1 parent 73fb3d5 commit 7d38278
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.kkumulkkum.server.constant.Constant;
import org.kkumulkkum.server.dto.auth.request.UserLoginDto;
import org.kkumulkkum.server.dto.auth.response.JwtTokenDto;
import org.kkumulkkum.server.dto.auth.response.UserTokenDto;
import org.kkumulkkum.server.service.auth.AuthService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -19,7 +20,7 @@ public class AuthController {
private final AuthService authService;

@PostMapping("/auth/signin")
public ResponseEntity<JwtTokenDto> signin(
public ResponseEntity<UserTokenDto> signin(
@NotNull @RequestHeader(Constant.AUTHORIZATION_HEADER) final String providerToken,
@Valid @RequestBody final UserLoginDto userLoginDto
) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/kkumulkkum/server/domain/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class UserInfo extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String fcmToken;

private String name;

private String profileImg;
Expand Down Expand Up @@ -48,6 +50,11 @@ public UserInfo(String name,
this.tardySum = 0L;
}


public void updateFcmToken(String fcmToken) {
this.fcmToken = fcmToken;
}

public void updateImage(String imageUrl) {
this.profileImg = imageUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

public record UserLoginDto(
@NotNull
Provider provider
Provider provider,
@NotNull
String fcmToken
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.kkumulkkum.server.dto.auth.response;

import org.kkumulkkum.server.domain.UserInfo;

public record UserTokenDto(
String name,
JwtTokenDto jwtTokenDto
) {
public static UserTokenDto of(UserInfo userInfo, JwtTokenDto tokens) {
return new UserTokenDto(
userInfo.getName(),
tokens
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import org.kkumulkkum.server.domain.enums.Role;
import org.kkumulkkum.server.dto.auth.request.UserLoginDto;
import org.kkumulkkum.server.dto.auth.response.JwtTokenDto;
import org.kkumulkkum.server.dto.auth.response.UserTokenDto;
import org.kkumulkkum.server.exception.AuthException;
import org.kkumulkkum.server.exception.code.AuthErrorCode;
import org.kkumulkkum.server.service.user.UserRetriever;
import org.kkumulkkum.server.service.user.UserSaver;
import org.kkumulkkum.server.service.userInfo.UserInfoRetriever;
import org.kkumulkkum.server.service.userInfo.UserInfoSaver;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -33,14 +35,17 @@ public class AuthService {
private final JwtTokenProvider jwtTokenProvider;
private final TokenRetriever tokenRetriever;
private final TokenRemover tokenRemover;
private final UserInfoRetriever userInfoRetriever;

@Transactional
public JwtTokenDto signin(final String providerToken, final UserLoginDto userLoginDto) {
public UserTokenDto signin(final String providerToken, final UserLoginDto userLoginDto) {
SocialUserDto socialUserDto = getSocialInfo(providerToken, userLoginDto);
User user = loadOrCreateUser(userLoginDto.provider(), socialUserDto);
UserInfo userInfo = userInfoRetriever.findByUserId(user.getId());
userInfo.updateFcmToken(userLoginDto.fcmToken());
JwtTokenDto tokens = jwtTokenProvider.issueTokens(user.getId());
saveToken(user.getId(), tokens);
return tokens;
return UserTokenDto.of(userInfo, tokens);
}

@Transactional
Expand Down

0 comments on commit 7d38278

Please sign in to comment.