Skip to content

Commit

Permalink
refactor(AttendeeController): HttpServletResponse 대신 ResponseEntity가 …
Browse files Browse the repository at this point in the history
…제공하는 API를 사용하여 쿠키 추가 로직 개선
  • Loading branch information
seokmyungham committed Aug 1, 2024
1 parent 0df9663 commit 11d67fc
Showing 1 changed file with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package kr.momo.controller.attendee;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import kr.momo.service.attendee.AttendeeService;
import kr.momo.service.attendee.dto.AttendeeLoginRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -20,21 +21,21 @@ public class AttendeeController {
private final AttendeeService attendeeService;

@PostMapping("/api/v1/meetings/{uuid}/login")
public void login(
@PathVariable String uuid, @RequestBody @Valid AttendeeLoginRequest request, HttpServletResponse response
) {
public ResponseEntity<Void> login(@PathVariable String uuid, @RequestBody @Valid AttendeeLoginRequest request) {
String token = attendeeService.login(uuid, request);
String path = String.format("/api/v1/meetings/%s/", uuid);

Cookie cookie = createCookie(token, path);
response.addCookie(cookie);
return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, createCookie(token, path))
.build();
}

private Cookie createCookie(String value, String path) {
Cookie cookie = new Cookie(ACCESS_TOKEN, value);
cookie.setHttpOnly(true);
cookie.setSecure(true);
cookie.setPath(path);
return cookie;
private String createCookie(String value, String path) {
return ResponseCookie.from(ACCESS_TOKEN, value)
.httpOnly(true)
.secure(true)
.path(path)
.build()
.toString();
}
}

0 comments on commit 11d67fc

Please sign in to comment.