my problem is I can store all the login details in MongoDB. and when I log a user I get the token but I'm not sure to do the logout functionality.
login
router.post("/admin/sign_in", async (req, res) => {
try {
const { email, password } = req.body;
if (!(email && password)) {
res.status(400).send("All input is required");
}
const user = await User.findOne({ email });
if (user && (await bcrypt.compare(password, user.password))) {
const token = jwt.sign(
{ user_id: user._id, email },
process.env.TOKEN_KEY,
{
expiresIn: "15h",
}
);
user.token = token;
return res.status(200).json(user);
}
} catch (err) {
return res.status(400).send("Invalid Credentials");
}
});
module.exports = router;
authentication
const jwt = require("jsonwebtoken");
const config = process.env;
const authenticate = (req, res, next) => {
const token =
req.body.token || req.query.token || req.headers["autherization"];
if (!token) {
return res.status(403).send("A token is required for authentication");
}
try {
const decoded = jwt.verify(token, config.TOKEN_KEY);
req.user = decoded;
} catch (err) {
return res.status(401).send("Invalid Token");
}
return next();
};
module.exports = authenticate;
here I have all the functionality working fine and I need an idea to implement the logout functionality