I have this block of code and when a user login, it should set the "access_token" cookie to a token, but when i check the browser application cookie, i see nothing and user is unauthenticated.
const login = async (req, res) => {
try {
const oneUser = await Users.findOne({ username: req.body.username });
if (!oneUser) {
return res.status(403).json("No such user in the database");
}
const isPassword = await bcryptjs.compare(req.body.password, oneUser.password);
if (!isPassword) {
return res.status(500).json("Password is incorrect");
}
const token = jwt.sign(
{
id: oneUser._id,
isAdmin: oneUser.isAdmin,
},
process.env.jwt,
);
const { password, ...others } = oneUser._doc;
res
.cookie("access_token", token)
.status(200)
.json({ ...others });
} catch (err) {
res.status(500).json(err);
}
};
in the routes auth file i call it like so
router.post("/login", login);
then in my index.html file, i linked the routes up.
app.use("/api/auth", authRoute);