Skip to content

Commit

Permalink
feat: 로그아웃 기능 구현 (#195)
Browse files Browse the repository at this point in the history
* [#191] feat: 로그인 시 제공되는 세션 만료 기간 설정

* [#191] feat: 로그아웃 시 쿠키 만료시키기
  • Loading branch information
shin-mallang authored Jan 1, 2024
1 parent b0e64e6 commit eb6ae3f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/mallang/auth/presentation/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import com.mallang.auth.presentation.support.Auth;
import com.mallang.auth.query.MemberQueryService;
import com.mallang.auth.query.response.MemberResponse;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import java.net.URI;
Expand Down Expand Up @@ -64,6 +66,19 @@ public ResponseEntity<Void> login(
return ResponseEntity.status(OK).build();
}

@GetMapping("/logout")
public ResponseEntity<Void> logout(
HttpServletRequest request,
HttpServletResponse response
) {
request.getSession().invalidate();
for (Cookie cookie : request.getCookies()) {
cookie.setMaxAge(0);
response.addCookie(cookie);
}
return ResponseEntity.status(OK).build();
}

@GetMapping("/{id}")
public ResponseEntity<MemberResponse> findProfile(
@PathVariable("id") Long memberId
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ auth:
session:
ttl: 3600

server:
servlet:
session:
cookie:
path: /
max-age: 864000 # 10일 = 10 * 24 * 60 * 60 초
http-only: true
secure: false

#logging:
# level:
# org.hibernate.orm.jdbc.bind: TRACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public class MemberAcceptanceSteps {
.extract();
}

public static ExtractableResponse<Response> 로그아웃_요청(
String 세션
) {
return given(세션)
.get("/members/logout")
.then()
.log().all()
.extract();
}

public static ExtractableResponse<Response> 일반_로그인_요청(
String 아이디,
String 비밀번호
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static com.mallang.acceptance.AcceptanceSteps.찾을수_없음;
import static com.mallang.acceptance.auth.AuthAcceptanceSteps.회원가입과_로그인_후_세션_ID_반환;
import static com.mallang.acceptance.auth.MemberAcceptanceSteps.내_정보_조회_요청;
import static com.mallang.acceptance.auth.MemberAcceptanceSteps.로그아웃_요청;
import static com.mallang.acceptance.auth.MemberAcceptanceSteps.아이디_중복_체크_요청;
import static com.mallang.acceptance.auth.MemberAcceptanceSteps.일반_로그인_요청;
import static com.mallang.acceptance.auth.MemberAcceptanceSteps.일반_회원가입_요청;
Expand Down Expand Up @@ -132,6 +133,24 @@ void setUp() {
}
}


@Nested
class 로그아웃_API {

@Test
void 로그아웃한다() {
// given
var 말랑_세션_ID = 회원가입과_로그인_후_세션_ID_반환("mallang");

// when
var 응답 = 로그아웃_요청(말랑_세션_ID);

// then
응답_상태를_검증한다(응답, 정상_처리);
assertThat(응답.header("Set-Cookie")).contains("Max-Age=0");
}
}

@Nested
class 내_정보_조회_API {

Expand Down

0 comments on commit eb6ae3f

Please sign in to comment.