0

On my server.js (main launch file) I'm using express and cookie-parser for cookies.

const cookieParser = require('cookie-parser');
.....
app.use(cookieParser());

And on my router file I'm setting the cookie

router.get('/auth', function (req, res, next) {
    res.cookie('lcuser', crypto.randomBytes(32).toString('hex').toUpperCase(), {
        domain: config.domain,
        expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
        httpOnly: true
    });
    res.writeHead(302, {Location: config.domain});
    res.end();
});

but when going trough the url http://localhost:1110/auth it does redirect to main page but doesn't set the cookie. Where the problem could be?

Arnas Pecelis
  • 149
  • 1
  • 14

2 Answers2

0

When I look at the API Doc i see no function cookie()

https://github.com/expressjs/cookie-parser

b3nc1
  • 106
  • 7
  • thats why cookie-parser is being used there with app - [more here](https://stackoverflow.com/a/44894355/9521977) and [here](https://expressjs.com/en/4x/api.html#res.cookie) – Arnas Pecelis Apr 18 '18 at 19:48
0

Well dunno why this didn't worked but I tried old method and it works..

res.writeHead(302, {'Content-Type': 'text/plain', 'Location': '/', 'Set-Cookie': 'lcuser=' + crypto.randomBytes(32).toString('hex').toUpperCase()});
res.end();
Arnas Pecelis
  • 149
  • 1
  • 14