dimanche 16 septembre 2018

Cannot receive cookies on backend

Hello SO,

My server uses Node.js and Express. I have properly set up cookie-parser and body-parser and I can receive and send cookies most of the time.

So, my app is very basic actually.

  1. Login page is presented.
  2. Ajax request to seslogin with idToken and csrfToken.
  3. Backend receives this tokens and can read cookies from req.
  4. Creates session cookie and sends it to client.
  5. Client redirects to /profile with window.location.assign('profile')
  6. Normally backend reads session cookie, verify it and present a UI.

In my app.ts, I can receive cookies perfectly in this part. I'm calling seslogin with an Ajax request on my client script.

app.post('/seslogin', function (req, res) {
    // Get ID token and CSRF token.
    const idToken = req.body.idToken.toString();
    const csrfToken = req.body.csrfToken.toString();

    console.info('[SESLOGIN] Cookies: ', req.cookies);
    console.info('[SESLOGIN] Session Cookie: ', req.cookies.session);
    ...
    ...
});

However, very interestingly in this part cookies are undefined

app.get('/profile', function (req, res) {
    // Get session cookie.
    const sessionCookie = req.cookies.session || '';

    console.info('[PROFILE] Cookies: ', req.cookies);
    console.info('[PROFILE] Session Cookie: ', req.cookies.session);

    //Test purposes
    res.send(req.cookies).end();
    ...
});




Aucun commentaire:

Enregistrer un commentaire