Home > Blockchain >  Can't set cookie with Node.js Express
Can't set cookie with Node.js Express

Time:09-22

can't set cookie from my ayth controller, can't understand why. If i set from folder app.js everything fine.

const createSendToken = (user, statusCode, res) => {
  const token = signToken(user._id);
  const cookieOptions = {
    expires: new Date(Date.now()   process.env.JWT_COOKIE_EXPIRES_IN * 24 * 60 * 60 * 1000),
    secure: false,
  };
  if (process.env.NODE_ENV === 'production') cookieOptions.secure = true;
  user.password = undefined;

  res.cookie('jwt', token, cookieOptions);

  res.status(statusCode).json({
    status: 'success',
    token,
    data: {
      user,
    },
  });
};

CodePudding user response:

Have you used cookie-parser ? Because As of now the cookie parser is a separate npm package, so instead of using

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

CodePudding user response:

That resolve my problem

const cookieOptions = {
  expires: new Date(Date.now()   process.env.JWT_COOKIE_EXPIRES_IN * 24 * 60 * 60 * 1000),
  secure: true,
  sameSite: 'none',
};
  • Related