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

Cookies not saving in http dev environment #13

Open
BigBallard opened this issue Sep 20, 2024 · 5 comments
Open

Cookies not saving in http dev environment #13

BigBallard opened this issue Sep 20, 2024 · 5 comments

Comments

@BigBallard
Copy link

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
Copy link
Owner

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

@BigBallard
Copy link
Author

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
Copy link
Owner

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

@BigBallard
Copy link
Author

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

@theodorejb
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants