0

I have a node.js app hosted on firebase. However where it is hosted should not matter too much here as I am simply trying to store some information in cookies in the /set-cookie path so it can be accessed in the /user path. Relevant code in index.ts here:

// create HTTP server
const app = express();

// use json form parser middleware
app.use(bodyParser.json());

// use query string parser middlware
app.use(bodyParser.urlencoded({ extended: true }));


// cookies
app.use(cookieParser());

app.use(cookieSession({
      name   : 'session'
    , keys   : ['abigsecret', 'anothersecret']
    , maxAge : 100 * 60 * 60 * 1000
}));


// setting the cookie here
app.get('/set-cookie', (req,res) => {
    res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });
    res.send('cookie set...');
}})



// And I would like to access it here
app.get('/user' , (req, res) => {
    console.log('cookies: ', req.cookies)
    res.render('pages/user')
});

Of course this is not working as intended. since on the console I do not see the cookie I set in /set-cookie, but rather some client side cookie that was passed to the server. What is the way to set cookies so that I can access it elsewhere?

What I am seeing on the console is this when I navigate to '/user` after '/get-cookie'

info: cookies:  { _ga: 'GA1.1.1192529393.1524186034',
  ss_cvr: '5407c247-08f6-4b6d-8b27-e623c7788eef|1523239108805|1524436640490|1524443386060|46' }
info: 

but I should be seeing some version of : 'rememberme', '1', correct?

xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
  • Are you navigating to /set-cookie prior to /user? Seems to work fine for me. – clockwatcher Jun 02 '18 at 01:53
  • @clockwatcher yeah. how did you access this bit: 'rememberme', '1'? with the code I provided? Mine does something different> could it be because mine is hosted on firebase? – xiaolingxiao Jun 02 '18 at 03:05

1 Answers1

1

The following is a full example that works for me.

server.js

// create HTTP server
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');
const pug = require('pug');
const app = express();

app.set('views', './views');
app.set('view engine', 'pug');

// use json form parser middleware
app.use(bodyParser.json());

// use query string parser middlware
app.use(bodyParser.urlencoded({ extended: true }));


// cookies
app.use(cookieParser());

app.use(cookieSession({
      name   : 'session'
    , keys   : ['abigsecret', 'anothersecret']
    , maxAge : 100 * 60 * 60 * 1000
}));


// setting the cookie here
app.get('/set-cookie', (req,res) => {
    res.setHeader('Cache-Control', 'private');
    res.cookie('__session', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });
    res.send('cookie set...');
});



// And I would like to access it here
app.get('/user' , (req, res) => {
    console.log(req.cookies);
    res.render('pages/user', {cookies: req.cookies})
});

app.listen(3000, () => { console.log('Listening on port 3000')});

Then views/pages/user.pug:

doctype html
html
  body
    h1 Rememberme => #{cookies.__session}

Navigating in a browser first to http://localhost:3000/set-cookie and then to http://localhost:3000/user works for me. The console.log(req.cookies) includes the remember me and passing it into the template also works.

xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
clockwatcher
  • 3,193
  • 13
  • 13
  • ok when I run the example above launched with node.js server using `app.listen`, then the cookie is set. in fact I can access the cookie from many node.js project instances. The problem is that I am actually using the firebase server with `exports.app = functions.https.onRequest(app)`, so I cannot set cookies using res.cookie for some reason, even though that is what is used in the doc here: https://firebase.google.com/docs/auth/admin/manage-cookies. Furthermore, when I deploy the site it also cannot set cookies in production. – xiaolingxiao Jun 02 '18 at 15:36
  • ok found the answer here: https://stackoverflow.com/questions/44929653/firebase-cloud-function-wont-store-cookie-named-other-than-session. I just need a line of code `res.setHeader('Cache-Control', 'private'); above the `res.cookie(...)` . – xiaolingxiao Jun 02 '18 at 15:40