분명히 응답헤더에는 set-cookies가 잘찍히는데 브라우저 쿠키에 자동으로 저장이 안된다면?
원인은 sameSite였다. 명시하지않더라도 크롬정책에 의하여 기본값으로 세팅되어있다고한다.
해결방법은 서버에서 쿠키 구울때 sameSite:none랑 secure옵션을 추가해보자.
단 이렇게될경우 https에서만 작동된다. http 즉 로컬(http://localhost:3000/)로 개발할때는 크롬에서 막아 저장되지않으니 참고.
router.post(
"/",
async (res, req) => {
const jwt = 'test'
const cookieOptions = {
// sameSite: "strict",
sameSite: "none",
httpOnly: true,
secure: true,
// domain: "http://localhost",
// expires: new Date(Date.now() + parseInt(ENV.AUTH_EXPIREATION)),
maxAge: 24 * 60 * 60 * 1000,
path: "/",
};
res.cookie("x_auth", jwt, cookieOptions);
return res.status(200).json({
message: "jwt token issued",
jwt_token,
});
})
그래도 안되면 혹시 쿠키저장을 크롬에서 막고있지않은지 settings을 살펴본다.
localhost에서도 크롬에서 쿠키를 확인하고싶다면 cloudflare에서 터널링을 뚫어서하는 방법도있다고하는데 좀더 찾아봐야겠다.
참고
브라우저 쿠키와 SameSite 속성 / seob.dev
브라우저 쿠키에 대한 기본적인 내용들, 그리고 웹 개발자들에게 중요한 "SameSite" 속성을 다룹니다. "SameSite" 속성이 어떤 속성인지, 각 브라우저에서 어떻게 동작하고 있는지 알아봅니다.
seob.dev
https://www.chromium.org/updates/same-site/
SameSite Updates
SameSite Updates Launch Timeline Last updated Mar 18, 2021. Latest update: Mar 18, 2021: The flags #same-site-by-default-cookies and #cookies-without-same-site-must-be-secure have been removed from chrome://flags as of Chrome 91, as the behavior is now ena
www.chromium.org
https://developers.google.com/search/blog/2020/01/get-ready-for-new-samesitenone-secure?hl=ko
새로운 SameSite=None; Secure 쿠키 설정에 대비 | Google 검색 센터 블로그 | Google for Developers
developers.google.com
'Network&etc > HTTP' 카테고리의 다른 글
[400에러] Unexpected token o in JSON at position 1 error (0) | 2022.09.15 |
---|---|
url 특수문자 처리 encodeURIComponent <-> decodeURIComponent (0) | 2022.08.04 |
Rest API 사용시 Caching 주의 (0) | 2022.07.14 |
cors 에러와의 만남 (0) | 2022.06.21 |
Failed to load resource: the server responded with a status of 404 (Not Found) (0) | 2022.06.21 |