From 168c617897e82013b150c08e92b592608b113630 Mon Sep 17 00:00:00 2001 From: Bibiing Date: Tue, 17 Dec 2024 18:21:04 +0700 Subject: [PATCH] backend(10) --- package-lock.json | 54 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + server.js | 11 ++++++---- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0bd7883..99160ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "axios": "^1.7.9", "bcrypt": "^5.1.1", "bcryptjs": "^2.4.3", + "connect-mongo": "^5.1.0", "cookie-session": "^2.1.0", "cors": "^2.8.5", "date-fns": "^4.1.0", @@ -1876,6 +1877,18 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/asn1.js": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/asn1.js/-/asn1.js-5.4.1.tgz", + "integrity": "sha512-+I//4cYPccV8LdmBLiX8CYvf9Sp3vQsrqu2QNXRcrbiWvcx/UdlFiqUJJzxRQxgsZmvhXhn4cSKeSmoFjVdupA==", + "license": "MIT", + "dependencies": { + "bn.js": "^4.0.0", + "inherits": "^2.0.1", + "minimalistic-assert": "^1.0.0", + "safer-buffer": "^2.1.0" + } + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -1995,6 +2008,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/bn.js": { + "version": "4.12.1", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.12.1.tgz", + "integrity": "sha512-k8TVBiPkPJT9uHLdOKfFpqcfprwBFOAAXXozRubr7R7PfIuKvQlzcI4M0pALeqXN09vdaMbUdUj+pass+uULAg==", + "license": "MIT" + }, "node_modules/body-parser": { "version": "1.20.3", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.3.tgz", @@ -2492,6 +2511,23 @@ "url": "https://github.com/chalk/supports-color?sponsor=1" } }, + "node_modules/connect-mongo": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/connect-mongo/-/connect-mongo-5.1.0.tgz", + "integrity": "sha512-xT0vxQLqyqoUTxPLzlP9a/u+vir0zNkhiy9uAdHjSCcUUf7TS5b55Icw8lVyYFxfemP3Mf9gdwUOgeF3cxCAhw==", + "license": "MIT", + "dependencies": { + "debug": "^4.3.1", + "kruptein": "^3.0.0" + }, + "engines": { + "node": ">=12.9.0" + }, + "peerDependencies": { + "express-session": "^1.17.1", + "mongodb": ">= 5.1.0 < 7" + } + }, "node_modules/console-control-strings": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz", @@ -5044,6 +5080,18 @@ "json-buffer": "3.0.1" } }, + "node_modules/kruptein": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/kruptein/-/kruptein-3.0.7.tgz", + "integrity": "sha512-vTftnEjfbqFHLqxDUMQCj6gBo5lKqjV4f0JsM8rk8rM3xmvFZ2eSy4YALdaye7E+cDKnEj7eAjFR3vwh8a4PgQ==", + "license": "MIT", + "dependencies": { + "asn1.js": "^5.4.1" + }, + "engines": { + "node": ">8" + } + }, "node_modules/levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -5273,6 +5321,12 @@ "node": ">= 0.6" } }, + "node_modules/minimalistic-assert": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz", + "integrity": "sha512-UtJcAD4yEaGtjPezWuO9wC4nwUnVH/8/Im3yEHQP4b67cXlD/Qr9hdITCU1xDbSEXg2XKNaP8jsReV7vQd00/A==", + "license": "ISC" + }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", diff --git a/package.json b/package.json index 546e26d..c06ffc6 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "axios": "^1.7.9", "bcrypt": "^5.1.1", "bcryptjs": "^2.4.3", + "connect-mongo": "^5.1.0", "cookie-session": "^2.1.0", "cors": "^2.8.5", "date-fns": "^4.1.0", diff --git a/server.js b/server.js index 801f56c..bd0dbee 100644 --- a/server.js +++ b/server.js @@ -5,6 +5,7 @@ import helmet from "helmet"; import cors from "cors"; import session from "express-session"; import passport from "passport"; +import MongoStore from "connect-mongo"; // Import route handlers import setupAuthRoutes from "./src/routes/authGoogle.js"; @@ -47,10 +48,7 @@ app.use( // Database Connection const connectDB = async () => { try { - await mongoose.connect(process.env.MONGODB_URI, { - useNewUrlParser: true, - useUnifiedTopology: true, - }); + await mongoose.connect(process.env.MONGODB_URI); console.log("MongoDB connected successfully"); } catch (error) { console.error("MongoDB connection error:", error); @@ -78,6 +76,11 @@ app.use( secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: false, + store: MongoStore.create({ + mongoUrl: process.env.MONGODB_URI, + autoRemove: "interval", + autoRemoveInterval: 10, // Minutes + }), cookie: { maxAge: 24 * 60 * 60 * 1000, secure: process.env.NODE_ENV === "production",