I'm developping an app in mern stack using http only cookie request and while handling it in frontend it doesnt work but in postman it is working And here is the coode for generating token
import jwt from "jsonwebtoken";
const generateToken = (res, userId) => {
const token = jwt.sign({ userId }, process.env.JWT_SECRET, {
expiresIn: "10d",
});
res.cookie("jwt", token, {
httpOnly: true,
secure: true,
sameSite: "none",
maxAge: 10 * 24 * 60 * 60 * 1000,
});
};
export default generateToken;
and here is the routes
import express from "express";
import {
authUser,
registerUser,
logoutUser,
getUserProfile,
updateUserProfile,
} from "../controllers/userController.js";
import { protect } from "../middleware/authMiddleware.js";
const router = express.Router();
router.post("/auth", authUser);
router.post("/", registerUser);
router.post("/logout", logoutUser);
router
.route("/profile")
.get(protect, getUserProfile)
.put(protect, updateUserProfile);
export default router;
and here is the server
import express from "express";
import dotenv from "dotenv";
import cookieParser from "cookie-parser";
import connectToDb from "./config/connectToDb.js";
import userRoutes from "./routes/userRoutes.js";
import { notFound, errorHandler } from "./middleware/errorMiddleware.js";
import cors from "cors";
import helmet from "helmet";
dotenv.config();
const port = process.env.PORT || 5000;
connectToDb();
const app = express();
app.use(helmet());
app.use(cors({ credentials: true }));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use("/api/users", userRoutes);
app.get("/", (req, res) => {
res.send("Server is ready");
});
app.use(notFound);
app.use(errorHandler);
app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`);
});
and here is the api call in frontend with redux
import { apiSlice } from "./apiSlice";
// Update the endpoint URL to localhost:5000
const USERS_URL = "/api/users";
export const usersApiSlice = apiSlice.injectEndpoints({
endpoints: (builder) => ({
login: builder.mutation({
query: (data) => ({
url: `${USERS_URL}/auth`,
method: "POST",
body: data,
}),
}),
register: builder.mutation({
query: (data) => ({
url: `${USERS_URL}`,
method: "POST",
body: data,
}),
}),
logout: builder.mutation({
query: () => ({
url: `${USERS_URL}/logout`,
method: "POST",
}),
}),
updateUser: builder.mutation({
query: (data) => ({
url: `${USERS_URL}/profile`,
method: "PUT",
body: data,
}),
}),
}),
});
export const {
useLoginMutation,
useLogoutMutation,
useRegisterMutation,
useUpdateUserMutation,
} = usersApiSlice;
i ve tried to change to change the credentials in server.js and also and changer secure to true and the also changer the sameSite to none and problem in exsisting