Safari不会保存Cookie,但Chrome会保存 [英] Safari isn't saving cookies, but Chrome is
问题描述
更新:值得一提的是,我的网站是通过iframe加载的。
UPDATE: It's worth mentioning, my website is being loaded via an iframe.
这是我app.js中的cookieSession:
Here's my cookieSession in my app.js:
app.use(cookieParser());
app.use(cookieSession({
secret: "SECRET_SIGNING_KEY",
maxAge: 15724800000
}));
然后我尝试设置用户和登录时的令牌。
I then try to set the user, and token when logging in.
app.post('/login', function(req, res){
Parse.User.logIn(req.body.username, req.body.password).then(function(user) {
req.session.user = user;
req.session.token = user.getSessionToken();
res.redirect('/dashboard');
}, function(error) {
console.log(error)
req.session = null;
res.render('login');
});
});
此功能在Chrome浏览器中有效,但在Safari中不起作用。
This works in Chrome but it doesn't work in Safari.
我已经通过网络控制台检查了Safari存储,在我的域下没有任何保存。
I've checked the Safari storage via the web console and under my domain there is nothing being saved.
为什么不起作用?
推荐答案
您似乎在此处遇到了Safari错误( https://bugs.webkit.org/show_bug.cgi?id=3512 );
您正在同时设置cookie的同时将任何访问的浏览器重定向到/ dashboard,并且Safari在遇到302(或301我这样)的HTTP状态时会忽略Set-Cookie标头。
在这种情况下,您需要将用户令牌
保留在变量中,并将其放入 / dashboard
控制器,然后重新检查并将用户令牌
存储在 req.session
中。
It looks like you hit a Safari bug here (https://bugs.webkit.org/show_bug.cgi?id=3512);
You are redirecting any visiting browser to /dashboard while setting the cookie at the same time, and Safari is ignoring the Set-Cookie header when encountering the 302( or 301 I thing so) HTTP status.
In this case you need keep user token
in a variable an put it to /dashboard
controller then recheck and store user token
in req.session
.
- 已更新:将您的令牌(由
登录
控制器生成)发布到dashboard
控制器
- Updated: Post your token (genereted by
login
controller) todashboard
controller
app.post('/login', function(req, res) {
Parse.User.logIn(req.body.username, req.body.password).then(function(user) {
res.redirect('/dashboard?token=' + user.getSessionToken() + '&user=' + user);
}, function(error) {
console.log(error)
req.session = null;
res.render('login');
});
});
app.post('/dashboard', function(req, res) {
if (req.query.user && req.query.token) {
// save for first time
req.session.user = req.query.user;
req.session.token = req.query.user;
}
// check token in session before go
});
这篇关于Safari不会保存Cookie,但Chrome会保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!