Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 응답헤더로 cookie set (httponly, secure 적용) #71

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -46,9 +48,22 @@ public ResponseEntity<ApiResponse<String>> getToken(@RequestHeader String author
String userKakaoEmail = kakaoAuthService.getKakaoUserEmail(authorizationCode);
Optional<Member> existMember = memberService.findMemberByEmail(userKakaoEmail);
if (existMember.isPresent()) {
return ResponseEntity.ok(
ApiResponse.with(HttpStatus.OK, "로그인 성공", authService.getServiceToken(userKakaoEmail)));
String serviceToken = authService.getServiceToken(userKakaoEmail);

ResponseCookie cookie =
ResponseCookie.from("serviceToken", serviceToken)
.httpOnly(true)
.secure(true)
.path("/")
.sameSite("Strict")
.build();

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.SET_COOKIE, cookie.toString());

return ResponseEntity.ok()
.headers(headers)
.body(ApiResponse.with(HttpStatus.OK, "로그인 성공", serviceToken));
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else 는 빼고 좋을 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

움 그냥 넣을래요! 그게 분리되어 보여서 좋아요~

return ResponseEntity.status(HttpStatus.FOUND)
.location(
Expand All @@ -63,6 +78,20 @@ public ResponseEntity<ApiResponse<String>> signUpMember(
String serviceToken =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	@PostMapping("/signup")
	public ResponseEntity<ApiResponse<String>> signUpMember(@RequestParam String email, @RequestBody MemberInfoRequest memberInfoRequest)
여기서 email 정보만 RequestParam 으로 선언한 이유가 궁금해요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리다이렉트 페이지 url이랑 같이 보내는 거라서 param에 있는게 좋을 것 같아서요

authService.register(
email, memberInfoRequest.deliveryName(), memberInfoRequest.phoneNumber());
return ResponseEntity.ok(ApiResponse.with(HttpStatus.OK, "로그인 성공", serviceToken));

ResponseCookie cookie =
ResponseCookie.from("serviceToken", serviceToken)
.httpOnly(true)
.secure(true)
.path("/")
.sameSite("Strict")
.build();

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.SET_COOKIE, cookie.toString());

return ResponseEntity.ok()
.headers(headers)
.body(ApiResponse.with(HttpStatus.OK, "회원가입 성공", serviceToken));
}
}