I get cookie from Postman but not on Chrome

Viewed 1072

I am trying to set a cookie with name of auth, includes user's id signed with jwt. I dont really see the auth cookie on Chrome when I hit http://localhost:5000/ I only see these two;

chrome

But the thing is, when I hit the same route with Postman, I get auth cookie in the console. By the way I console the cookie with req.cookies

And there is my server.js;

import express from 'express';
import dotenv from 'dotenv';
import connectDB from './utils/db.js';
import { notFound, errorHandler } from './middleware/errorMiddleware.js';
const app = express();
dotenv.config();
import cookieParser from 'cookie-parser';
import userRoutes from './routes/userRoutes.js';
import cors from 'cors';

app.use(cors());

connectDB();
app.use(cookieParser());
app.use(express.json());

app.get('/', (req, res) => {
  res.send('hi');
  console.log(req.cookies);
});

app.use('/api/users', userRoutes);

app.use(notFound);
app.use(errorHandler);

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => console.log('Server is listening on PORT 5000'));

And here is my register function where I first set the cookie;

// desc     Register User
// route    /api/users/register
// access   Public
const register = asyncHandler(async (req, res) => {
  let { name, email, password } = req.body;

  const isUserExists = await User.findOne({ email });
  if (isUserExists) {
    res.status(400);
    throw new Error('Invalid e-mail or password');
  }

  const salt = await bcrypt.genSalt(10);
  password = await bcrypt.hash(password, salt);

  const user = await User.create({
    name,
    email,
    password,
  });

  const token = generateToken(user._id);
  res.cookie('auth', token, {
    httpOnly: false,
    maxAge: 1000 * 60 * 60 * 24 * 3, // 3 dayss
  });

  res.status(201);

  res.json({
    _id: user._id,
    name: user.name,
    email: user.email,
    isAdmin: user.isAdmin,
  });
});
1 Answers
Related