Skip to content

Commit

Permalink
feat : user signOut
Browse files Browse the repository at this point in the history
  • Loading branch information
NameIsUser06 committed Dec 27, 2023
1 parent 5cb2ac4 commit 3d4c268
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.bssm.attachit.domain.auth.presentation;

import jakarta.servlet.http.HttpServletRequest;
import leehj050211.bsmOauth.exception.BsmOAuthCodeNotFoundException;
import leehj050211.bsmOauth.exception.BsmOAuthInvalidClientException;
import leehj050211.bsmOauth.exception.BsmOAuthTokenNotFoundException;
import lombok.RequiredArgsConstructor;
import org.bssm.attachit.domain.auth.presentation.dto.response.TokenResponse;
import org.bssm.attachit.domain.auth.service.UserLoginService;
import org.bssm.attachit.domain.auth.service.UserSignInService;
import org.bssm.attachit.domain.auth.service.UserSignOutService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -16,10 +18,16 @@
@RequestMapping("/auth")
public class AuthController {

private final UserLoginService userLoginService;
private final UserSignInService userSignInService;
private final UserSignOutService userSignOutService;

@PostMapping
public ResponseEntity<TokenResponse> signIn(@RequestParam("code") String code) throws BsmOAuthInvalidClientException, IOException, BsmOAuthCodeNotFoundException, BsmOAuthTokenNotFoundException {
return userLoginService.execute(code);
return userSignInService.execute(code);
}

@PostMapping("/signout")
public ResponseEntity<String> signOut(HttpServletRequest httpServletRequest) {
return userSignOutService.execute(httpServletRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface RefreshTokenRepository extends JpaRepository<RefreshToken, Long
boolean existsByAccessToken(String token);

Optional<RefreshToken> findByEmail(String email);

void deleteByRefreshToken(String refreshToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@Service
@RequiredArgsConstructor
public class UserLoginService {
public class UserSignInService {

private final UserRepository userRepository;
private final AuthProperties authProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.bssm.attachit.domain.auth.service;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.bssm.attachit.domain.auth.repository.RefreshTokenRepository;
import org.bssm.attachit.global.jwt.exception.TokenNotFoundException;
import org.bssm.attachit.global.jwt.util.JwtUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class UserSignOutService {

private final RefreshTokenRepository refreshTokenRepository;
private final JwtUtil jwtUtil;

public ResponseEntity<String> execute(HttpServletRequest httpServletRequest) {
String refreshToken = httpServletRequest.getHeader("Authorization-refresh").split(" ")[1].trim();
if (refreshToken.isEmpty()) {
throw TokenNotFoundException.EXCEPTION;
}
refreshTokenRepository.deleteByRefreshToken(refreshToken);
return ResponseEntity.ok("success");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ResponseEntity<ErrorResponse> handleGlobal(AttachItException e) {
final ErrorCode errorCode = e.getErrorCode();
log.error(
"\n" + "{\n" +
"\t\"status\": " + errorCode.getStatus() + '\"' +
"\t\"status\": " + errorCode.getStatus() +
",\n\t\"message\": \"" + errorCode.getMessage() + '\"' +
"\n}"
);
Expand All @@ -27,3 +27,4 @@ public ResponseEntity<ErrorResponse> handleGlobal(AttachItException e) {
HttpStatus.valueOf(errorCode.getStatus()));
}
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.bssm.attachit.global.error.exception;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum ErrorCode {
USER_NOT_FOUND(404, "유저를 찾을 수 없습니다"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class JwtFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String token = jwtUtil.resolveToken(request);

if (token != null && !token.isEmpty()) {
if (!refreshTokenRepository.existsByAccessToken(token)) {
throw LoggedOutAccessTokenException.EXCEPTION;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spring:
max-file-size: 20MB

jpa:
show-sql: true
show-sql: false
hibernate:
ddl-auto: update
open-in-view: false
Expand Down

0 comments on commit 3d4c268

Please sign in to comment.