I can set the cookie when the app is running on localhost but when i deploy it on vercel the cookie is not set #mern stack

Below is the code for the generateToken function which gets called when the user tries to login

import jwt from “jsonwebtoken”;

const generateToken = (res, userId) => {
const token = jwt.sign({ userId }, process.env.JWT_SECRET, {
expiresIn: “30d”,
});

// Set JWT as an HTTP-Only Cookie
res.cookie(“jwt”, token, {
expires: new Date(
Date.now() + 30 * 24 * 60 * 60 * 1000
),
httpOnly: true,
secure:true,
sameSite: “none”
});

return token;
};

export default generateToken;

Login/Logout Controller function

const loginUser = asyncHandler(async (req, res) => {
const { email, password } = req.body;

const existingUser = await User.findOne({ email });

if (existingUser) {
const isPasswordValid = await bcrypt.compare(
password,
existingUser.password
);

if (isPasswordValid) {
  createToken(res, existingUser._id);

  res.status(201).json({
    _id: existingUser._id,
    username: existingUser.username,
    email: existingUser.email,
    isAdmin: existingUser.isAdmin,
  });
  return;
}

}
});

const logoutCurrentUser = asyncHandler(async (req, res) => {
res.cookie(“jwt”, “”, {
httyOnly: true,
expires: new Date(0),
});

res.status(200).json({ message: “Logged out successfully” });
});

Please help me out with this error
Github Repo: GitHub - coderpawan/E-commerce

1 Like

I am having the same problem since Friday.

I have a app that logs in using supabase auth, everything was working fine before and still works fine on localhost. Now on every new deploy the login works but the cookies are not persisted so that any navigation will redirect back to the login page again.

I have reverted prod to the previous deploy, which still works. But if I try to deploy the same commit it does not work. I think something changed internally in vercel to mess things up.

But if the things have internally changed it would mishap the whole thing because we can’t let users to pay or checkout before authorisation. How to handle this I cant think anymore

I was debugging this all day, seems like vercel injected some middleware the messes up with the cookies. Its the first time i need their support but no one replies and they replaced chat support with a useless gpt.

There is an other ticket about the exact same problem => Next auth api route throwing Error: Failed to collect page data for /api/auth/[...nextauth] only on production

We are experiencing this problem. We are not able to use server actions in Nextjs and Supabase.

1 Like

The problem on my end was coz the started all prefetching including prefetching the logout button, that it was the same logic since forever but vercel started default prefetch to true.

Looks like you’re good now, @3den! :smile:

Let’s keep your conversation in one place: Next auth api route throwing Error: Failed to collect page data for /api/auth/[…nextauth] only on production :slight_smile:

Hi @coderpawan!

Are you still experiencing this issue?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.