#typescript #next-js #tailwindcss

로그인 시 Cookie 처리

Oct 10, 2022


로그인 요청 후 response 쿠기에 정상적으로 jwt 토큰이 담겨 오는 것을 확인했다. 그러나 개발자도구 > Application 탭 내 jwt 토근이 저장되지 않는다.

이유는 Server에서 로그인 후 response 객체에 쿠키를 Set 하는 과정에서 옵션넣어줘야 한다. reddit-clone-app\server\src\routes\auth.ts 파일에 아래 내용을 추가한다.

...생략
const login = async (req : Request, res: Response) => {
...생략
        // 쿠키 저장
        // var setCookie = cookie.selialize('foo', 'bar');
        res.set(
            "Set-Cookie",
            cookie.serialize("token", token, {
                httpOnly: true,
                secure: process.env.NODE_ENV === "development",
                sameSite: "strict",
                maxAge: 60 * 60 * 24 * 7, // 1week
                path: "/"
            })
        );
...생략
}
...생략

옵션 설명 참조: https://ko.javascript.info/cookie



*****

© 2021, Ritij Jain | Pudhina Fresh theme for Jekyll.