When I create a new "BlacklistedToken" document, I want it to be automatically deleted from my mongoDB database after lets say 1 day. So exactly 24 hours from after it has been created and added to the database, I want it automatically deleted.
I know there is something called TTL and I have seen examples of expiration dates being set for documents, but I never understood how to implement it with my code below.
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
token: {type: String, required: true, unique: true}
}, {timestamps: true});
module.exports = mongoose.model("BlacklistedToken", UserSchema);
const newBLT = new BlacklistedToken(
{ token: req.cookies.cookieJWT }
);
await newBLT.save();
Bonus question: What I am blacklisting is a JWT accesstoken. The tokens expires 24 hours after creation, and they are also stored in a httpOnly cookie which also expires in 24 hours. Now when I want to blacklist the token, is it possible to get the remaining time till the cookie expires and use that time as the TTL property when I create the new BlacklistedToken document?
TTL = req.cookies.cookieJWT.timeTilItExpires; // something like that