diff --git a/.env b/.env index 4c3999f6857a2e69befb578812bb836e0c750976..0309843d9a87c1d5161df2a38a6cd7765fcd637e 100644 --- a/.env +++ b/.env @@ -1,4 +1,2 @@ PORT= 3001 -MONGODB_URI=mongodb+srv://username:nYQUR5VHoYFLiK8L@notification-service.vzvnjoq.mongodb.net/?retryWrites=true&w=majority -DB_NAME=notificationapp -NAME=notification-service \ No newline at end of file +JWT_SECRET=pZnf1tK5NNWfgv6NlWpHVCk/+pFQxIzIdKec8JTn+GA= \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index c716d8873b71502000de58c52c9a86234a796089..9fd8bda0bc6dc53d7a5a6224ca8f8bd85a45becb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ FROM node:alpine + +EXPOSE 3001 WORKDIR /usr/src/app COPY package*.json ./ RUN npm ci diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 422e9e3a346ba84e04ac5209e67e8b0841baa782..0000000000000000000000000000000000000000 --- a/docker-compose.yml +++ /dev/null @@ -1,21 +0,0 @@ -version: '3.9' - -services: - # Node API service - api: - build: . - ports: - - 4000:3001 - environment: - - PORT=3001 - - DB_URL=mongodb+srv://username:nYQUR5VHoYFLiK8L@notification-service.vzvnjoq.mongodb.net/?retryWrites=true&w=majority - - - - - - - - - - diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json index 5bc69aba3d4f0e1e3c105b17450acc3194a8f909..a1d822bb9e29a7cb3ef146814b6e4617a9b22e99 100644 --- a/node_modules/.package-lock.json +++ b/node_modules/.package-lock.json @@ -348,6 +348,19 @@ "node": ">= 0.6" } }, + "node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", @@ -494,6 +507,14 @@ "node": ">=0.12.0" } }, + "node_modules/jwt-simple": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/jwt-simple/-/jwt-simple-0.5.6.tgz", + "integrity": "sha512-40aUybvhH9t2h71ncA1/1SbtTNCVZHgsTsTgqPUxGWDmUDrXyDf2wMNQKEbdBjbf4AI+fQhbECNTV6lWxQKUzg==", + "engines": { + "node": ">= 0.4.0" + } + }, "node_modules/kareem": { "version": "2.5.1", "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.5.1.tgz", diff --git a/package-lock.json b/package-lock.json index a39364c73a742ebbabb58b6bc81e320a8c872798..fd7f7957d3873a5aa530d692aa68cfdc45389371 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "dotenv": "^16.0.3", "express": "^4.18.2", + "jwt-simple": "^0.5.6", "mongodb": "^5.3.0", "mongoose": "^7.0.3", "nodemon": "^2.0.22" @@ -519,6 +520,14 @@ "node": ">=0.12.0" } }, + "node_modules/jwt-simple": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/jwt-simple/-/jwt-simple-0.5.6.tgz", + "integrity": "sha512-40aUybvhH9t2h71ncA1/1SbtTNCVZHgsTsTgqPUxGWDmUDrXyDf2wMNQKEbdBjbf4AI+fQhbECNTV6lWxQKUzg==", + "engines": { + "node": ">= 0.4.0" + } + }, "node_modules/kareem": { "version": "2.5.1", "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.5.1.tgz", diff --git a/package.json b/package.json index cadbe89ec98eb96d176d633e0ed1ed717cefd7c2..6a89839ec025c2768a26c44bea80b6fe9fa8f621 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "express": "^4.18.2", "mongodb": "^5.3.0", "mongoose": "^7.0.3", - "nodemon": "^2.0.22" + "nodemon": "^2.0.22", + "jwt-simple": "^0.5.6" } -} +} \ No newline at end of file diff --git a/src/routes/notification.js b/src/routes/notification.js index 0ee451288986fe2436c6f1fcd55010b948dde786..bfc85b4fafc489bc104d8def1b28d6faaa1867b9 100644 --- a/src/routes/notification.js +++ b/src/routes/notification.js @@ -1,7 +1,7 @@ const express = require('express') const router = express.Router() const Notification = require('../schema/schema') - +import { decode } from "jwt-simple"; // Recieve the notifaction // user id, title, description @@ -13,71 +13,98 @@ const Notification = require('../schema/schema') // if the user wants to delete the notifcations // get that repsond - delete the notifcations from the database +const jwtAlgorithm = "HS512"; -router.get("/info/:id", async (req,res) =>{ - // grab the userid - // retrieve all the notifcations stored in the database - try{ - const notifications = await Notification.find({user_id:req.params.id}); - res.status(200).send(notifications); - }catch(error){ - res.status(500).send(error); - } - // send the notifications to the notification page - - }); - - - router.delete("/delete/:id", async (req,res) =>{ - // delete a notification - // grab the notification id - // delete on the database - try{ - const notifications = await Notification.findByIdAndDelete(req.params.id) - if(!notifications){ - return res.status(404).send(); - } - res.send(notifications); - }catch(error){ - res.status(500).send(error); +// retrieve all the notifications for a user that are stored in the database. +router.get("/info", async (req, res) => { + try { + + // Get the JWT secret from the environment variables + const secretKey = process.env.JWT_SECRET; + + // If this is not set we want to throw an error as this is required to retrieve the user + // id from the provided token. + if (secretKey == null) { + console.error('JWT_SECRET is not set in the environment variables'); + return res.status(500).send("JWT_SECRET is not set in the environment variables"); } - }); + // Get the token from the request headers + const token = req.headers.authorization.split(" ")[1]; + + // Decode this token with the secret key + const payload = decode(token, secretKey, false, jwtAlgorithm); + + // Get the user id from the decoded token payload. + const userId = payload.id; + + // Find all the notifications for the user and return them without the user_id and __v fields. + // as this should not be exposed to the user. + const notifications = await Notification.find({ user_id: userId }, { user_id: 0, __v: 0 }); + + + res.status(200).send(notifications); - router.delete("/delete/all/:id", async (req,res) =>{ - // delete all the notifications - // grab the user id - // delete on the database - try{ - const notifications = await Notification.deleteMany({user_id:req.params.id}) - if(!notifications){ - return res.status(404).send(); - } - res.send(notifications); - }catch(error){ - res.status(500).send(error); + } catch (error) { + res.status(500).send(error); + } +}); + + +router.delete("/delete/:id", async (req, res) => { + // delete a notification + // grab the notification id + // delete on the database + try { + const notifications = await Notification.findByIdAndDelete(req.params.id) + if (!notifications) { + return res.status(404).send(); } + res.send(notifications); + } catch (error) { + res.status(500).send(error); + } - }); +}); - - - router.post("/create", async (req,res) => { - //recieve the notifcation info from the user - // send the notifcation to the database (create/save/insert methods) - //const notifications = new Notification(req.body); - - const date = new Date; - const notifications = new Notification({ user_id: req.body['user_id'], title: req.body['title'], description: req.body['description'], date: date }); - - try{ - await notifications.save(); - res.status(201).send(notifications); - } catch (error){ - res.status(400).send(error); +router.delete("/delete/all/:id", async (req, res) => { + // delete all the notifications + // grab the user id + // delete on the database + try { + const notifications = await Notification.deleteMany({ user_id: req.params.id }) + if (!notifications) { + return res.status(404).send(); } - + res.send(notifications); + } catch (error) { + res.status(500).send(error); + } + +}); + + + +router.post("/create", async (req, res) => { + // receive the notification info from the user + // send the notification to the database (create/save/insert methods) + + const date = new Date; + const notifications = new Notification({ + user_id: req.body['user_id'], + title: req.body['title'], + description: req.body['description'], + date: date }); - module.exports = router \ No newline at end of file + try { + await notifications.save(); + res.status(201).send(notifications); + } catch (error) { + res.status(400).send(error); + } + +}); + +module.exports = router \ No newline at end of file