Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

javascript - Safari isn't saving cookies, but Chrome is

UPDATE: It's worth mentioning, my website is being loaded via an iframe.

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');
    });

});

This works in Chrome but it doesn't work in Safari.

I've checked the Safari storage via the web console and under my domain there is nothing being saved.

Any reason why it's not working?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

  • Updated: Post your token (genereted by login controller) to dashboard 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

});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...