Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

248
Vistas
Cookies won't set in browser but does in postman

I have searched around, but still can't get it to work. I followed this blog to set things up.

this is where I send my cookie (server):

//after user is has been authenticated
export const logInUser = async (req, res, next) => {
try {
  const token = getToken({ _id: req.user._id, username: req.user.username, email: req.user.email})
  const refreshToken = getRefreshToken({  _id: req.user._id, username: req.user.username, email: req.user.email})
  await User.findOne({ username: req.user.username}).then((user)=>{
      user.refreshToken.push({ refreshToken })
      user.save((error, user) => {
        if (error) {
          res.status(500).json(error)
        } else {
          //sending the cookie
          res.cookie("refreshToken", refreshToken, {httpOnly: true, secure: !dev, signed: true, maxAge: (60 * 60 * 24 * 30) * 1000, sameSite: "none"})
          res.send({ success: true, token,  _id: req.user._id, username: req.user.username, email: req.user.email})
        }
      })
    })
} catch (error) {
  next(error)
    res.status(500).json(error)
    console.log(error)
}
}

index.js (server):

const corsOptions ={
origin: true, 
credentials:true,
optionSuccessStatus:200
}
app.use(express.json());
app.use(helmet());
app.use(morgan("common"));
app.use(cookieParser(process.env.COOKIE_SECRET))
app.use(cors(corsOptions));
app.use(passport.initialize());

making the request on the client-side (using next.js):

function login(username, password) {
axios.post("http://localhost:5000/api/auth/login", {username, password}, {withCredentials: true, credentials: 'include'}).then((res)=>{
  setCurrentUser(res.data)
}).catch((error)=>{
  console.log(error)
})
}

Response in browser

Everything works perfectly in postman

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

IF you are trying to see the cookie in your browser devtools then check this answer https://stackoverflow.com/a/38604501/16091749.

If what you are trying to do is to access your cookie from your Next app, then you can't do that unless you disable the httpOnly flag, which is not recommended in this case because you are keeping sensitive data in this cookie, so if you disable this flag your cookie will be vulnerable to any malicious script that may be in contact with it. For more info about the cookies check this link https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies

about 4 years ago · Juan Pablo Isaza Denunciar

0

I finally solved this by getting rid of the sameSite: "none" attribute on the cookie in this line res.cookie("refreshToken", refreshToken, {httpOnly: true, secure: !dev, signed: true, maxAge: (60 * 60 * 24 * 30) * 1000, sameSite: "none"})

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda