Skip to content

Commit

Permalink
Add device-token deletion logic when sign-out (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinlee1703 authored Sep 18, 2023
2 parents 3157899 + 643e890 commit 978324d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.swmaestro.repl.gifthub.auth.dto.KakaoDto;
import org.swmaestro.repl.gifthub.auth.dto.NaverDto;
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.TokenDto;
import org.swmaestro.repl.gifthub.auth.entity.Member;
Expand Down Expand Up @@ -259,17 +260,14 @@ public ResponseEntity<Message> appleCallback(@RequestBody String code) throws IO
@ApiResponse(responseCode = "200", description = "로그인 성공"),
@ApiResponse(responseCode = "400(401)", description = "존재하지 않는 사용자")
})
public ResponseEntity<Message> signOut(HttpServletRequest request) {
public ResponseEntity<Message> signOut(HttpServletRequest request, @RequestBody SignOutDto signOutDto) {
String username = jwtProvider.getUsername(jwtProvider.resolveToken(request).substring(7));
authService.signOut(username);
return new ResponseEntity<Message>(
authService.signOut(username, signOutDto);
return ResponseEntity.ok().body(
Message.builder()
.status(StatusEnum.OK)
.message("로그아웃 성공!")
.data(null)
.build(),
new HttpJsonHeaders(),
HttpStatus.OK
);
.build());
}
}
23 changes: 23 additions & 0 deletions src/main/java/org/swmaestro/repl/gifthub/auth/dto/SignOutDto.java
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;
import lombok.ToString;

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

@Builder
public SignOutDto(String deviceToken) {
this.deviceToken = deviceToken;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.swmaestro.repl.gifthub.auth.service;

import org.swmaestro.repl.gifthub.auth.dto.SignInDto;
import org.swmaestro.repl.gifthub.auth.dto.SignOutDto;
import org.swmaestro.repl.gifthub.auth.dto.TokenDto;

public interface AuthService {

TokenDto signIn(SignInDto loginDto);

void signOut(String username);
void signOut(String username, SignOutDto signOutDto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.swmaestro.repl.gifthub.auth.dto.SignInDto;
import org.swmaestro.repl.gifthub.auth.dto.SignOutDto;
import org.swmaestro.repl.gifthub.auth.dto.TokenDto;
import org.swmaestro.repl.gifthub.auth.entity.Member;
import org.swmaestro.repl.gifthub.auth.repository.MemberRepository;
Expand All @@ -21,6 +22,7 @@ public class AuthServiceImpl implements AuthService {
private final JwtProvider jwtProvider;
private final RefreshTokenService refreshTokenService;
private final NaverService naverService;
private final DeviceTokenService deviceTokenService;

public TokenDto signIn(SignInDto loginDto) {
Member member = memberRepository.findByUsername(loginDto.getUsername());
Expand All @@ -44,11 +46,12 @@ public TokenDto signIn(SignInDto loginDto) {
}

@Transactional
public void signOut(String username) {
public void signOut(String username, SignOutDto signOutDto) {
Member member = memberRepository.findByUsername(username);
if (member == null) {
throw new BusinessException("존재하지 않는 사용자입니다.", StatusEnum.UNAUTHORIZED);
}
refreshTokenService.deleteRefreshToken(username);
deviceTokenService.delete(signOutDto.getDeviceToken());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.swmaestro.repl.gifthub.auth.dto.KakaoDto;
import org.swmaestro.repl.gifthub.auth.dto.NaverDto;
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.TokenDto;
import org.swmaestro.repl.gifthub.auth.entity.Member;
Expand Down Expand Up @@ -274,11 +275,17 @@ public void signOutTest() throws Exception {
String username = "jinlee1703";
String accessToken = "my_awesome_access_token";

SignOutDto signOutDto = SignOutDto.builder()
.deviceToken("my_awesome_device_token")
.build();

when(jwtProvider.getUsername(accessToken)).thenReturn(username);
when(jwtProvider.resolveToken(any())).thenReturn(accessToken);

mockMvc.perform(post("/auth/sign-out")
.header("Authorization", "Bearer " + accessToken))
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(signOutDto)))
.andExpect(status().isOk());

}
Expand Down

0 comments on commit 978324d

Please sign in to comment.