I sent a jwt token as cookies after login. It is visible in the cookies section of the browser but when I make network requests, the cookies is not added to the request as such I’m unable to access protected routes from my frontend. But when I use something like Postman or insomnia, I’m able to access the cookies and access protected routes
My backend login
const login = async (req, res, next) => {
let { email, password } = req.body;
email = email.trim();
if (!email || !password)
return next(errorHandler(400, "All fields are required"));
if (email === "" || password.trim() === "")
return next(errorHandler(400, "No field can be left blank"));
try {
const user = await UserSchema.findOne({ email });
if (!user)
return next(
errorHandler(
404,
"Incorrect username or password, would you like to sign up instead?"
)
);
if (user.fromGoogle)
return next(
errorHandler(403, "You already signed in with a different method")
);
const checkPassword = bcrypt.compareSync(password, user.password);
if (!checkPassword) return next(errorHandler(400, "Wrong password"));
const accessToken = jwt.sign(
{ id: user._id, fromGoogle: user.fromGoogle, accType: user.accountType },
process.env.JWT_SECRET
);
const { _id, ...others } = user._doc;
res
.cookie("access_token", accessToken, {
httpOnly: true,
secure: true,
sameSite: "none",
})
.status(200)
.json(_id);
} catch (error) {
return next(error);
}
};
My React frontend
const submitHandler = async (e) => {
e.preventDefault();
if (!email || !password || email.trim() === "" || password.trim() === "") {
dispatch(loginFailure(null));
return setInputError("All fields are required");
}
dispatch(loginStart());
try {
const res = await backendConnection.post("/auth/login", inputValue, {
withCredentials: true,
credentials: "include",
});
dispatch(loginSuccess());
localStorage.setItem("clientId", JSON.stringify(res.data));
window.location.href = "/";
} catch (error) {
dispatch(
loginFailure(
error.response.data
? error.response.data.message
: "Something went wrong"
)
);
}
};
Please I’ll be very glad if someone helps me out