es-cookie icon indicating copy to clipboard operation
es-cookie copied to clipboard

Cookies not saving in http dev environment

Open BigBallard opened this issue 1 year ago • 5 comments

In attempting to use this library, I am finding that my cookies are not being saved even when no errors are occurring. Frankly, I don't know if it's because I am in an http localhost environment. After attempting to save the cookie, I check the browser's dev tools -> Application -> Cookies -> http://localhost:3000, and I don't see any cookie.

I am using the current version of es-cookie.

AuthStore

export const AuthStore: AuthStore = {
    setAuthCookie(data: AuthCookie) {
        console.log(`Saving auth cookie ${JSON.stringify(data)}`)
        CookieStore.save(AUTH_KEY, JSON.stringify(data), {
            daysToLive: 5,
            cookieDomain: window.location.origin
        })
    },
    clearAuthCookie(): void {
        CookieStore.remove(key, {cookieDomain: window.location.origin})
    }
}
export const CookieStore: CookieStore = {
...
    save(key: string, value: any, options?: CookieStorageOptions): void {
        console.log(`Saving cookie: [${key}:${value}]`)
        let attrs: Cookies.CookieAttributes = {
            HttpOnly: false
        }
        if ('https:' === window.location.protocol) {
            console.log("Storing cookie as secure")
            attrs = {
                secure: true,
                sameSite: 'none'
            }
        }

        attrs.expires = options?.daysToLive ?? 1

        if(options?.cookieDomain) {
            console.log(`Cookie domain: ${options.cookieDomain}`)
            attrs.domain = options.cookieDomain
        }

        Cookies.set(key, JSON.stringify(value), attrs)
    },
...

BigBallard avatar Sep 20 '24 15:09 BigBallard

What is the name and value of the cookie you are trying to save?

theodorejb avatar Sep 22 '24 23:09 theodorejb

auth for name and the value is stringified json of a token value.> What is the name and value of the cookie you are trying to save?

BigBallard avatar Sep 23 '24 02:09 BigBallard

Does it work with a simpler value instead of a stringified object?

theodorejb avatar Sep 23 '24 02:09 theodorejb

No it does not. even changing the key too foo and value to bar does anything. still no saved cookie.

BigBallard avatar Sep 26 '24 23:09 BigBallard

Well, you will have to keep simplifying until you figure out where the issue is. E.g. try directly setting a cookie without all the CookieStore complexity. If that doesn't work, try setting a cookie directly with document.cookie = ... to ensure there isn't an issue there.

theodorejb avatar Sep 27 '24 00:09 theodorejb