Cookies not saving in http dev environment
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)
},
...
What is the name and value of the cookie you are trying to save?
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?
Does it work with a simpler value instead of a stringified object?
No it does not. even changing the key too foo and value to bar does anything. still no saved cookie.
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.