[英]Making a cookie available to a sibling domain with Ember
In an Ember app, we set cookies using ember-cookie
.在 Ember 应用程序中,我们使用
ember-cookie
设置 cookies 。 At the moment, the userToken
cookie is available to www.example.com
only.目前,
userToken
cookie 仅对www.example.com
可用。 I need to make it available to api.example.com
as well and expected below code to work: Even if domain
(which is actually set through an environment variable) starts with "www", the cookie should be set one level higher (and thus be available to the api
subdomain as well).我需要将它提供给
api.example.com
并且期望下面的代码可以工作:即使domain
(实际上是通过环境变量设置)以“www”开头,cookie 也应该设置更高一级(并且因此也可用于api
子域)。
set cookieToken(token) {
let domain = 'www.example.com';
// Share with main domain (and API subdomain) even if host is www subdomain
if (domain.startsWith('www')) {
const apiSubdomain = domain.replace(/^www/, '');
this.cookies.write('userToken', JSON.stringify(token), { path: '/', apiSubdomain })
}
}
This approach failed, however:然而,这种方法失败了:
I also tried setting the cookie for api.example.com
explicitly:我还尝试为
api.example.com
明确设置 cookie:
if (domain.startsWith('www')) {
const apiSubdomain = domain.replace(/^www/, 'api');
this.cookies.write('userToken', JSON.stringify(token), { path: '/', apiSubdomain })
}
The outcome was the same:结果是一样的:
What can I do to make this cookie accesible for both subdomains?我该怎么做才能使两个子域都可以访问此 cookie?
The problem was that I didn't specify the domain
key in the options hash.问题是我没有在选项 hash 中指定
domain
密钥。 The following code would have worked because domain
is equivalent to domain: domain
:以下代码会起作用,因为
domain
等效于domain: domain
:
this.cookies.write('userToken', JSON.stringify(token), { path: '/', domain })
In my case, however, I named the variable differently, so I need to specify the key explicitly:然而,在我的例子中,我以不同的方式命名了变量,所以我需要明确指定键:
this.cookies.write('userToken', JSON.stringify(token), { path: '/', domain: apiSubdomain })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.