From fdbe421f46090c641af5df257788891e8cc0f8b6 Mon Sep 17 00:00:00 2001 From: Barbara Goldbeck Date: Sat, 11 Oct 2025 15:46:51 +0200 Subject: [PATCH 1/6] Project day one done --- server/app.js | 21 +++++++++++++++------ server/package-lock.json | 23 +++++++++++++++++++++++ server/package.json | 1 + 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/server/app.js b/server/app.js index dfe51f49..29326cb9 100644 --- a/server/app.js +++ b/server/app.js @@ -2,19 +2,21 @@ const express = require("express"); const morgan = require("morgan"); const cookieParser = require("cookie-parser"); const PORT = 5005; +const cors = require("cors") // STATIC DATA // Devs Team - Import the provided files with JSON data of students and cohorts here: -// ... - +const cohorts = require("./cohorts.json") +const students = require("./students.json") // INITIALIZE EXPRESS APP - https://expressjs.com/en/4x/api.html#express const app = express(); // MIDDLEWARE -// Research Team - Set up CORS middleware here: -// ... +app.use(cors({ + origin: ["http://localhost:5005"] +})); app.use(express.json()); app.use(morgan("dev")); app.use(express.static("public")); @@ -23,12 +25,19 @@ app.use(cookieParser()); // ROUTES - https://expressjs.com/en/starter/basic-routing.html -// Devs Team - Start working on the routes here: -// ... app.get("/docs", (req, res) => { res.sendFile(__dirname + "/views/docs.html"); }); +app.get("/api/cohorts", (req, res) => { + res.json(cohorts) + +}); + +app.get("/api/students", (req, res) => { + res.json(students) + +}); // START SERVER app.listen(PORT, () => { diff --git a/server/package-lock.json b/server/package-lock.json index 23e2f78a..8268e7db 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "cookie-parser": "^1.4.6", + "cors": "^2.8.5", "express": "^4.18.2", "morgan": "^1.10.0", "nodemon": "^3.0.1" @@ -216,6 +217,19 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "license": "MIT", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -700,6 +714,15 @@ "node": ">=0.10.0" } }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-inspect": { "version": "1.12.3", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz", diff --git a/server/package.json b/server/package.json index 7aa0847c..18606964 100644 --- a/server/package.json +++ b/server/package.json @@ -19,6 +19,7 @@ "homepage": "https://github.com/ironhack-labs/cohort-tools-project#readme", "dependencies": { "cookie-parser": "^1.4.6", + "cors": "^2.8.5", "express": "^4.18.2", "morgan": "^1.10.0", "nodemon": "^3.0.1" From 2514946efad98b1356a731ceedce11b20c6932ff Mon Sep 17 00:00:00 2001 From: Barbara Goldbeck Date: Thu, 16 Oct 2025 19:38:55 +0200 Subject: [PATCH 2/6] day 1, day 2 completed --- server/app.js | 30 ++++- server/models/Cohort.model.js | 20 +++ server/models/Student.model.js | 23 ++++ server/package-lock.json | 235 +++++++++++++++++++++++++++++++++ server/package.json | 1 + 5 files changed, 305 insertions(+), 4 deletions(-) create mode 100644 server/models/Cohort.model.js create mode 100644 server/models/Student.model.js diff --git a/server/app.js b/server/app.js index 29326cb9..755c82aa 100644 --- a/server/app.js +++ b/server/app.js @@ -3,6 +3,14 @@ const morgan = require("morgan"); const cookieParser = require("cookie-parser"); const PORT = 5005; const cors = require("cors") +const mongoose = require("mongoose") +const Student = require("./models/Student.model.js") +const Cohort = require("./models/Cohort.model.js") + +mongoose +.connect('mongodb://127.0.0.1:27017/cohorts-tools-api') + .then(x => console.log(`Connected to Mongo! Database name: "${x.connections[0].name}"`)) + .catch(err => console.error('Error connecting to mongo', err)); // STATIC DATA // Devs Team - Import the provided files with JSON data of students and cohorts here: @@ -30,13 +38,27 @@ app.get("/docs", (req, res) => { }); app.get("/api/cohorts", (req, res) => { - res.json(cohorts) - + Cohort.find({}) + .then(cohorts => { + console.log("Retrieved cohorts:", cohorts) + res.status(200).json(cohorts); + }) + .catch(error => { + console.error("Error while retrieving cohorts data", error) + res.status(500).json({ error: "Failed to retrieve cohorts data"}) + }); }); app.get("/api/students", (req, res) => { - res.json(students) - + Student.find({}) + .then(students => { + console.log("Retrieved students:", students) + res.status(200).json(students); + }) + .catch(error => { + console.error("Error while retrieving students data", error) + res.status(500).json({ error: "Failed to retrieve students data"}) + }) }); // START SERVER diff --git a/server/models/Cohort.model.js b/server/models/Cohort.model.js new file mode 100644 index 00000000..47d02425 --- /dev/null +++ b/server/models/Cohort.model.js @@ -0,0 +1,20 @@ +const mongoose = require("mongoose") +const Schema = mongoose.Schema + + +const cohortSchema = new Schema({ + inProgress: Boolean, + cohortSlug: String, + cohortName: String, + program: String, + campus: String, + startDate: Date, + endDate: Date, + programManager: String, + leadTeacher: String, + totalHours: Number, +}) + +const Cohort = mongoose.model("Cohort", cohortSchema); + +module.exports = Cohort; \ No newline at end of file diff --git a/server/models/Student.model.js b/server/models/Student.model.js new file mode 100644 index 00000000..043734e3 --- /dev/null +++ b/server/models/Student.model.js @@ -0,0 +1,23 @@ +const mongoose = require("mongoose") +const Schema = mongoose.Schema + +const studentSchema = new Schema({ + firstName: String, + lastName: String, + email: String, + phone: String, + linkedUrl: String, + languages: [String], + program: String, + background: String, + image: String, + projects: [String], + cohort: { + type: mongoose.Schema.Types.ObjectId, + ref: "Cohort" + } +}) + +const Student = mongoose.model("Student", studentSchema) + +module.exports = Student; \ No newline at end of file diff --git a/server/package-lock.json b/server/package-lock.json index 8268e7db..880649e5 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -12,10 +12,35 @@ "cookie-parser": "^1.4.6", "cors": "^2.8.5", "express": "^4.18.2", + "mongoose": "^8.19.1", "morgan": "^1.10.0", "nodemon": "^3.0.1" } }, + "node_modules/@mongodb-js/saslprep": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.3.1.tgz", + "integrity": "sha512-6nZrq5kfAz0POWyhljnbWQQJQ5uT8oE2ddX303q1uY0tWsivWKgBDXBBvuFPwOqRRalXJuVO9EjOdVtuhLX0zg==", + "license": "MIT", + "dependencies": { + "sparse-bitfield": "^3.0.3" + } + }, + "node_modules/@types/webidl-conversions": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz", + "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA==", + "license": "MIT" + }, + "node_modules/@types/whatwg-url": { + "version": "11.0.5", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.5.tgz", + "integrity": "sha512-coYR071JRaHa+xoEvvYqvnIHaVqaYrLPbsufM9BF63HkwI5Lgmy2QR8Q5K/lYDYo5AK82wOvSOS0UsLTpTG7uQ==", + "license": "MIT", + "dependencies": { + "@types/webidl-conversions": "*" + } + }, "node_modules/abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -122,6 +147,15 @@ "node": ">=8" } }, + "node_modules/bson": { + "version": "6.10.4", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.10.4.tgz", + "integrity": "sha512-WIsKqkSC0ABoBJuT1LEX+2HEvNmNKKgnTAyd0fL8qzK4SH2i9NXg+t08YtdZp/V9IZ33cxe3iV4yM0qg8lMQng==", + "license": "Apache-2.0", + "engines": { + "node": ">=16.20.1" + } + }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -540,6 +574,15 @@ "node": ">=0.12.0" } }, + "node_modules/kareem": { + "version": "2.6.3", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.6.3.tgz", + "integrity": "sha512-C3iHfuGUXK2u8/ipq9LfjFfXFxAZMQJJq7vLS45r3D9Y2xQ/m4S8zaR4zMLFWh9AsNPXmcFfUDhTEO8UIC/V6Q==", + "license": "Apache-2.0", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/lru-cache": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", @@ -559,6 +602,12 @@ "node": ">= 0.6" } }, + "node_modules/memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "license": "MIT" + }, "node_modules/merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -613,6 +662,90 @@ "node": "*" } }, + "node_modules/mongodb": { + "version": "6.20.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.20.0.tgz", + "integrity": "sha512-Tl6MEIU3K4Rq3TSHd+sZQqRBoGlFsOgNrH5ltAcFBV62Re3Fd+FcaVf8uSEQFOJ51SDowDVttBTONMfoYWrWlQ==", + "license": "Apache-2.0", + "dependencies": { + "@mongodb-js/saslprep": "^1.3.0", + "bson": "^6.10.4", + "mongodb-connection-string-url": "^3.0.2" + }, + "engines": { + "node": ">=16.20.1" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.188.0", + "@mongodb-js/zstd": "^1.1.0 || ^2.0.0", + "gcp-metadata": "^5.2.0", + "kerberos": "^2.0.1", + "mongodb-client-encryption": ">=6.0.0 <7", + "snappy": "^7.3.2", + "socks": "^2.7.1" + }, + "peerDependenciesMeta": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "@mongodb-js/zstd": { + "optional": true + }, + "gcp-metadata": { + "optional": true + }, + "kerberos": { + "optional": true + }, + "mongodb-client-encryption": { + "optional": true + }, + "snappy": { + "optional": true + }, + "socks": { + "optional": true + } + } + }, + "node_modules/mongodb-connection-string-url": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.2.tgz", + "integrity": "sha512-rMO7CGo/9BFwyZABcKAWL8UJwH/Kc2x0g72uhDWzG48URRax5TCIcJ7Rc3RZqffZzO/Gwff/jyKwCU9TN8gehA==", + "license": "Apache-2.0", + "dependencies": { + "@types/whatwg-url": "^11.0.2", + "whatwg-url": "^14.1.0 || ^13.0.0" + } + }, + "node_modules/mongoose": { + "version": "8.19.1", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.19.1.tgz", + "integrity": "sha512-oB7hGQJn4f8aebqE7mhE54EReb5cxVgpCxQCQj0K/cK3q4J3Tg08nFP6sM52nJ4Hlm8jsDnhVYpqIITZUAhckQ==", + "license": "MIT", + "dependencies": { + "bson": "^6.10.4", + "kareem": "2.6.3", + "mongodb": "~6.20.0", + "mpath": "0.9.0", + "mquery": "5.0.0", + "ms": "2.1.3", + "sift": "17.1.3" + }, + "engines": { + "node": ">=16.20.1" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/mongoose" + } + }, + "node_modules/mongoose/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, "node_modules/morgan": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", @@ -639,6 +772,50 @@ "node": ">= 0.8" } }, + "node_modules/mpath": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz", + "integrity": "sha512-ikJRQTk8hw5DEoFVxHG1Gn9T/xcjtdnOKIU1JTmGjZZlg9LST2mBLmcX3/ICIbgJydT2GOc15RnNy5mHmzfSew==", + "license": "MIT", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/mquery": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-5.0.0.tgz", + "integrity": "sha512-iQMncpmEK8R8ncT8HJGsGc9Dsp8xcgYMVSbs5jgnm1lFHTZqMJTUWTDx1LBO8+mK3tPNZWFLBghQEIOULSTHZg==", + "license": "MIT", + "dependencies": { + "debug": "4.x" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/mquery/node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/mquery/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -791,6 +968,15 @@ "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", "integrity": "sha512-77DZwxQmxKnu3aR542U+X8FypNzbfJ+C5XQDk3uWjWxn6151aIMGthWYRXTqT1E5oJvg+ljaa2OJi+VfvCOQ8w==" }, + "node_modules/punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, "node_modules/qs": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", @@ -936,6 +1122,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/sift": { + "version": "17.1.3", + "resolved": "https://registry.npmjs.org/sift/-/sift-17.1.3.tgz", + "integrity": "sha512-Rtlj66/b0ICeFzYTuNvX/EF1igRbbnGSvEyT79McoZa/DeGhMyC5pWKOEsZKnpkqtSeovd5FL/bjHWC3CIIvCQ==", + "license": "MIT" + }, "node_modules/simple-update-notifier": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/simple-update-notifier/-/simple-update-notifier-2.0.0.tgz", @@ -947,6 +1139,15 @@ "node": ">=10" } }, + "node_modules/sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", + "license": "MIT", + "dependencies": { + "memory-pager": "^1.0.2" + } + }, "node_modules/statuses": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", @@ -996,6 +1197,18 @@ "nodetouch": "bin/nodetouch.js" } }, + "node_modules/tr46": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-5.1.1.tgz", + "integrity": "sha512-hdF5ZgjTqgAntKkklYw0R03MG2x/bSzTtkxmIRw/sTNV8YXsCJ1tfLAX23lhxhHJlEf3CRCOCGGWw3vI3GaSPw==", + "license": "MIT", + "dependencies": { + "punycode": "^2.3.1" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", @@ -1037,6 +1250,28 @@ "node": ">= 0.8" } }, + "node_modules/webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + } + }, + "node_modules/whatwg-url": { + "version": "14.2.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-14.2.0.tgz", + "integrity": "sha512-De72GdQZzNTUBBChsXueQUnPKDkg/5A5zp7pFDuQAj5UFoENpiACU0wlCvzpAGnTkj++ihpKwKyYewn/XNUbKw==", + "license": "MIT", + "dependencies": { + "tr46": "^5.1.0", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", diff --git a/server/package.json b/server/package.json index 18606964..13a9dd05 100644 --- a/server/package.json +++ b/server/package.json @@ -21,6 +21,7 @@ "cookie-parser": "^1.4.6", "cors": "^2.8.5", "express": "^4.18.2", + "mongoose": "^8.19.1", "morgan": "^1.10.0", "nodemon": "^3.0.1" } From 4e2df49d03081c2bb5554fc1126ac30ab0b6c9e0 Mon Sep 17 00:00:00 2001 From: Barbara Goldbeck Date: Fri, 17 Oct 2025 11:32:59 +0200 Subject: [PATCH 3/6] completed day3 --- server/app.js | 116 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 113 insertions(+), 3 deletions(-) diff --git a/server/app.js b/server/app.js index 755c82aa..9c7662fa 100644 --- a/server/app.js +++ b/server/app.js @@ -22,9 +22,9 @@ const app = express(); // MIDDLEWARE -app.use(cors({ - origin: ["http://localhost:5005"] -})); +app.use(cors( { + origin: ["http://localhost:5173"] +} )); app.use(express.json()); app.use(morgan("dev")); app.use(express.static("public")); @@ -37,6 +37,7 @@ app.get("/docs", (req, res) => { res.sendFile(__dirname + "/views/docs.html"); }); +//COHORTS app.get("/api/cohorts", (req, res) => { Cohort.find({}) .then(cohorts => { @@ -49,6 +50,52 @@ app.get("/api/cohorts", (req, res) => { }); }); +app.post("/api/cohorts", (req, res) => { + Cohort.create(req.body) + .then(createdCohort => { + console.log("Cohort created", createdCohort) + res.status(201).json(createdCohort) + }) + .catch(error => { + console.error("Error creating cohort", error) + res.status(500).json( { error: "Failed to create cohort"}) + }) +}) + +app.get("/api/cohorts/:cohortId", (req, res) => { + Cohort.findById(req.params.cohortId) + .then(cohort => { + console.log("Single cohort", cohort) + res.status(200).json(cohort) + }) +}) + +app.put("/api/cohorts/:cohortId", (req, res) => { + Cohort.findByIdAndUpdate(req.params.cohortId, req.body, { new: true }) + .then(updatedCohort => { + console.log("Updated cohort", updatedCohort) + res.status(200).json(updatedCohort) + }) + .catch(error => { + console.error("Error updating cohort", error) + res.status(500).json( { error: "Error updating cohort."}) + }) +}) + +app.delete("/api/cohorts/:cohortId", (req, res) => { + Cohort.findByIdAndDelete(req.params.cohortId) + .then(result => { + console.log("Cohort deleted") + res.status(204).send(); + }) + .catch(error => { + console.error("Error deleting cohort", error) + res.status(500).json( {error: "Error deleting cohort"}) + }) +}) + + +//STUDENTS app.get("/api/students", (req, res) => { Student.find({}) .then(students => { @@ -61,6 +108,69 @@ app.get("/api/students", (req, res) => { }) }); +app.post("/api/students", (req, res) => { + Student.create(req.body) + .then(createdStudent => { + console.log("Retrieved students:", createdStudent) + res.status(200).json(createdStudent); + }) + .catch(error => { + console.error("Error while creating student", error) + res.status(500).json({ error: "Failed to create student"}) + }) +}); + +//Get all students of a given cohort +app.get("/api/students/cohort/:cohortId", (req, res) => { + Student.find({ cohort: req.params.cohortId }) + .populate("cohort") + .then(students => { + console.log("Retrieved students:", students) + res.status(200).json(students); + }) + .catch(error => { + console.error("Error while retrieving students of cohort.", error) + res.status(500).json({ error: "Failed to retrieve students of cohort."}) + }) +}); + +//Get specific student by Id +app.get("/api/students/:studentId", (req, res) => { + Student.findById(req.params.studentId) + .then(student => { + console.log("Retrieved student:", student) + res.status(200).json(student); + }) + .catch(error => { + console.error("Error while retrieving students data", error) + res.status(500).json({ error: "Failed to retrieve students data"}) + }) +}); + +app.put("/api/students/:studentId", (req, res) => { + Student.findByIdAndUpdate(req.params.studentId, req.body, { new: true }) + .then(updatedStudent => { + console.log("Updated student", updatedStudent) + res.status(200).json(updatedStudent) + }) + .catch(error => { + console.error("Error updating student", error) + res.status(500).json( { error: "Error updating student."}) + }) +}) + +app.delete("api/students/:studentId", (req, res) => { + Student.findByIdAndDelete(req.params.studentId) + .then(result => { + console.log("Student deleted") + res.status(204).send(); + }) + .catch(error => { + console.error("Error deleting student", error) + res.status(500).json( {error: "Error deleting studet"}) + }) +}) + // START SERVER app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); From 12ceb85f0ed2e7c54707730fbffa9d071b4188f0 Mon Sep 17 00:00:00 2001 From: Barbara Goldbeck Date: Thu, 23 Oct 2025 21:04:35 +0200 Subject: [PATCH 4/6] working on day 4 and 5 --- server/.env.sample | 3 +- server/app.js | 78 +--- server/error-handling/index.js | 17 + server/middleware/jwt.middleware.js | 15 + server/models/User.model.js | 16 + server/package-lock.json | 584 +++++++++++++++++++++++----- server/package.json | 7 +- server/routes/auth.routes.js | 97 +++++ server/routes/cohorts.routes.js | 0 server/routes/students.routes.js | 83 ++++ 10 files changed, 725 insertions(+), 175 deletions(-) create mode 100644 server/error-handling/index.js create mode 100644 server/middleware/jwt.middleware.js create mode 100644 server/models/User.model.js create mode 100644 server/routes/auth.routes.js create mode 100644 server/routes/cohorts.routes.js create mode 100644 server/routes/students.routes.js diff --git a/server/.env.sample b/server/.env.sample index 0042a604..03bdc5af 100644 --- a/server/.env.sample +++ b/server/.env.sample @@ -1 +1,2 @@ -PORT=5005 \ No newline at end of file +PORT=5005 +TOKEN_SECRET=3B4b0Ba8 \ No newline at end of file diff --git a/server/app.js b/server/app.js index 9c7662fa..27ab1f00 100644 --- a/server/app.js +++ b/server/app.js @@ -6,6 +6,8 @@ const cors = require("cors") const mongoose = require("mongoose") const Student = require("./models/Student.model.js") const Cohort = require("./models/Cohort.model.js") +const { errorHandler, notFoundHandler } = require("./error-handling/index.js") +const { isAuthenticated } = require("./middleware/jwt.middleware.js") mongoose .connect('mongodb://127.0.0.1:27017/cohorts-tools-api') @@ -30,6 +32,7 @@ app.use(morgan("dev")); app.use(express.static("public")); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); +app.use(errorHandler); // ROUTES - https://expressjs.com/en/starter/basic-routing.html @@ -95,81 +98,16 @@ app.delete("/api/cohorts/:cohortId", (req, res) => { }) -//STUDENTS -app.get("/api/students", (req, res) => { - Student.find({}) - .then(students => { - console.log("Retrieved students:", students) - res.status(200).json(students); - }) - .catch(error => { - console.error("Error while retrieving students data", error) - res.status(500).json({ error: "Failed to retrieve students data"}) - }) -}); -app.post("/api/students", (req, res) => { - Student.create(req.body) - .then(createdStudent => { - console.log("Retrieved students:", createdStudent) - res.status(200).json(createdStudent); - }) - .catch(error => { - console.error("Error while creating student", error) - res.status(500).json({ error: "Failed to create student"}) - }) -}); -//Get all students of a given cohort -app.get("/api/students/cohort/:cohortId", (req, res) => { - Student.find({ cohort: req.params.cohortId }) - .populate("cohort") - .then(students => { - console.log("Retrieved students:", students) - res.status(200).json(students); - }) - .catch(error => { - console.error("Error while retrieving students of cohort.", error) - res.status(500).json({ error: "Failed to retrieve students of cohort."}) - }) -}); +const studentsRouter = require('./routes/students.routes.js') +app.use("/api/students", studentsRouter) -//Get specific student by Id -app.get("/api/students/:studentId", (req, res) => { - Student.findById(req.params.studentId) - .then(student => { - console.log("Retrieved student:", student) - res.status(200).json(student); - }) - .catch(error => { - console.error("Error while retrieving students data", error) - res.status(500).json({ error: "Failed to retrieve students data"}) - }) -}); +const authRouter = require("./routes/auth.routes"); +app.use("/auth", authRouter) -app.put("/api/students/:studentId", (req, res) => { - Student.findByIdAndUpdate(req.params.studentId, req.body, { new: true }) - .then(updatedStudent => { - console.log("Updated student", updatedStudent) - res.status(200).json(updatedStudent) - }) - .catch(error => { - console.error("Error updating student", error) - res.status(500).json( { error: "Error updating student."}) - }) -}) +app.use(notFoundHandler); -app.delete("api/students/:studentId", (req, res) => { - Student.findByIdAndDelete(req.params.studentId) - .then(result => { - console.log("Student deleted") - res.status(204).send(); - }) - .catch(error => { - console.error("Error deleting student", error) - res.status(500).json( {error: "Error deleting studet"}) - }) -}) // START SERVER app.listen(PORT, () => { diff --git a/server/error-handling/index.js b/server/error-handling/index.js new file mode 100644 index 00000000..0c17e1f0 --- /dev/null +++ b/server/error-handling/index.js @@ -0,0 +1,17 @@ +function errorHandler (err, req, res, next) { + + console.error("ERROR", req.method, req.path, err); + + if(!res.headersSent) { + + res.status(500).json({ message: "Internal server error. Check the server console."}); + + } +} + +function notFoundHandler (req, res, next) { + + res.status(404).json({message: "This route does not exist"}) +} + +module.exports = { errorHandler, notFoundHandler} \ No newline at end of file diff --git a/server/middleware/jwt.middleware.js b/server/middleware/jwt.middleware.js new file mode 100644 index 00000000..590666c4 --- /dev/null +++ b/server/middleware/jwt.middleware.js @@ -0,0 +1,15 @@ +const jwt = require("jsonwebtoken"); + +const isAuthenticated = (req, res, next) => { + try{ + const token = req.headers.authorization.split(" ")[1]; + const payload = jwt.verify(token, process.env.TOKEN_SECRET); + req.payload = payload; + + next() + } catch (error) { + res.status(401).json("Token not provided or not valid") + } +} + +module.exports = { isAuthenticated } \ No newline at end of file diff --git a/server/models/User.model.js b/server/models/User.model.js new file mode 100644 index 00000000..3f516a43 --- /dev/null +++ b/server/models/User.model.js @@ -0,0 +1,16 @@ +const mongoose = require("mongoose"); +const { Schema, model } = mongoose; + +const userSchema = new Schema({ + email: { + type: String, + unique: true + }, + password: String, + name: String, + + +}); + +module.exports = model("User", userSchema) + diff --git a/server/package-lock.json b/server/package-lock.json index 880649e5..a66bc4d5 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -9,10 +9,13 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "bcryptjs": "^3.0.2", "cookie-parser": "^1.4.6", "cors": "^2.8.5", - "express": "^4.18.2", - "mongoose": "^8.19.1", + "express": "^4.21.2", + "express-jwt": "^8.5.1", + "jsonwebtoken": "^9.0.2", + "mongoose": "^8.19.2", "morgan": "^1.10.0", "nodemon": "^3.0.1" } @@ -26,6 +29,31 @@ "sparse-bitfield": "^3.0.3" } }, + "node_modules/@types/jsonwebtoken": { + "version": "9.0.10", + "resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-9.0.10.tgz", + "integrity": "sha512-asx5hIG9Qmf/1oStypjanR7iKTv0gXQ1Ov/jfrX6kS/EO0OFni8orbmGCn0672NHR3kXHwpAwR+B368ZGN/2rA==", + "license": "MIT", + "dependencies": { + "@types/ms": "*", + "@types/node": "*" + } + }, + "node_modules/@types/ms": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@types/ms/-/ms-2.1.0.tgz", + "integrity": "sha512-GsCCIZDE/p3i96vtEqx+7dBUGXrc7zeSK3wwPHIaRThS+9OhWIXRqzs4d6k1SVU8g91DrNRWxWUGhp5KXQb2VA==", + "license": "MIT" + }, + "node_modules/@types/node": { + "version": "24.9.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-24.9.1.tgz", + "integrity": "sha512-QoiaXANRkSXK6p0Duvt56W208du4P9Uye9hWLWgGMDTEoKPhuenzNcC4vGUmrNkiOKTlIrBoyNQYNpSwfEZXSg==", + "license": "MIT", + "dependencies": { + "undici-types": "~7.16.0" + } + }, "node_modules/@types/webidl-conversions": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz", @@ -96,6 +124,15 @@ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, + "node_modules/bcryptjs": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-3.0.2.tgz", + "integrity": "sha512-k38b3XOZKv60C4E2hVsXTolJWfkGRMbILBIe2IBITXciy5bOsTKot5kDrf3ZfufQtQOUN5mXceUEpU1rTl9Uog==", + "license": "BSD-3-Clause", + "bin": { + "bcrypt": "bin/bcrypt" + } + }, "node_modules/binary-extensions": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", @@ -105,20 +142,21 @@ } }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.3", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.3.tgz", + "integrity": "sha512-7rAxByjUMqQ3/bHJy7D6OGXvx/MMc4IqBn/X0fcM1QUcAItpZrBEYhWGem+tzXH90c+G01ypMcYJBO9Y30203g==", + "license": "MIT", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", "http-errors": "2.0.0", "iconv-lite": "0.4.24", "on-finished": "2.4.1", - "qs": "6.11.0", - "raw-body": "2.5.1", + "qs": "6.13.0", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -156,21 +194,45 @@ "node": ">=16.20.1" } }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==", + "license": "BSD-3-Clause" + }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", + "license": "MIT", "engines": { "node": ">= 0.8" } }, - "node_modules/call-bind": { + "node_modules/call-bind-apply-helpers": { "version": "1.0.2", - "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", - "integrity": "sha512-7O+FbCihrB5WGbFYesctwmTKae6rOiIzmz1icreWJ+0aA7LJfuqhEso2T9ncpcFtzMQtzXf2QGGueWJGTYsqrA==", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/call-bound": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz", + "integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==", + "license": "MIT", "dependencies": { - "function-bind": "^1.1.1", - "get-intrinsic": "^1.0.2" + "call-bind-apply-helpers": "^1.0.2", + "get-intrinsic": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" }, "funding": { "url": "https://github.com/sponsors/ljharb" @@ -222,6 +284,7 @@ "version": "1.0.5", "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==", + "license": "MIT", "engines": { "node": ">= 0.6" } @@ -284,68 +347,126 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==", + "license": "MIT", "engines": { "node": ">= 0.8", "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "license": "Apache-2.0", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==" }, "node_modules/encodeurl": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", - "integrity": "sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz", + "integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==", + "license": "MIT", "engines": { "node": ">= 0.8" } }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-object-atoms": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", + "integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/escape-html": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", - "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==" + "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==", + "license": "MIT" }, "node_modules/etag": { "version": "1.8.1", "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==", + "license": "MIT", "engines": { "node": ">= 0.6" } }, "node_modules/express": { - "version": "4.18.2", - "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", - "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==", + "version": "4.21.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.21.2.tgz", + "integrity": "sha512-28HqgMZAmih1Czt9ny7qr6ek2qddF4FclbMzwhCREB6OFfH+rXAnuNCwo1/wFvrtbgsQDb4kSbX9de9lFbrXnA==", + "license": "MIT", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", - "body-parser": "1.20.1", + "body-parser": "1.20.3", "content-disposition": "0.5.4", "content-type": "~1.0.4", - "cookie": "0.5.0", + "cookie": "0.7.1", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", - "encodeurl": "~1.0.2", + "encodeurl": "~2.0.0", "escape-html": "~1.0.3", "etag": "~1.8.1", - "finalhandler": "1.2.0", + "finalhandler": "1.3.1", "fresh": "0.5.2", "http-errors": "2.0.0", - "merge-descriptors": "1.0.1", + "merge-descriptors": "1.0.3", "methods": "~1.1.2", "on-finished": "2.4.1", "parseurl": "~1.3.3", - "path-to-regexp": "0.1.7", + "path-to-regexp": "0.1.12", "proxy-addr": "~2.0.7", - "qs": "6.11.0", + "qs": "6.13.0", "range-parser": "~1.2.1", "safe-buffer": "5.2.1", - "send": "0.18.0", - "serve-static": "1.15.0", + "send": "0.19.0", + "serve-static": "1.16.2", "setprototypeof": "1.2.0", "statuses": "2.0.1", "type-is": "~1.6.18", @@ -354,12 +475,37 @@ }, "engines": { "node": ">= 0.10.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" } }, + "node_modules/express-jwt": { + "version": "8.5.1", + "resolved": "https://registry.npmjs.org/express-jwt/-/express-jwt-8.5.1.tgz", + "integrity": "sha512-Dv6QjDLpR2jmdb8M6XQXiCcpEom7mK8TOqnr0/TngDKsG2DHVkO8+XnVxkJVN7BuS1I3OrGw6N8j5DaaGgkDRQ==", + "license": "MIT", + "dependencies": { + "@types/jsonwebtoken": "^9", + "express-unless": "^2.1.3", + "jsonwebtoken": "^9.0.0" + }, + "engines": { + "node": ">= 8.0.0" + } + }, + "node_modules/express-unless": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/express-unless/-/express-unless-2.1.3.tgz", + "integrity": "sha512-wj4tLMyCVYuIIKHGt0FhCtIViBcwzWejX0EjNxveAa6dG+0XBCQhMbx+PnkLkFCxLC69qoFrxds4pIyL88inaQ==", + "license": "MIT" + }, "node_modules/express/node_modules/cookie": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", - "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==", + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.1.tgz", + "integrity": "sha512-6DnInpx7SJ2AK3+CTUE/ZM0vWTUboZCegxhC2xiIydHR9jNuTAASBrfEpHhiGOZw/nX51bHt6YQl8jsGo4y/0w==", + "license": "MIT", "engines": { "node": ">= 0.6" } @@ -376,12 +522,13 @@ } }, "node_modules/finalhandler": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", - "integrity": "sha512-5uXcUVftlQMFnWC9qu/svkWv3GTd2PfUhK/3PLkYNAe7FbqJMt3515HaxE6eRL74GdsriiwujiawdaB1BpEISg==", + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.3.1.tgz", + "integrity": "sha512-6BN9trH7bp3qvnrRyzsBz+g3lZxTNZTbVO2EV1CS0WIcDbawYVdYvGflME/9QP0h0pYlCDBCTjYa9nZzMDpyxQ==", + "license": "MIT", "dependencies": { "debug": "2.6.9", - "encodeurl": "~1.0.2", + "encodeurl": "~2.0.0", "escape-html": "~1.0.3", "on-finished": "2.4.1", "parseurl": "~1.3.3", @@ -404,6 +551,7 @@ "version": "0.5.2", "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", "integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==", + "license": "MIT", "engines": { "node": ">= 0.6" } @@ -422,24 +570,51 @@ } }, "node_modules/function-bind": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } }, "node_modules/get-intrinsic": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.1.tgz", - "integrity": "sha512-2DcsyfABl+gVHEfCOaTrWgyt+tb6MSEGmKq+kI5HwLbIYgjgmMcV8KQ41uaKz1xxUcn9tJtgFbQUEVcEbd0FYw==", + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "license": "MIT", "dependencies": { - "function-bind": "^1.1.1", - "has": "^1.0.3", - "has-proto": "^1.0.1", - "has-symbols": "^1.0.3" + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" }, "funding": { "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/glob-parent": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", @@ -451,15 +626,16 @@ "node": ">= 6" } }, - "node_modules/has": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", - "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", - "dependencies": { - "function-bind": "^1.1.1" - }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", "engines": { - "node": ">= 0.4.0" + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" } }, "node_modules/has-flag": { @@ -470,10 +646,11 @@ "node": ">=4" } }, - "node_modules/has-proto": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/has-proto/-/has-proto-1.0.1.tgz", - "integrity": "sha512-7qE+iP+O+bgF9clE5+UoBFzE65mlBiVj3tKCrlNQ0Ogwm0BjpT/gK4SlLYDMybDh5I3TCTKnPPa0oMG7JDYrhg==", + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "license": "MIT", "engines": { "node": ">= 0.4" }, @@ -481,21 +658,23 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/has-symbols": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.3.tgz", - "integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==", + "node_modules/hasown": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", + "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, "engines": { "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" } }, "node_modules/http-errors": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", "integrity": "sha512-FtwrG/euBzaEjYeRqOgly7G0qviiXoJWnvEH2Z1plBdXgbyjv34pHTSb9zoeHMyDy33+DWy5Wt9Wo+TURtOYSQ==", + "license": "MIT", "dependencies": { "depd": "2.0.0", "inherits": "2.0.4", @@ -511,6 +690,7 @@ "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "license": "MIT", "dependencies": { "safer-buffer": ">= 2.1.2 < 3" }, @@ -526,7 +706,8 @@ "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "license": "ISC" }, "node_modules/ipaddr.js": { "version": "1.9.1", @@ -574,6 +755,55 @@ "node": ">=0.12.0" } }, + "node_modules/jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "license": "MIT", + "dependencies": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^7.5.4" + }, + "engines": { + "node": ">=12", + "npm": ">=6" + } + }, + "node_modules/jsonwebtoken/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/jwa": { + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.2.tgz", + "integrity": "sha512-eeH5JO+21J78qMvTIDdBXidBd6nG2kZjg5Ohz/1fpa28Z4CcsWUzJ1ZZyFq/3z3N17aZy+ZuBoHljASbL1WfOw==", + "license": "MIT", + "dependencies": { + "buffer-equal-constant-time": "^1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "license": "MIT", + "dependencies": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "node_modules/kareem": { "version": "2.6.3", "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.6.3.tgz", @@ -583,6 +813,48 @@ "node": ">=12.0.0" } }, + "node_modules/lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==", + "license": "MIT" + }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==", + "license": "MIT" + }, + "node_modules/lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==", + "license": "MIT" + }, + "node_modules/lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==", + "license": "MIT" + }, + "node_modules/lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==", + "license": "MIT" + }, + "node_modules/lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==", + "license": "MIT" + }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==", + "license": "MIT" + }, "node_modules/lru-cache": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", @@ -594,10 +866,20 @@ "node": ">=10" } }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==", + "license": "MIT", "engines": { "node": ">= 0.6" } @@ -609,9 +891,13 @@ "license": "MIT" }, "node_modules/merge-descriptors": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", - "integrity": "sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w==" + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.3.tgz", + "integrity": "sha512-gaNvAS7TZ897/rVaZ0nMtAyxNyi/pdbjbAwUpFQpN70GqnVfOiXpeUUMKRBmzXaSQ8DdTX4/0ms62r2K+hE6mQ==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } }, "node_modules/methods": { "version": "1.1.2", @@ -625,6 +911,7 @@ "version": "1.6.0", "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==", + "license": "MIT", "bin": { "mime": "cli.js" }, @@ -719,9 +1006,9 @@ } }, "node_modules/mongoose": { - "version": "8.19.1", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.19.1.tgz", - "integrity": "sha512-oB7hGQJn4f8aebqE7mhE54EReb5cxVgpCxQCQj0K/cK3q4J3Tg08nFP6sM52nJ4Hlm8jsDnhVYpqIITZUAhckQ==", + "version": "8.19.2", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.19.2.tgz", + "integrity": "sha512-ww2T4dBV+suCbOfG5YPwj9pLCfUVyj8FEA1D3Ux1HHqutpLxGyOYEPU06iPRBW4cKr3PJfOSYsIpHWPTkz5zig==", "license": "MIT", "dependencies": { "bson": "^6.10.4", @@ -901,9 +1188,13 @@ } }, "node_modules/object-inspect": { - "version": "1.12.3", - "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz", - "integrity": "sha512-geUvdk7c+eizMNUDkRpW1wJwgfOiOeHbxBR/hLXK1aT6zmVSO0jsQcs7fj6MGw89jC/cjGfLcNOrtMYtGqm81g==", + "version": "1.13.4", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz", + "integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, "funding": { "url": "https://github.com/sponsors/ljharb" } @@ -912,6 +1203,7 @@ "version": "2.4.1", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz", "integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==", + "license": "MIT", "dependencies": { "ee-first": "1.1.1" }, @@ -931,14 +1223,16 @@ "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", + "license": "MIT", "engines": { "node": ">= 0.8" } }, "node_modules/path-to-regexp": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", - "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==" + "version": "0.1.12", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.12.tgz", + "integrity": "sha512-RA1GjUVMnvYFxuqovrEqZoxxW5NUZqbwKtYz/Tt7nXerk0LbLblQmrsgdeOxV5SFHf0UDggjS/bSeOZwt1pmEQ==", + "license": "MIT" }, "node_modules/picomatch": { "version": "2.3.1", @@ -978,11 +1272,12 @@ } }, "node_modules/qs": { - "version": "6.11.0", - "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", - "integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==", + "version": "6.13.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.13.0.tgz", + "integrity": "sha512-+38qI9SOr8tfZ4QmJNplMUxqjbe7LKvvZgWdExBOmd+egZTtjLB67Gu0HRX3u/XOq7UU2Nx6nsjvS16Z9uwfpg==", + "license": "BSD-3-Clause", "dependencies": { - "side-channel": "^1.0.4" + "side-channel": "^1.0.6" }, "engines": { "node": ">=0.6" @@ -995,14 +1290,16 @@ "version": "1.2.1", "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==", + "license": "MIT", "engines": { "node": ">= 0.6" } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", + "license": "MIT", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", @@ -1046,7 +1343,8 @@ "node_modules/safer-buffer": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", - "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "license": "MIT" }, "node_modules/semver": { "version": "7.5.4", @@ -1063,9 +1361,10 @@ } }, "node_modules/send": { - "version": "0.18.0", - "resolved": "https://registry.npmjs.org/send/-/send-0.18.0.tgz", - "integrity": "sha512-qqWzuOjSFOuqPjFe4NOsMLafToQQwBSOEpS+FwEt3A2V3vKubTquT3vmLTQpFgMXp8AlFWFuP1qKaJZOtPpVXg==", + "version": "0.19.0", + "resolved": "https://registry.npmjs.org/send/-/send-0.19.0.tgz", + "integrity": "sha512-dW41u5VfLXu8SJh5bwRmyYUbAoSB3c9uQh6L8h/KtsFREPWpbX1lrljJo186Jc4nmci/sGUZ9a0a0J2zgfq2hw==", + "license": "MIT", "dependencies": { "debug": "2.6.9", "depd": "2.0.0", @@ -1085,20 +1384,31 @@ "node": ">= 0.8.0" } }, + "node_modules/send/node_modules/encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/send/node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", - "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" }, "node_modules/serve-static": { - "version": "1.15.0", - "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.15.0.tgz", - "integrity": "sha512-XGuRDNjXUijsUL0vl6nSD7cwURuzEgglbOaFuZM9g3kwDXOWVTck0jLzjPzGD+TazWbboZYu52/9/XPdUgne9g==", + "version": "1.16.2", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.16.2.tgz", + "integrity": "sha512-VqpjJZKadQB/PEbEwvFdO43Ax5dFBZ2UECszz8bQ7pi7wt//PWe1P6MN7eCnjsatYtBT6EuiClbjSWP2WrIoTw==", + "license": "MIT", "dependencies": { - "encodeurl": "~1.0.2", + "encodeurl": "~2.0.0", "escape-html": "~1.0.3", "parseurl": "~1.3.3", - "send": "0.18.0" + "send": "0.19.0" }, "engines": { "node": ">= 0.8.0" @@ -1107,16 +1417,76 @@ "node_modules/setprototypeof": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", - "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==", + "license": "ISC" }, "node_modules/side-channel": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz", - "integrity": "sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.0.tgz", + "integrity": "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.3", + "side-channel-list": "^1.0.0", + "side-channel-map": "^1.0.1", + "side-channel-weakmap": "^1.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-list": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.0.tgz", + "integrity": "sha512-FCLHtRD/gnpCiCHEiJLOwdmFP+wzCmDEkc9y7NsYxeF4u7Btsn1ZuwgwJGxImImHicJArLP4R0yX4c2KCrMrTA==", + "license": "MIT", "dependencies": { - "call-bind": "^1.0.0", - "get-intrinsic": "^1.0.2", - "object-inspect": "^1.9.0" + "es-errors": "^1.3.0", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-map": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz", + "integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-weakmap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz", + "integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3", + "side-channel-map": "^1.0.1" + }, + "engines": { + "node": ">= 0.4" }, "funding": { "url": "https://github.com/sponsors/ljharb" @@ -1152,6 +1522,7 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", "integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==", + "license": "MIT", "engines": { "node": ">= 0.8" } @@ -1182,6 +1553,7 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==", + "license": "MIT", "engines": { "node": ">=0.6" } @@ -1213,6 +1585,7 @@ "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "license": "MIT", "dependencies": { "media-typer": "0.3.0", "mime-types": "~2.1.24" @@ -1226,10 +1599,17 @@ "resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.5.tgz", "integrity": "sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA==" }, + "node_modules/undici-types": { + "version": "7.16.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.16.0.tgz", + "integrity": "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==", + "license": "MIT" + }, "node_modules/unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==", + "license": "MIT", "engines": { "node": ">= 0.8" } diff --git a/server/package.json b/server/package.json index 13a9dd05..5d54e91e 100644 --- a/server/package.json +++ b/server/package.json @@ -18,10 +18,13 @@ }, "homepage": "https://github.com/ironhack-labs/cohort-tools-project#readme", "dependencies": { + "bcryptjs": "^3.0.2", "cookie-parser": "^1.4.6", "cors": "^2.8.5", - "express": "^4.18.2", - "mongoose": "^8.19.1", + "express": "^4.21.2", + "express-jwt": "^8.5.1", + "jsonwebtoken": "^9.0.2", + "mongoose": "^8.19.2", "morgan": "^1.10.0", "nodemon": "^3.0.1" } diff --git a/server/routes/auth.routes.js b/server/routes/auth.routes.js new file mode 100644 index 00000000..662aac75 --- /dev/null +++ b/server/routes/auth.routes.js @@ -0,0 +1,97 @@ +const express = require("express") +const bcrypt = require("bcryptjs") +const jwt = require("jsonwebtoken") +const User = require("../models/User.model") +const { isAuthenticated } = require("./../middleware/jwt.middleware.js") +const router = express.Router() +const saltRounds = 10; + +//POST /auth/signup - Creates new user +router.post("/signup", (req, res, next) => { + const { email, password, name } = req.body; + + if (email === "" || password === "" || name === "") { + res.status(400).json({ message: "Provide email, password and name"}) + return; + } + + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/; + if (!emailRegex.test(email)) { + res.status(400).json({ message: "Provide a valid email address."}); + return; + } + + const passwordRegex = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}/; + if (!passwordRegex.test(password)) { + res.status(400).json({ message: 'Password must have at least 6 characters and contain at least one number, one lowercase and one uppercase letter.' }); + return; + } + + User.findOne({ email }) + .then((foundUser) => { + + if (foundUser) { + res.status(400).json({ message: "User already exists"}) + return; + } + + const salt =bcrypt.genSaltSync(saltRounds); + const hashedPassword = bcrypt.hashSync(password, salt); + return User.create({ email, password: hashedPassword, name}) + + }) + .then((createdUser) => { + const { email, name, _id } = createdUser + const user = { email, name, _id }; + res.status(201).json( { user: user }) + }) + .catch(err => { + console.log(err); + res.status(500).json({ message: "Internal Server Error"}) + }) +}) + +//POST /auth/login +router.post("/login", (req, res, next) => { + const { email, password } = req.body + + if (email === '' || password === '') { + res.status(400).json({ message: "Provide email and password." }); + return; + } + + User.findOne({email}) + .then((foundUser) => { + + if(!foundUser) { + + return; + } + const passwordCorrect = bcrypt.compareSync(password, foundUser.password); + + if (passwordCorrect) { + const { _id, email, name } = foundUser + const payload = { _id, email, name }; + + const authToken = jwt.sign( + payload, + process.env.TOKEN_SECRET, + {algorithm: "HS256", expiresIn: "6h"} + ) + + res.status(200).json({authToken: authToken}) + } else { + res.status(401).json({message: "Unable to authenticate the user"}) + } + }) + .catch(err => res.status(500).json({ message: "Internal Server Error"})) +}) + +//GET /auth/verify +router.get("verify", isAuthenticated, (req, res, next) => { + console.log(`req.payload`, req.payload) + + res.status(200).json(req.payload) +}) + +module.exports = router; \ No newline at end of file diff --git a/server/routes/cohorts.routes.js b/server/routes/cohorts.routes.js new file mode 100644 index 00000000..e69de29b diff --git a/server/routes/students.routes.js b/server/routes/students.routes.js new file mode 100644 index 00000000..943f1d7d --- /dev/null +++ b/server/routes/students.routes.js @@ -0,0 +1,83 @@ +const express = require('express') +const router = express.Router() + +const Student = require('../models/Student.model') + + +//STUDENTS +router.get("/", (req, res) => { + Student.find({}) + .then(students => { + console.log("Retrieved students:", students) + res.status(200).json(students); + }) + .catch(error => { + console.error("Error while retrieving students data", error) + res.status(500).json({ error: "Failed to retrieve students data"}) + }) +}); + +router.post("/", (req, res) => { + Student.create(req.body) + .then(createdStudent => { + console.log("Created student:", createdStudent) + res.status(200).json(createdStudent); + }) + .catch(error => { + console.error("Error while creating student", error) + res.status(500).json({ error: "Failed to create student"}) + }) +}); + +//Get all students of a given cohort +router.get("/cohort/:cohortId", (req, res) => { + Student.find({ cohort: req.params.cohortId }) + .populate("cohort") + .then(students => { + console.log("Retrieved students:", students) + res.status(200).json(students); + }) + .catch(error => { + console.error("Error while retrieving students of cohort.", error) + res.status(500).json({ error: "Failed to retrieve students of cohort."}) + }) +}); + +//Get specific student by Id +router.get("/:studentId", (req, res) => { + Student.findById(req.params.studentId) + .then(student => { + console.log("Retrieved student:", student) + res.status(200).json(student); + }) + .catch(error => { + console.error("Error while retrieving students data", error) + res.status(500).json({ error: "Failed to retrieve students data"}) + }) +}); + +router.put("/:studentId", (req, res) => { + Student.findByIdAndUpdate(req.params.studentId, req.body, { new: true }) + .then(updatedStudent => { + console.log("Updated student", updatedStudent) + res.status(200).json(updatedStudent) + }) + .catch(error => { + console.error("Error updating student", error) + res.status(500).json( { error: "Error updating student."}) + }) +}) + +router.delete("/:studentId", (req, res) => { + Student.findByIdAndDelete(req.params.studentId) + .then(result => { + console.log("Student deleted") + res.status(204).send(); + }) + .catch(error => { + console.error("Error deleting student", error) + res.status(500).json( {error: "Error deleting student"}) + }) +}) + +module.exports = router; \ No newline at end of file From edb558acafe69e0df4d2acc5aac98673e0465d31 Mon Sep 17 00:00:00 2001 From: Barbara Goldbeck Date: Fri, 24 Oct 2025 12:26:25 +0200 Subject: [PATCH 5/6] finished day 4 and 5,but isAuthenticated does not work yet --- server/.env.sample | 2 - server/app.js | 76 +++++------------------------ server/middleware/jwt.middleware.js | 3 +- server/package-lock.json | 13 +++++ server/package.json | 1 + server/routes/auth.routes.js | 6 ++- server/routes/cohorts.routes.js | 61 +++++++++++++++++++++++ server/routes/students.routes.js | 15 +++--- server/routes/user.routes.js | 20 ++++++++ 9 files changed, 121 insertions(+), 76 deletions(-) delete mode 100644 server/.env.sample create mode 100644 server/routes/user.routes.js diff --git a/server/.env.sample b/server/.env.sample deleted file mode 100644 index 03bdc5af..00000000 --- a/server/.env.sample +++ /dev/null @@ -1,2 +0,0 @@ -PORT=5005 -TOKEN_SECRET=3B4b0Ba8 \ No newline at end of file diff --git a/server/app.js b/server/app.js index 27ab1f00..ae1167af 100644 --- a/server/app.js +++ b/server/app.js @@ -1,13 +1,14 @@ +require("dotenv/config"); + + const express = require("express"); const morgan = require("morgan"); const cookieParser = require("cookie-parser"); const PORT = 5005; const cors = require("cors") const mongoose = require("mongoose") -const Student = require("./models/Student.model.js") -const Cohort = require("./models/Cohort.model.js") const { errorHandler, notFoundHandler } = require("./error-handling/index.js") -const { isAuthenticated } = require("./middleware/jwt.middleware.js") + mongoose .connect('mongodb://127.0.0.1:27017/cohorts-tools-api') @@ -40,72 +41,19 @@ app.get("/docs", (req, res) => { res.sendFile(__dirname + "/views/docs.html"); }); -//COHORTS -app.get("/api/cohorts", (req, res) => { - Cohort.find({}) - .then(cohorts => { - console.log("Retrieved cohorts:", cohorts) - res.status(200).json(cohorts); - }) - .catch(error => { - console.error("Error while retrieving cohorts data", error) - res.status(500).json({ error: "Failed to retrieve cohorts data"}) - }); -}); - -app.post("/api/cohorts", (req, res) => { - Cohort.create(req.body) - .then(createdCohort => { - console.log("Cohort created", createdCohort) - res.status(201).json(createdCohort) - }) - .catch(error => { - console.error("Error creating cohort", error) - res.status(500).json( { error: "Failed to create cohort"}) - }) -}) - -app.get("/api/cohorts/:cohortId", (req, res) => { - Cohort.findById(req.params.cohortId) - .then(cohort => { - console.log("Single cohort", cohort) - res.status(200).json(cohort) - }) -}) - -app.put("/api/cohorts/:cohortId", (req, res) => { - Cohort.findByIdAndUpdate(req.params.cohortId, req.body, { new: true }) - .then(updatedCohort => { - console.log("Updated cohort", updatedCohort) - res.status(200).json(updatedCohort) - }) - .catch(error => { - console.error("Error updating cohort", error) - res.status(500).json( { error: "Error updating cohort."}) - }) -}) - -app.delete("/api/cohorts/:cohortId", (req, res) => { - Cohort.findByIdAndDelete(req.params.cohortId) - .then(result => { - console.log("Cohort deleted") - res.status(204).send(); - }) - .catch(error => { - console.error("Error deleting cohort", error) - res.status(500).json( {error: "Error deleting cohort"}) - }) -}) - - - - const studentsRouter = require('./routes/students.routes.js') app.use("/api/students", studentsRouter) -const authRouter = require("./routes/auth.routes"); +const cohortsRouter = require('./routes/cohorts.routes.js') +app.use("/api/cohorts", cohortsRouter) + +const authRouter = require("./routes/auth.routes.js"); app.use("/auth", authRouter) +const usersRouter = require("./routes/user.routes.js"); +app.use("/api/users", usersRouter); + +//Not Found Error Handler at the end of all routes! app.use(notFoundHandler); diff --git a/server/middleware/jwt.middleware.js b/server/middleware/jwt.middleware.js index 590666c4..961da876 100644 --- a/server/middleware/jwt.middleware.js +++ b/server/middleware/jwt.middleware.js @@ -3,12 +3,13 @@ const jwt = require("jsonwebtoken"); const isAuthenticated = (req, res, next) => { try{ const token = req.headers.authorization.split(" ")[1]; + console.log("Authorization header:", req.headers.authorization) const payload = jwt.verify(token, process.env.TOKEN_SECRET); req.payload = payload; next() } catch (error) { - res.status(401).json("Token not provided or not valid") + res.status(401).json({ message: "Token not provided or not valid" } ) } } diff --git a/server/package-lock.json b/server/package-lock.json index a66bc4d5..7d9e9993 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -12,6 +12,7 @@ "bcryptjs": "^3.0.2", "cookie-parser": "^1.4.6", "cors": "^2.8.5", + "dotenv": "^17.2.3", "express": "^4.21.2", "express-jwt": "^8.5.1", "jsonwebtoken": "^9.0.2", @@ -353,6 +354,18 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/dotenv": { + "version": "17.2.3", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.2.3.tgz", + "integrity": "sha512-JVUnt+DUIzu87TABbhPmNfVdBDt18BLOWjMUFJMSi/Qqg7NTYtabbvSNJGOJ7afbRuv9D/lngizHtP7QyLQ+9w==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/dunder-proto": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", diff --git a/server/package.json b/server/package.json index 5d54e91e..12103541 100644 --- a/server/package.json +++ b/server/package.json @@ -21,6 +21,7 @@ "bcryptjs": "^3.0.2", "cookie-parser": "^1.4.6", "cors": "^2.8.5", + "dotenv": "^17.2.3", "express": "^4.21.2", "express-jwt": "^8.5.1", "jsonwebtoken": "^9.0.2", diff --git a/server/routes/auth.routes.js b/server/routes/auth.routes.js index 662aac75..156cffb1 100644 --- a/server/routes/auth.routes.js +++ b/server/routes/auth.routes.js @@ -64,7 +64,7 @@ router.post("/login", (req, res, next) => { .then((foundUser) => { if(!foundUser) { - + res.status(400).json({ message: "User not found!" }) return; } const passwordCorrect = bcrypt.compareSync(password, foundUser.password); @@ -88,10 +88,12 @@ router.post("/login", (req, res, next) => { }) //GET /auth/verify -router.get("verify", isAuthenticated, (req, res, next) => { +router.get("/verify", isAuthenticated, (req, res, next) => { + //console for development only - remove it in production console.log(`req.payload`, req.payload) res.status(200).json(req.payload) }) + module.exports = router; \ No newline at end of file diff --git a/server/routes/cohorts.routes.js b/server/routes/cohorts.routes.js index e69de29b..7c7ce82a 100644 --- a/server/routes/cohorts.routes.js +++ b/server/routes/cohorts.routes.js @@ -0,0 +1,61 @@ +const express = require('express') +const router = express.Router() +const Cohort = require("../models/Cohort.model"); + +router.get("/", (req, res) => { + Cohort.find({}) + .then(cohorts => { + console.log("Retrieved cohorts:", cohorts) + res.status(200).json(cohorts); + }) + .catch(error => { + console.error("Error while retrieving cohorts data", error) + res.status(500).json({ error: "Failed to retrieve cohorts data"}) + }); +}); + +router.post("/", (req, res) => { + Cohort.create(req.body) + .then(createdCohort => { + console.log("Cohort created", createdCohort) + res.status(201).json(createdCohort) + }) + .catch(error => { + console.error("Error creating cohort", error) + res.status(500).json( { error: "Failed to create cohort"}) + }) +}) + +router.get("/:cohortId", (req, res) => { + Cohort.findById(req.params.cohortId) + .then(cohort => { + console.log("Single cohort", cohort) + res.status(200).json(cohort) + }) +}) + +router.put("/:cohortId", (req, res) => { + Cohort.findByIdAndUpdate(req.params.cohortId, req.body, { new: true }) + .then(updatedCohort => { + console.log("Updated cohort", updatedCohort) + res.status(200).json(updatedCohort) + }) + .catch(error => { + console.error("Error updating cohort", error) + res.status(500).json( { error: "Error updating cohort."}) + }) +}) + +router.delete("/:cohortId", (req, res) => { + Cohort.findByIdAndDelete(req.params.cohortId) + .then(result => { + console.log("Cohort deleted") + res.status(204).send(); + }) + .catch(error => { + console.error("Error deleting cohort", error) + res.status(500).json( {error: "Error deleting cohort"}) + }) +}) + +module.exports = router; \ No newline at end of file diff --git a/server/routes/students.routes.js b/server/routes/students.routes.js index 943f1d7d..e29131bd 100644 --- a/server/routes/students.routes.js +++ b/server/routes/students.routes.js @@ -1,11 +1,12 @@ const express = require('express') const router = express.Router() -const Student = require('../models/Student.model') +const Student = require('../models/Student.model'); +const { isAuthenticated } = require('../middleware/jwt.middleware'); //STUDENTS -router.get("/", (req, res) => { +router.get("/", (req, res) => { Student.find({}) .then(students => { console.log("Retrieved students:", students) @@ -17,7 +18,7 @@ router.get("/", (req, res) => { }) }); -router.post("/", (req, res) => { +router.post("/", (req, res) => { Student.create(req.body) .then(createdStudent => { console.log("Created student:", createdStudent) @@ -30,7 +31,7 @@ router.post("/", (req, res) => { }); //Get all students of a given cohort -router.get("/cohort/:cohortId", (req, res) => { +router.get("/cohort/:cohortId", (req, res) => { Student.find({ cohort: req.params.cohortId }) .populate("cohort") .then(students => { @@ -44,7 +45,7 @@ router.get("/cohort/:cohortId", (req, res) => { }); //Get specific student by Id -router.get("/:studentId", (req, res) => { +router.get("/:studentId", (req, res) => { Student.findById(req.params.studentId) .then(student => { console.log("Retrieved student:", student) @@ -56,7 +57,7 @@ router.get("/:studentId", (req, res) => { }) }); -router.put("/:studentId", (req, res) => { +router.put("/:studentId", (req, res) => { Student.findByIdAndUpdate(req.params.studentId, req.body, { new: true }) .then(updatedStudent => { console.log("Updated student", updatedStudent) @@ -68,7 +69,7 @@ router.put("/:studentId", (req, res) => { }) }) -router.delete("/:studentId", (req, res) => { +router.delete("/:studentId", (req, res) => { Student.findByIdAndDelete(req.params.studentId) .then(result => { console.log("Student deleted") diff --git a/server/routes/user.routes.js b/server/routes/user.routes.js new file mode 100644 index 00000000..b7aeaa0d --- /dev/null +++ b/server/routes/user.routes.js @@ -0,0 +1,20 @@ +const express = require("express") +const User = require("../models/User.model") +const { isAuthenticated } = require("./../middleware/jwt.middleware.js") +const router = express.Router() + +//GET UserRoute + +router.get("/:id", (req, res, next) => { +User.findById(req.params.id) + +.then(user => { + console.log("Found user", user) + res.status(200).json(user) +}) +.catch(err => { + res.status(500).json({ message: "Could not find User"}) +}) +}) + +module.exports = router; \ No newline at end of file From 23ad9dbd56e9c137bef4e67d04b540168a0f92bd Mon Sep 17 00:00:00 2001 From: Barbara Goldbeck Date: Fri, 24 Oct 2025 12:37:34 +0200 Subject: [PATCH 6/6] commented concole log --- server/middleware/jwt.middleware.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/middleware/jwt.middleware.js b/server/middleware/jwt.middleware.js index 961da876..3b25ce91 100644 --- a/server/middleware/jwt.middleware.js +++ b/server/middleware/jwt.middleware.js @@ -3,7 +3,7 @@ const jwt = require("jsonwebtoken"); const isAuthenticated = (req, res, next) => { try{ const token = req.headers.authorization.split(" ")[1]; - console.log("Authorization header:", req.headers.authorization) + //console.log("Authorization header:", req.headers.authorization) const payload = jwt.verify(token, process.env.TOKEN_SECRET); req.payload = payload;