Skip to content

Commit

Permalink
Implement device service to manage device token and fcm token refresh…
Browse files Browse the repository at this point in the history
… tokens simultaneously (#145)
  • Loading branch information
inh2613 authored Oct 27, 2023
2 parents 3f9f86d + 1bce1c4 commit 05b374e
Show file tree
Hide file tree
Showing 23 changed files with 270 additions and 420 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
import org.swmaestro.repl.gifthub.auth.dto.SignInDto;
import org.swmaestro.repl.gifthub.auth.dto.SignOutDto;
import org.swmaestro.repl.gifthub.auth.dto.SignUpDto;
import org.swmaestro.repl.gifthub.auth.dto.UserDeviceDto;
import org.swmaestro.repl.gifthub.auth.service.AuthService;
import org.swmaestro.repl.gifthub.auth.service.RefreshTokenService;
import org.swmaestro.repl.gifthub.auth.service.DeviceService;
import org.swmaestro.repl.gifthub.auth.type.OAuthPlatform;
import org.swmaestro.repl.gifthub.util.JwtProvider;
import org.swmaestro.repl.gifthub.util.Message;
Expand All @@ -31,7 +32,7 @@
@Tag(name = "Auth", description = "사용자 인증 관련 API")
public class AuthController {
private final AuthService authService;
private final RefreshTokenService refreshTokenService;
private final DeviceService deviceService;
private final JwtProvider jwtProvider;

@PostMapping("/sign-up")
Expand Down Expand Up @@ -74,17 +75,17 @@ public ResponseEntity<Message> signIn(HttpServletRequest request, @RequestBody S
@ApiResponse(responseCode = "200", description = "Access Token 재발급 성공"),
@ApiResponse(responseCode = "400", description = "Access Token 재발급 실패"),
})
public ResponseEntity<Message> reissueAccessToken(HttpServletRequest request, @RequestHeader("Authorization") String refreshToken) {
String newAccessToken = refreshTokenService.createNewAccessTokenByValidateRefreshToken(refreshToken);
String newRefreshToken = refreshTokenService.createNewRefreshTokenByValidateRefreshToken(refreshToken);
public ResponseEntity<Message> reissueAccessToken(HttpServletRequest request, @RequestHeader("Authorization") String refreshToken,
@RequestBody UserDeviceDto userDeviceDto) {
String newAccessToken = deviceService.createNewAccessTokenByValidateRefreshToken(refreshToken);
String newRefreshToken = deviceService.createNewRefreshTokenByValidateRefreshToken(refreshToken);

JwtTokenDto jwtTokenDto = JwtTokenDto.builder()
.accessToken(newAccessToken)
.refreshToken(newRefreshToken)
.build();

refreshToken = refreshToken.substring(7);
refreshTokenService.storeRefreshToken(jwtTokenDto, jwtProvider.getUsername(refreshToken));
deviceService.create(jwtTokenDto, userDeviceDto.getDeviceToken(), userDeviceDto.getFcmToken());

return ResponseEntity.ok(
SuccessMessage.builder()
Expand Down Expand Up @@ -163,8 +164,8 @@ public ResponseEntity<Message> appleSignIn(HttpServletRequest request, @RequestB
@ApiResponse(responseCode = "400(401)", description = "존재하지 않는 사용자")
})
public ResponseEntity<Message> signOut(HttpServletRequest request, @RequestBody SignOutDto signOutDto) {
String username = jwtProvider.getUsername(jwtProvider.resolveToken(request).substring(7));
authService.signOut(username, signOutDto);
Long userId = jwtProvider.getUserId(jwtProvider.resolveToken(request).substring(7));
authService.signOut(userId, signOutDto);
return ResponseEntity.ok(
SuccessMessage.builder()
.path(request.getRequestURI())
Expand All @@ -177,8 +178,8 @@ public ResponseEntity<Message> signOut(HttpServletRequest request, @RequestBody
@ApiResponses({
@ApiResponse(responseCode = "200", description = "회원가입 성공"),
})
public ResponseEntity<Message> signUpAnonymous(HttpServletRequest request) {
JwtTokenDto jwtTokenDto = authService.signUpAnonymous();
public ResponseEntity<Message> signUpAnonymous(HttpServletRequest request, @RequestBody UserDeviceDto userDeviceDto) {
JwtTokenDto jwtTokenDto = authService.signUpAnonymous(userDeviceDto);
return ResponseEntity.ok(
SuccessMessage.builder()
.path(request.getRequestURI())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class OAuthTokenDto {
private String token;
private String deviceToken;
private String fcmToken;

@Builder
public OAuthTokenDto(String token) {
public OAuthTokenDto(String token, String deviceToken, String fcmToken) {
this.token = token;
this.deviceToken = deviceToken;
this.fcmToken = fcmToken;
}
}
13 changes: 12 additions & 1 deletion src/main/java/org/swmaestro/repl/gifthub/auth/dto/SignInDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.io.Serializable;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
Expand All @@ -13,6 +16,7 @@
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class SignInDto implements Serializable {
@NotNull
@Size(min = 4, max = 60)
Expand All @@ -22,9 +26,16 @@ public class SignInDto implements Serializable {
@Size(min = 8, max = 64)
private String password;

@NotNull
private String deviceToken;

private String fcmToken;

@Builder
public SignInDto(String username, String password) {
public SignInDto(String username, String password, String deviceToken, String fcmToken) {
this.username = username;
this.password = password;
this.deviceToken = deviceToken;
this.fcmToken = fcmToken;
}
}
13 changes: 12 additions & 1 deletion src/main/java/org/swmaestro/repl/gifthub/auth/dto/SignUpDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.io.Serializable;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
Expand All @@ -13,6 +16,7 @@
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class SignUpDto implements Serializable {
@NotNull
@Size(min = 4, max = 60)
Expand All @@ -26,10 +30,17 @@ public class SignUpDto implements Serializable {
@Size(min = 2, max = 12)
private String nickname;

@NotNull
private String deviceToken;

private String fcmToken;

@Builder
public SignUpDto(String username, String password, String nickname) {
public SignUpDto(String username, String password, String nickname, String deviceToken, String fcmToken) {
this.username = username;
this.password = password;
this.nickname = nickname;
this.deviceToken = deviceToken;
this.fcmToken = fcmToken;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.swmaestro.repl.gifthub.auth.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class UserDeviceDto {
private String deviceToken;
private String fcmToken;

@Builder
public UserDeviceDto(String deviceToken, String fcmToken) {
this.deviceToken = deviceToken;
this.fcmToken = fcmToken;
}
}
52 changes: 52 additions & 0 deletions src/main/java/org/swmaestro/repl/gifthub/auth/entity/Device.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.swmaestro.repl.gifthub.auth.entity;

import java.time.LocalDateTime;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@EntityListeners(AuditingEntityListener.class)
public class Device {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private Long userId;

@Column(length = 255, nullable = false)
private String refreshToken;

@Column(length = 200, nullable = false)
private String deviceToken;

@Column(length = 200)
private String fcmToken;

@CreatedDate
@Column(nullable = false)
private LocalDateTime createdAt;

@Builder
public Device(Long id, Long userId, String refreshToken, String deviceToken, String fcmToken) {
this.id = id;
this.userId = userId;
this.refreshToken = refreshToken;
this.deviceToken = deviceToken;
this.fcmToken = fcmToken;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.swmaestro.repl.gifthub.auth.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.swmaestro.repl.gifthub.auth.entity.Device;

public interface DeviceRepository extends JpaRepository<Device, Long> {

Optional<Device> findByUserIdAndDeviceToken(Long userId, String deviceToken);

void deleteByUserIdAndDeviceToken(Long userId, String deviceToken);

Device findByRefreshToken(String refreshToken);

List<Device> findAllByUserId(Long userId);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
User findByNickname(String nickname);

User findByUsernameAndDeletedAtIsNull(String username);

User findByIdAndDeletedAtIsNull(Long userId);
}
Loading

0 comments on commit 05b374e

Please sign in to comment.