theodorejb / es-cookie

A simple, lightweight module for handling cookies
MIT License
45 stars 2 forks source link

Cookies not saving in http dev environment #13

Open BigBallard opened 1 week ago

BigBallard commented 1 week ago

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)
    },
...
theodorejb commented 5 days ago

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

BigBallard commented 5 days ago

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?

theodorejb commented 5 days ago

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

BigBallard commented 1 day ago

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

theodorejb commented 1 day ago

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.