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](https://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: https://github.com/coderpawan/E-commerce