how to get a cookie for with the same name for both `.domain.com` and `domain.com` js-cookie

huangapple go评论74阅读模式
英文:

how to get a cookie for with the same name for both `.domain.com` and `domain.com` js-cookie

问题

分析:
用户使用了js-cookie库并遇到了一个问题,即在存在同名但不同域的cookie时,如何获取具有域值.domain.com的cookie。用户尝试查看源代码,发现get函数似乎会创建一个以名称为索引的对象,因此如果存在同名但不同域的两个cookie,后者会覆盖前者。

用户提供了一个示例代码,但在尝试解决问题时遇到了困难。

想法:
用户想知道如何获取具有域值.domain.com的cookie,即使同名的domain.com的cookie也存在。用户已经尝试了一些方法,但仍然困惑。

推测:
可能需要查看js-cookie库的文档以获取有关如何正确处理具有不同域的同名cookie的信息。也许有一些特殊的选项或方法可以帮助解决这个问题。

汉化后的代码:(原文代码部分不翻译)

import Cookie from 'js-cookie';

// 这两个cookie中的一个将无法通过Cookie.get获取
Cookie.set('myCookie', 'myValue1', { domain: '.domain.com' });
Cookie.set('myCookie', 'myValue2', { domain: 'domain.com' });
英文:

I'm using the js-cookie library library and I can't figure out how to get the a cookie with a domain value of .domain.com if a cookie with same name exists for domain.com. I've looked at the source and the get function seems to create an object indexed by the name so it will be overridden if two cookies with the same name but different domains exist.

an example:


import Cookie from 'js-cookie'

// one of these two cookies will be unavailable via Cookie.get
Cookie.set('myCookie', 'myValue1', {domain: '.domain.com'})
Cookie.set('myCookie', 'myValue2', {domain: 'domain.com'})

I thought maybe the withAttributes function could help but I think that changes attributes when creating a cookie. I'm not totally sure though the docs are sparse there and i can't figure out the code.

答案1

得分: 1

分析:这段内容讨论了关于同名的不同Cookie在同时生效的问题。文档指出无法通过传递Cookie的属性之一来读取特定的Cookie,例如在写入Cookie时可能使用的属性:

Cookies.get('foo', { domain: 'sub.example.com' }) // `domain` won't have any effect

推测:名为foo的Cookie只有在代码调用的位置可见时才能在.get()中使用;在读取时,域名和/或路径属性将不会产生影响。

汉化后的代码:同名的不同Cookie不能同时生效。文档指出:

Cookies.get('foo', { domain: 'sub.example.com' }) // `domain`不会产生影响

这是库的限制,而不是浏览器仅使一个Cookie可见的限制。

英文:

You can't have multiple different cookies with the same name being in effect at the same time. The docs say

> Note: It is not possible to read a particular cookie by passing one of the cookie attributes (which may or may not have been used when writing the cookie in question):
>
> Cookies.get('foo', { domain: 'sub.example.com' }) // domain won't have any effect
>
> The cookie with the name foo will only be available on .get() if it's visible from where the code is called; the domain and/or path attribute will not have an effect when reading.

This is not a limitation of the library, it's the browser that makes only one cookie visible.

答案2

得分: -1

你可以按名称和域检索Cookie,如下所示:

import Cookie from 'js-cookie';

function getCookieByDomain(name, domain) {
  const cookies = Cookie.get();
  const matchingCookies = Object.entries(cookies).filter(([cookieName]) => cookieName === name);

  // 搜索具有指定域的Cookie
  const cookieWithDomain = matchingCookies.find(([cookieName, cookieValue]) => {
    const cookieOptions = Cookie.getJSON(cookieName, { raw: true });
    return cookieOptions.domain === domain;
  });

  if (cookieWithDomain) {
    const [cookieName, cookieValue] = cookieWithDomain;
    return { [cookieName]: cookieValue };
  }

  return null; // 未找到具有指定域的Cookie
}

// 用法:
const cookieForDomain = getCookieByDomain('myCookie', '.domain.com');
console.log(cookieForDomain); // 如果存在,则输出:{ myCookie: 'myValue1' },否则输出 null
英文:

you can retrieve the cookie by name and domain like so

import Cookie from 'js-cookie';

function getCookieByDomain(name, domain) {
  const cookies = Cookie.get();
  const matchingCookies = Object.entries(cookies).filter(([cookieName]) => cookieName === name);

  // Search for a cookie with the specified domain
  const cookieWithDomain = matchingCookies.find(([cookieName, cookieValue]) => {
    const cookieOptions = Cookie.getJSON(cookieName, { raw: true });
    return cookieOptions.domain === domain;
  });

  if (cookieWithDomain) {
    const [cookieName, cookieValue] = cookieWithDomain;
    return { [cookieName]: cookieValue };
  }

  return null; // Cookie not found with the specified domain
}

// Usage:
const cookieForDomain = getCookieByDomain('myCookie', '.domain.com');
console.log(cookieForDomain); // Outputs: { myCookie: 'myValue1' } if it exists, otherwise null

huangapple
  • 本文由 发表于 2023年7月28日 03:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定