Skip to content

Commit 524a56c

Browse files
committed
feat: 로그아웃 시 자동 로그인 기능 해제되도록 구현
1 parent 75a88db commit 524a56c

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

src/main/java/com/example/busan/auth/AuthController.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.example.busan.auth.dto.LoginRequest;
88
import com.example.busan.auth.service.AuthService;
99
import com.example.busan.auth.service.PhoneAuthenticator;
10+
import jakarta.servlet.http.HttpServletRequest;
1011
import jakarta.servlet.http.HttpServletResponse;
1112
import jakarta.servlet.http.HttpSession;
1213
import org.springframework.http.ResponseEntity;
@@ -50,8 +51,11 @@ public ResponseEntity<Void> login(@RequestBody final LoginRequest request,
5051
}
5152

5253
@PostMapping("/logout")
53-
public ResponseEntity<Void> logout(final HttpSession session) {
54+
public ResponseEntity<Void> logout(final HttpSession session,
55+
final HttpServletRequest request,
56+
final HttpServletResponse response) {
5457
session.removeAttribute(AUTHORIZATION);
58+
autoLoginManager.removeAutoLogin(request, response);
5559
return ResponseEntity.noContent().build();
5660
}
5761

src/main/java/com/example/busan/auth/domain/AutoLoginManager.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public void setAutoCookie(final HttpSession httpSession,
4545

4646
public Authentication getAuthentication(final HttpServletRequest request) {
4747
final Cookie autoLoggedInCookie = getAutoLoggedInCookie(request);
48+
if (autoLoggedInCookie == null) {
49+
throw new UnauthorizedException();
50+
}
4851

4952
final String id = autoLoggedInCookie.getValue();
5053
return autoLoginRepository.findById(id)
@@ -56,15 +59,19 @@ private Cookie getAutoLoggedInCookie(final HttpServletRequest request) {
5659
return Arrays.stream(request.getCookies())
5760
.filter(cookie -> cookie.getName().equals(AUTO_LOGIN_COOKIE_NAME))
5861
.findAny()
59-
.orElseThrow(UnauthorizedException::new);
62+
.orElse(null);
6063
}
6164

6265
public void removeAutoLogin(final HttpServletRequest request,
6366
final HttpServletResponse response) {
6467
final Cookie autoLoggedInCookie = getAutoLoggedInCookie(request);
65-
autoLoggedInCookie.setMaxAge(0);
66-
response.addCookie(autoLoggedInCookie);
6768

68-
autoLoginRepository.deleteById(autoLoggedInCookie.getValue());
69+
final Cookie cookie = new Cookie(AUTO_LOGIN_COOKIE_NAME, "");
70+
cookie.setMaxAge(0);
71+
response.addCookie(cookie);
72+
73+
if (autoLoggedInCookie != null) {
74+
autoLoginRepository.deleteById(autoLoggedInCookie.getValue());
75+
}
6976
}
7077
}

src/main/resources/static/api/openapi3.yaml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ paths:
9393
content:
9494
application/json:
9595
schema:
96-
$ref: '#/components/schemas/auth-login-74814376'
96+
$ref: '#/components/schemas/auth-login1098228858'
9797
examples:
9898
로그인 하기:
99-
value: "{\"email\":\"[email protected]\",\"password\":\"password\"}"
99+
value: "{\"email\":\"[email protected]\",\"password\":\"password\",\"\
100+
isAuto\":true}"
100101
responses:
101102
"204":
102103
description: "204"
@@ -142,7 +143,7 @@ paths:
142143
get:
143144
tags:
144145
- members
145-
operationId: 현재 유저 정보 조회하기
146+
operationId: 자동 로그인 하기현재 유저 정보 조회하기
146147
responses:
147148
"200":
148149
description: "200"
@@ -154,7 +155,11 @@ paths:
154155
현재 유저 정보 조회하기:
155156
value: "{\"name\":\"연어\",\"phone\":\"01012341234\",\"email\":\"\
156157
[email protected]\",\"role\":\"USER\",\"company\":\"우형\",\"region\"\
157-
:\"BUSAN\",\"createdAt\":\"2023-12-22T21:27:18.831673\"}"
158+
:\"BUSAN\",\"createdAt\":\"2024-01-03T17:58:33.227632\"}"
159+
자동 로그인 하기:
160+
value: "{\"name\":\"연어\",\"phone\":\"01012341234\",\"email\":\"\
161+
[email protected]\",\"role\":\"USER\",\"company\":\"우형\",\"region\"\
162+
:\"BUSAN\",\"createdAt\":\"2024-01-03T17:33:27.690919\"}"
158163
post:
159164
tags:
160165
- members
@@ -281,14 +286,14 @@ paths:
281286
examples:
282287
자신의 회의실 예약 목록 최신 순으로 보기:
283288
value: "[{\"id\":1,\"status\":\"RESERVED\",\"cancelReason\":null,\"\
284-
startTime\":\"2023-12-22T21:27:20.516436\",\"endTime\":\"2023-12-22T23:27:20.51644\"\
289+
startTime\":\"2024-01-03T17:58:34.971705\",\"endTime\":\"2024-01-03T19:58:34.97171\"\
285290
,\"name\":\"황재현\",\"phone\":\"01012341234\",\"reservedAt\":\"\
286-
2023-12-22T21:27:20.51645\",\"roomId\":1,\"roomName\":\"대회의실\"\
291+
2024-01-03T17:58:34.971719\",\"roomId\":1,\"roomName\":\"대회의실\"\
287292
},{\"id\":2,\"status\":\"CANCELED\",\"cancelReason\":\"쓰기 싫어졌어\
288-
요..\",\"startTime\":\"2023-12-22T21:27:20.516458\",\"endTime\"\
289-
:\"2023-12-22T23:27:20.51646\",\"name\":\"황재현\",\"phone\":\"01012341234\"\
290-
,\"reservedAt\":\"2023-12-22T21:27:20.516462\",\"roomId\":1,\"\
291-
roomName\":\"대회의실\"}]"
293+
요..\",\"startTime\":\"2024-01-03T17:58:34.971727\",\"endTime\"\
294+
:\"2024-01-03T19:58:34.971728\",\"name\":\"황재현\",\"phone\":\"\
295+
01012341234\",\"reservedAt\":\"2024-01-03T17:58:34.97173\",\"\
296+
roomId\":1,\"roomName\":\"대회의실\"}]"
292297
post:
293298
tags:
294299
- reservations
@@ -512,12 +517,15 @@ components:
512517
status:
513518
type: string
514519
description: 예약 상태
515-
auth-login-74814376:
520+
auth-login1098228858:
516521
type: object
517522
properties:
518523
password:
519524
type: string
520525
description: 비밀번호
526+
isAuto:
527+
type: boolean
528+
description: 자동 로그인 여부
521529
email:
522530
type: string
523531
description: 이메일

src/test/java/com/example/busan/auth/AuthControllerTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.example.busan.auth;
22

33
import com.example.busan.ApiTest;
4+
import com.example.busan.auth.domain.AutoLoginManager;
45
import com.example.busan.auth.dto.AuthenticatePhoneRequest;
56
import com.example.busan.auth.dto.Authentication;
67
import com.example.busan.auth.dto.FindEmailResponse;
78
import com.example.busan.auth.dto.LoginRequest;
89
import com.example.busan.auth.service.AuthService;
910
import com.example.busan.auth.service.PhoneAuthenticator;
1011
import com.example.busan.member.domain.Role;
12+
import jakarta.servlet.http.Cookie;
1113
import org.junit.jupiter.api.DisplayName;
1214
import org.junit.jupiter.api.Test;
1315
import org.springframework.boot.test.mock.mockito.MockBean;
@@ -80,7 +82,9 @@ void logout() throws Exception {
8082

8183
//when
8284
final MockHttpServletResponse response = mockMvc.perform(
83-
post("/auth/logout").session(httpSession))
85+
post("/auth/logout")
86+
.session(httpSession)
87+
.cookie(new Cookie(AutoLoginManager.AUTO_LOGIN_COOKIE_NAME, "")))
8488
.andDo(print())
8589
.andDo(document("로그아웃 하기"))
8690
.andReturn()

0 commit comments

Comments
 (0)