2

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?

Farhan Syed
  • 77
  • 1
  • 9
  • Possible duplicate of [Setting cross-domain cookies in Safari](https://stackoverflow.com/questions/408582/setting-cross-domain-cookies-in-safari) – styfle Jun 05 '17 at 20:59

2 Answers2

3

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

});
hoangdv
  • 15,138
  • 4
  • 27
  • 48
  • Won't this be a security bug if `User-A` visits `/login` and while being redirected, `User-B` happens to visit `/dashboard`? The global variable will be assigned to whoever happens to visit the page first after someone logs in. In this case, `User-B` will have `User-A`'s session. – styfle Jun 02 '17 at 13:53
  • `User-A` has a token string `tokenA` and set redirected will be to `res.redirect('/dashboard?token=tokenA');` in dasboard controller you can get all info of `User-A` – hoangdv Jun 06 '17 at 02:30
  • Looks good now. Also the link to the safari bug is very helpful, thanks – styfle Jun 06 '17 at 13:21
1

If you page is loaded in an iframe, the end user may choose to block Third Party Cookies which would prevent your website from writing the cookie.

See Safari Privacy Settings. for more info

Did you try to see if the cookie worked when the page was visited directly instead of an iframe?

styfle
  • 22,361
  • 27
  • 86
  • 128
  • Yes I tried directly and it works fine. The issue is I am required to use an iframe. I am building an app for Shopify Embedded SDK and they require to have it in an iframe. – Farhan Syed Jun 02 '17 at 07:40
  • Did you try changing your privacy settings to allow 3rd Party Cookies and see if that works? – styfle Jun 02 '17 at 14:57
  • Yeah I tried and it seems to work. I just find it an obstacle for users to do so. Unfortunately I am required by Shopify to use an iFrame to show my site as you can see [here](https://help.shopify.com/api/sdks/shopify-apps/embedded-app-sdk) – Farhan Syed Jun 02 '17 at 23:02
  • You might need to break out of the iframe, set the cookie, then redirect back to the original page (in this case, Shopify). Maybe [this](https://github.com/vitr/safari-cookie-in-iframe) will help. – styfle Jun 05 '17 at 16:40
  • This is also my issue when developing Shopify App that is embedded. – Andrien Pecson Feb 01 '21 at 05:31