Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 로그아웃 구현 #56

Merged
merged 4 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.issuetrackermax.controller.ApiResponse;
import com.issuetrackermax.controller.auth.dto.request.JwtRefreshTokenRequest;
import com.issuetrackermax.controller.auth.dto.request.LoginRequest;
import com.issuetrackermax.controller.auth.dto.request.LogoutRequest;
import com.issuetrackermax.controller.auth.dto.response.JwtResponse;
import com.issuetrackermax.service.jwt.JwtService;

Expand All @@ -30,6 +31,14 @@ public ApiResponse<JwtResponse> login(
);
}

@PostMapping("/logout")
public ApiResponse<Void> logout(
@RequestBody
@Valid LogoutRequest request) {
jwtService.logout(request.getRefreshToken());
return ApiResponse.success();
}

@PostMapping("/reissue-access-token")
public ApiResponse<JwtResponse> reissueAccessToken(
@RequestBody
Expand All @@ -38,4 +47,5 @@ public ApiResponse<JwtResponse> reissueAccessToken(
JwtResponse.from(jwtService.reissueAccessToken(request.getRefreshToken()))
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.issuetrackermax.controller.auth.dto.request;

import javax.validation.constraints.NotBlank;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class LogoutRequest {
@NotBlank
private String refreshToken;

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ApiResponse<MilestonePostResponse> post(@RequestBody MilestonePostRequest
return ApiResponse.success(response);
}

@PutMapping("/id")
@PutMapping("/{id}")
public ApiResponse<Void> modify(@PathVariable Long id, @RequestBody MilestoneModifyRequest milestoneModifyRequest) {
milestoneService.update(id, milestoneModifyRequest);
return ApiResponse.success();
Expand Down
12 changes: 12 additions & 0 deletions be/src/main/java/com/issuetrackermax/domain/jwt/JwtRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ public Long saveRefreshToken(String refreshToken, Long id) {
.addValue("id", id), keyHolder);
return Objects.requireNonNull(keyHolder.getKey()).longValue();
}

public int deleteRefreshToken(String refreshToken, Long id) {
String sql = "DELETE FROM token WHERE refresh_token = :refreshToken and id = :id";
return jdbcTemplate.update(sql, new MapSqlParameterSource()
.addValue("refreshToken", refreshToken)
.addValue("id", id));
}

public Boolean existsRefreshToken(String refreshToken) {
String sql = "SELECT EXISTS (SELECT 1 FROM token WHERE refresh_token = :refreshToken)";
return jdbcTemplate.queryForObject(sql, Map.of("refreshToken", refreshToken), Boolean.class);
}
}
11 changes: 10 additions & 1 deletion be/src/main/java/com/issuetrackermax/service/jwt/JwtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class JwtService {
private final JwtProvider jwtProvider;

@Transactional
public Jwt login(String email, String password) {
public Jwt login(String email, String password) {
Member member = memberRepository.findByMemberLoginId(email).get();

if (!verifyPassword(member, password)) {
Expand All @@ -43,6 +43,15 @@ public Jwt reissueAccessToken(String refreshToken) {
return jwtProvider.reissueAccessToken(generateMemberClaims(memberId), refreshToken);
}

@Transactional
public void logout(String refreshToken) {
jwtProvider.getClaims(refreshToken);
Long memberId = jwtRepository.findByRefreshToken(refreshToken);

jwtRepository.deleteRefreshToken(refreshToken, memberId);
return;
}

private Map<String, Object> generateMemberClaims(Long memberId) {
return Map.of(
"memberId", memberId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.issuetrackermax.controller.label.dto.request.LabelModifyRequest;
import com.issuetrackermax.controller.label.dto.request.LabelPostRequest;
Expand All @@ -23,15 +24,18 @@ public List<LabelDetailResponse> getLabelList() {
return labels.stream().map(LabelDetailResponse::from).collect(Collectors.toList());
}

@Transactional
public Long save(LabelPostRequest labelPostRequest) {
return labelRepository.save(Label.from(labelPostRequest));
}

@Transactional
public void update(Long id, LabelModifyRequest labelModifyRequest) {
labelRepository.update(id, Label.from(labelModifyRequest));
return;
}

@Transactional
public void delete(Long id) {
int count = labelRepository.deleteById(id);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.issuetrackermax.controller.milestone.dto.request.MilestoneModifyRequest;
import com.issuetrackermax.controller.milestone.dto.request.MilestonePostRequest;
Expand Down Expand Up @@ -64,21 +65,25 @@ private List<MilestoneDetailResponse> getMilestoneDetailResponses(List<Milestone
return response;
}

@Transactional
public Long save(MilestonePostRequest milestonePostRequest) {
Milestone milestone = Milestone.from(milestonePostRequest);
return milestoneRepository.save(milestone);
}

@Transactional
public void update(Long id, MilestoneModifyRequest milestoneModifyRequest) {
milestoneRepository.update(id, Milestone.from(milestoneModifyRequest));
return;
}

@Transactional
public void delete(Long id) {
int count = milestoneRepository.deleteById(id);
return;
}

@Transactional
public void updateStatus(Long id) {
milestoneRepository.updateStatus(id);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.issuetrackermax.common.exception.domain.LoginException;
import com.issuetrackermax.controller.member.dto.request.SignUpRequest;
import com.issuetrackermax.domain.IntegrationTestSupport;
import com.issuetrackermax.domain.jwt.JwtRepository;
import com.issuetrackermax.domain.jwt.entity.Jwt;
import com.issuetrackermax.service.member.MemberService;
import com.issuetrackermax.util.DatabaseCleaner;
Expand All @@ -23,6 +24,9 @@ class JwtServiceTest extends IntegrationTestSupport {
@Autowired
MemberService memberService;

@Autowired
JwtRepository jwtRepository;

@Autowired
DatabaseCleaner databaseCleaner;

Expand Down Expand Up @@ -123,4 +127,25 @@ void reissueAccessToken() {
assertThat(jwtProvider.getClaims(refreshToken).get("memberId")).isNull();

}

@DisplayName("logout을 하면 저장된 refreshToken이 삭제된다.")
@Test
void logout() {
// given
SignUpRequest signUpRequest = SignUpRequest.builder()
.loginId("[email protected]")
.nickName("June")
.password("12345678")
.build();
memberService.registerMember(signUpRequest);
Jwt jwt = jwtService.login("[email protected]", "12345678");
String refreshToken = jwt.getRefreshToken();
// when
jwtService.logout(refreshToken);

// then
Boolean exist = jwtRepository.existsRefreshToken(refreshToken);
assertThat(exist).isFalse();

}
}