I’m implementing authentication in a Node.js application by setting JWT tokens in cookies. Although the API request returns a status code of 200, the cookies are not being stored in the browser. Additionally, an error message states, “This attempt to set a cookie via the Set-Cookie header was blocked because its domain attribute was invalid with regards to the current host URL.” Furthermore, there is also a 307 Temporary Redirect error shown in the screenshots.
here is api code for authentication
try {
const { email, password } = req.body;
if (!email || !password) {
res.status(400);
throw new Error("All fields are required");
}
const admin = await SuperAdmin.find({ email });
if (admin.length === 0) {
res.status(400);
throw new Error("Invalid email or password");
}
if (admin && (await bcrypt.compare(password, admin[0].password))) {
const accessToken = jwt.sign(
{
admin: {
_id: admin[0]._id,
},
},
process.env.JWT_KEY,
{ expiresIn: "1d" }
);
res.cookie("token", accessToken, {
httpOnly: true,
domain: "localhost",
secure: true,
sameSite: "Lax",
maxAge: 3 * 24 * 60 * 100,
});
res.status(200).json({ message: "login successful" });
} else {
res.status(400);
throw new Error("email or password is not valid");
}
} catch (error) {
res.status(400).json({ message: error.message });
}