Skip to content

Commit

Permalink
feat: 로그아웃 시 자동 로그인 기능 해제되도록 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Jan 3, 2024
1 parent 75a88db commit 524a56c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/example/busan/auth/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.example.busan.auth.dto.LoginRequest;
import com.example.busan.auth.service.AuthService;
import com.example.busan.auth.service.PhoneAuthenticator;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -50,8 +51,11 @@ public ResponseEntity<Void> login(@RequestBody final LoginRequest request,
}

@PostMapping("/logout")
public ResponseEntity<Void> logout(final HttpSession session) {
public ResponseEntity<Void> logout(final HttpSession session,
final HttpServletRequest request,
final HttpServletResponse response) {
session.removeAttribute(AUTHORIZATION);
autoLoginManager.removeAutoLogin(request, response);
return ResponseEntity.noContent().build();
}

Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/example/busan/auth/domain/AutoLoginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public void setAutoCookie(final HttpSession httpSession,

public Authentication getAuthentication(final HttpServletRequest request) {
final Cookie autoLoggedInCookie = getAutoLoggedInCookie(request);
if (autoLoggedInCookie == null) {
throw new UnauthorizedException();
}

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

public void removeAutoLogin(final HttpServletRequest request,
final HttpServletResponse response) {
final Cookie autoLoggedInCookie = getAutoLoggedInCookie(request);
autoLoggedInCookie.setMaxAge(0);
response.addCookie(autoLoggedInCookie);

autoLoginRepository.deleteById(autoLoggedInCookie.getValue());
final Cookie cookie = new Cookie(AUTO_LOGIN_COOKIE_NAME, "");
cookie.setMaxAge(0);
response.addCookie(cookie);

if (autoLoggedInCookie != null) {
autoLoginRepository.deleteById(autoLoggedInCookie.getValue());
}
}
}
30 changes: 19 additions & 11 deletions src/main/resources/static/api/openapi3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/auth-login-74814376'
$ref: '#/components/schemas/auth-login1098228858'
examples:
로그인 하기:
value: "{\"email\":\"[email protected]\",\"password\":\"password\"}"
value: "{\"email\":\"[email protected]\",\"password\":\"password\",\"\
isAuto\":true}"
responses:
"204":
description: "204"
Expand Down Expand Up @@ -142,7 +143,7 @@ paths:
get:
tags:
- members
operationId: 현재 유저 정보 조회하기
operationId: 자동 로그인 하기현재 유저 정보 조회하기
responses:
"200":
description: "200"
Expand All @@ -154,7 +155,11 @@ paths:
현재 유저 정보 조회하기:
value: "{\"name\":\"연어\",\"phone\":\"01012341234\",\"email\":\"\
[email protected]\",\"role\":\"USER\",\"company\":\"우형\",\"region\"\
:\"BUSAN\",\"createdAt\":\"2023-12-22T21:27:18.831673\"}"
:\"BUSAN\",\"createdAt\":\"2024-01-03T17:58:33.227632\"}"
자동 로그인 하기:
value: "{\"name\":\"연어\",\"phone\":\"01012341234\",\"email\":\"\
[email protected]\",\"role\":\"USER\",\"company\":\"우형\",\"region\"\
:\"BUSAN\",\"createdAt\":\"2024-01-03T17:33:27.690919\"}"
post:
tags:
- members
Expand Down Expand Up @@ -281,14 +286,14 @@ paths:
examples:
자신의 회의실 예약 목록 최신 순으로 보기:
value: "[{\"id\":1,\"status\":\"RESERVED\",\"cancelReason\":null,\"\
startTime\":\"2023-12-22T21:27:20.516436\",\"endTime\":\"2023-12-22T23:27:20.51644\"\
startTime\":\"2024-01-03T17:58:34.971705\",\"endTime\":\"2024-01-03T19:58:34.97171\"\
,\"name\":\"황재현\",\"phone\":\"01012341234\",\"reservedAt\":\"\
2023-12-22T21:27:20.51645\",\"roomId\":1,\"roomName\":\"대회의실\"\
2024-01-03T17:58:34.971719\",\"roomId\":1,\"roomName\":\"대회의실\"\
},{\"id\":2,\"status\":\"CANCELED\",\"cancelReason\":\"쓰기 싫어졌어\
요..\",\"startTime\":\"2023-12-22T21:27:20.516458\",\"endTime\"\
:\"2023-12-22T23:27:20.51646\",\"name\":\"황재현\",\"phone\":\"01012341234\"\
,\"reservedAt\":\"2023-12-22T21:27:20.516462\",\"roomId\":1,\"\
roomName\":\"대회의실\"}]"
요..\",\"startTime\":\"2024-01-03T17:58:34.971727\",\"endTime\"\
:\"2024-01-03T19:58:34.971728\",\"name\":\"황재현\",\"phone\":\"\
01012341234\",\"reservedAt\":\"2024-01-03T17:58:34.97173\",\"\
roomId\":1,\"roomName\":\"대회의실\"}]"
post:
tags:
- reservations
Expand Down Expand Up @@ -512,12 +517,15 @@ components:
status:
type: string
description: 예약 상태
auth-login-74814376:
auth-login1098228858:
type: object
properties:
password:
type: string
description: 비밀번호
isAuto:
type: boolean
description: 자동 로그인 여부
email:
type: string
description: 이메일
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/com/example/busan/auth/AuthControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.example.busan.auth;

import com.example.busan.ApiTest;
import com.example.busan.auth.domain.AutoLoginManager;
import com.example.busan.auth.dto.AuthenticatePhoneRequest;
import com.example.busan.auth.dto.Authentication;
import com.example.busan.auth.dto.FindEmailResponse;
import com.example.busan.auth.dto.LoginRequest;
import com.example.busan.auth.service.AuthService;
import com.example.busan.auth.service.PhoneAuthenticator;
import com.example.busan.member.domain.Role;
import jakarta.servlet.http.Cookie;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
Expand Down Expand Up @@ -80,7 +82,9 @@ void logout() throws Exception {

//when
final MockHttpServletResponse response = mockMvc.perform(
post("/auth/logout").session(httpSession))
post("/auth/logout")
.session(httpSession)
.cookie(new Cookie(AutoLoginManager.AUTO_LOGIN_COOKIE_NAME, "")))
.andDo(print())
.andDo(document("로그아웃 하기"))
.andReturn()
Expand Down

0 comments on commit 524a56c

Please sign in to comment.