diff --git a/.gitignore b/.gitignore index b6e47617..26df0a2e 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,5 @@ dmypy.json # Pyre type checker .pyre/ + +node_modules/ \ No newline at end of file diff --git a/controllers/Friend.md b/controllers/Friend.md new file mode 100644 index 00000000..2484e903 --- /dev/null +++ b/controllers/Friend.md @@ -0,0 +1,86 @@ +# Friends API + +This is a simple RESTful API for managing friends. It allows you to: + +- GET a list of all friends +- GET a single friend by email ID +- POST a new friend +- PUT update a friend's details +- DELETE a friend + +By default, the API server will run on `http://localhost:5000`. + +## API Reference +``` +``` +### GET a list of all friends + +```http +GET /friends + +curl http://localhost:5000/friends +``` +#### Response: +``` +{ + "johnsmith@gamil.com": { + "firstName": "John", + "lastName": "Doe", + "DOB": "22-12-1990" + }, + "annasmith@gamil.com": { + "firstName": "Anna", + "lastName": "smith", + "DOB": "02-07-1983" + }, + "peterjones@gamil.com": { + "firstName": "Peter", + "lastName": "Jones", + "DOB": "21-03-1989" + } +} +``` +### GET a single friend by email ID +``` +GET /friends/:email + +curl http://localhost:5000/friends/johnsmith@gamil.com +``` +#### Response: +``` +{ + "firstName": "John", + "lastName": "Doe", + "DOB": "22-12-1990" +} +``` +## POST a new friend +``` +POST /friends + +curl -X POST -H "Content-Type: application/json" -d '{"email": "janesmith@gamil.com", "friend": {"firstName": "Jane", "lastName": "Smith", "DOB": "11-05-1995"}}' http://localhost:5000/friends +``` +#### Response: +``` +Friend added successfully +``` +## PUT update a friend's details +``` +PUT /friends/:email + +curl -X PUT -H "Content-Type: application/json" -d '{"friend": {"firstName": "John", "lastName": "Smith", "DOB": "22-12-1990"}}' http://localhost:5000/friends/johnsmith@gamil.com +``` +#### Response: +``` +Friend updated successfully +``` +## DELETE a friend +``` +DELETE /friends/:email + +curl -X DELETE http://localhost:5000/friends/peterjones@gamil.com +``` +#### Response: +``` +Friend deleted successfully +``` diff --git a/controllers/authController.js b/controllers/authController.js new file mode 100644 index 00000000..98c97960 --- /dev/null +++ b/controllers/authController.js @@ -0,0 +1,55 @@ +const jwt = require("jsonwebtoken"); // Importing the jsonwebtoken module +const { doesExist, authenticatedUser } = require("../helpers/auth"); + +const user = require("../models/user"); + +let login = (req, res) => { + const username = req.body.username; + const password = req.body.password; + + if (!username || !password) { + return res.status(400).json({ + message: "Error logging in. Please provide both username and password.", + }); // Using status code 400 for bad request + } + + if (authenticatedUser(username, password)) { + let accessToken = jwt.sign( + { + data: password, + }, + "access", + { expiresIn: 60 * 60 } + ); + + req.session.authorization = { + accessToken, + username, + }; + return res.status(200).send("User successfully logged in"); + } else { + return res.status(401).json({ + message: + "Invalid login credentials. Please check your username and password.", + }); // Using status code 401 for unauthorized + } +}; + +let register = (req, res) => { + const username = req.body.username; + const password = req.body.password; + + if (username && password) { + if (!doesExist(username)) { + user.addUser(username,password); + return res + .status(201) // Using status code 201 for created + .json({ message: "User successfully registered. You can now log in." }); + } else { + return res.status(409).json({ message: "Username already exists." }); // Using status code 409 for conflict + } + } + return res.status(400).json({ message: "Invalid username or password." }); // Using status code 400 for bad request +}; + +module.exports = { login, register }; diff --git a/controllers/friendsController.js b/controllers/friendsController.js new file mode 100644 index 00000000..565a1fc9 --- /dev/null +++ b/controllers/friendsController.js @@ -0,0 +1,90 @@ +const friends = require("../models/friend"); + +// GET request: Retrieve all friends +let getAllFriends = (req, res) => { + res.status(200).send(friends.getAllFriends()); +}; + +// GET by specific ID request: Retrieve a single friend with email ID +let getFriendByEmail = (req, res) => { + const email = req.params.email; + const friend = friends.getFriend(email); + if (friend) { + res.status(200).json(friend); + } else { + res.status(404).send("Friend not found"); + } +}; + +// POST request: Add a new friend +let addFriend = (req, res) => { + const email = req.body.email; + const friend = req.body.friend; + if (!email || !friend) { + res + .status(400) + .send("Please provide both email and friend in request body"); + } else if (!isValidFriend(friend)) { + res + .status(400) + .send( + "Invalid friend object. Please provide a valid friend object with firstName, lastName, and DOB" + ); + } else if (friends.getFriend(email)) { + res.status(409).send("Friend with this email already exists"); + } else { + friends.addFriend(email, friend); + res + .status(201) + .send("The user" + " " + req.body.firstName + " Has been added!"); + } +}; + +// PUT request: Update the details of a friend with email id +let updateFriend = (req, res) => { + const email = req.params.email; + const friend = req.body.friend; + if (!email || !friend) { + res + .status(400) + .send("Please provide both email and friend in request body"); + } else if (!isValidFriend(friend)) { + res + .status(400) + .send( + "Invalid friend object. Please provide a valid friend object with firstName, lastName, and DOB" + ); + } else if (!friends.getFriend(email)) { + res.status(404).send("Friend not found"); + } else { + friends.updateFriend(email, friend); + res.status(200).send(`Friend with the email ${email} updated.`); + } +}; + +// DELETE request: Delete a friend by email id +let deleteFriend = (req, res) => { + const email = req.params.email; + if (friends.getFriend(email)) { + friends.deleteFriend(email); + res.status(200).send(`Friend with the email ${email} deleted.`); + } else { + res.status(404).send("Friend not found"); + } +}; + +// Helper function to validate friend object +function isValidFriend(friend) { + if (!friend.firstName || !friend.lastName || !friend.DOB) { + return false; + } + return true; +} + +module.exports = { + getAllFriends, + getFriendByEmail, + addFriend, + updateFriend, + deleteFriend, +}; diff --git a/helpers/auth.js b/helpers/auth.js new file mode 100644 index 00000000..e176535e --- /dev/null +++ b/helpers/auth.js @@ -0,0 +1,24 @@ +const user = require("../models/user"); + +const doesExist = (username) => { + let userswithsamename = user.getUser(username); + if (userswithsamename.length > 0) { + return true; + } else { + return false; + } +}; + +const authenticatedUser = (username, password) => { + const users = user.getAllUsers(); + let validusers = users.filter((user) => { + return user.username === username && user.password === password; + }); + if (validusers.length > 0) { + return true; + } else { + return false; + } +}; + +module.exports = { doesExist, authenticatedUser }; diff --git a/index.js b/index.js index 168c0532..c01bdcdd 100644 --- a/index.js +++ b/index.js @@ -1,95 +1,25 @@ const express = require('express'); -const jwt = require('jsonwebtoken'); const session = require('express-session') -const routes = require('./router/friends.js') - -let users = [] - -const doesExist = (username)=>{ - let userswithsamename = users.filter((user)=>{ - return user.username === username - }); - if(userswithsamename.length > 0){ - return true; - } else { - return false; - } -} - -const authenticatedUser = (username,password)=>{ - let validusers = users.filter((user)=>{ - return (user.username === username && user.password === password) - }); - if(validusers.length > 0){ - return true; - } else { - return false; - } -} +const routes = require("./routes"); +const authMiddleware = require("./middleware/authMiddleware"); const app = express(); -app.use(session({secret:"fingerpint"},resave=true,saveUninitialized=true)); +// This code sets up session management using the Express session middleware +app.use( + session({ + secret: "fingerpint", + resave: true, + saveUninitialized: true, + }) +); app.use(express.json()); -app.use("/friends", function auth(req,res,next){ - if(req.session.authorization) { - token = req.session.authorization['accessToken']; - jwt.verify(token, "access",(err,user)=>{ - if(!err){ - req.user = user; - next(); - } - else{ - return res.status(403).json({message: "User not authenticated"}) - } - }); - } else { - return res.status(403).json({message: "User not logged in"}) - } -}); - -app.post("/login", (req,res) => { - const username = req.body.username; - const password = req.body.password; - - if (!username || !password) { - return res.status(404).json({message: "Error logging in"}); - } - - if (authenticatedUser(username,password)) { - let accessToken = jwt.sign({ - data: password - }, 'access', { expiresIn: 60 * 60 }); - - req.session.authorization = { - accessToken,username - } - return res.status(200).send("User successfully logged in"); - } else { - return res.status(208).json({message: "Invalid Login. Check username and password"}); - } -}); - -app.post("/register", (req,res) => { - const username = req.body.username; - const password = req.body.password; - - if (username && password) { - if (!doesExist(username)) { - users.push({"username":username,"password":password}); - return res.status(200).json({message: "User successfully registred. Now you can login"}); - } else { - return res.status(404).json({message: "User already exists!"}); - } - } - return res.status(404).json({message: "Unable to register user."}); -}); - +app.use("/friends", authMiddleware); const PORT =5000; -app.use("/friends", routes); +app.use("/", routes); app.listen(PORT,()=>console.log("Server is running")); diff --git a/middleware/authMiddleware.js b/middleware/authMiddleware.js new file mode 100644 index 00000000..7c22d33f --- /dev/null +++ b/middleware/authMiddleware.js @@ -0,0 +1,19 @@ +const jwt = require("jsonwebtoken"); + +function auth(req, res, next) { + if (req.session.authorization) { + const token = req.session.authorization.accessToken; + jwt.verify(token, "access", (err, user) => { + if (!err) { + req.user = user; + next(); + } else { + return res.status(403).json({ message: "User not authenticated" }); + } + }); + } else { + return res.status(403).json({ message: "User not logged in" }); + } +} + +module.exports = auth; diff --git a/models/friend.js b/models/friend.js new file mode 100644 index 00000000..197b8e28 --- /dev/null +++ b/models/friend.js @@ -0,0 +1,51 @@ +let friends = { + "johnsmith@gmail.com": { + firstName: "John", + lastName: "Doe", + DOB: "22-12-1990", + }, + "annasmith@gmail.com": { + firstName: "Anna", + lastName: "smith", + DOB: "02-07-1983", + }, + "peterjones@gmail.com": { + firstName: "Peter", + lastName: "Jones", + DOB: "21-03-1989", + }, +}; + +module.exports = { + // CREATE + addFriend: function (email, firstName, lastName, DOB) { + friends[email] = { + firstName: firstName, + lastName: lastName, + DOB: DOB, + }; + }, + + // READ + getFriend: function (email) { + return friends[email]; + }, + + getAllFriends: function () { + return friends; + }, + + // UPDATE + updateFriend: function (email, firstName, lastName, DOB) { + if (friends[email]) { + friends[email].firstName = firstName; + friends[email].lastName = lastName; + friends[email].DOB = DOB; + } + }, + + // DELETE + deleteFriend: function (email) { + delete friends[email]; + }, +}; diff --git a/models/user.js b/models/user.js new file mode 100644 index 00000000..34aff824 --- /dev/null +++ b/models/user.js @@ -0,0 +1,38 @@ +let users = [ + { + username: "user2", + password: "password2", + }, +]; + +module.exports = { + // CREATE + addUser: function (username, password) { + users.push({ username: username, password: password }); + }, + + // READ + getUser: function (username) { + return users.filter((user) => { + return user.username === username; + }); + }, + + getAllUsers: function () { + return users; + }, + + // UPDATE + updateUser: function (username, newUsername, newPassword) { + const userIndex = users.findIndex((user) => user.username === username); + if (userIndex !== -1) { + users[userIndex].username = newUsername; + users[userIndex].password = newPassword; + } + }, + + // DELETE + deleteUser: function (username) { + users = users.filter((user) => user.username !== username); + }, +}; diff --git a/package-lock.json b/package-lock.json index e8471ed4..317b92fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "express": "^4.18.1", "express-session": "^1.17.3", + "jsonwebtoken": "^8.5.1", "nodemon": "^2.0.19" } }, @@ -104,6 +105,11 @@ "node": ">=8" } }, + "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==" + }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -212,6 +218,14 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "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==", + "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", @@ -503,6 +517,86 @@ "node": ">=0.12.0" } }, + "node_modules/jsonwebtoken": { + "version": "8.5.1", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz", + "integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==", + "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": "^5.6.0" + }, + "engines": { + "node": ">=4", + "npm": ">=1.4.28" + } + }, + "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==" + }, + "node_modules/jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "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==", + "dependencies": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, + "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==" + }, + "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==" + }, + "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==" + }, + "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==" + }, + "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==" + }, + "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==" + }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==" + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -1052,6 +1146,11 @@ "fill-range": "^7.0.1" } }, + "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==" + }, "bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -1127,6 +1226,14 @@ "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==" }, + "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==", + "requires": { + "safe-buffer": "^5.0.1" + } + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -1347,6 +1454,84 @@ "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==" }, + "jsonwebtoken": { + "version": "8.5.1", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz", + "integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==", + "requires": { + "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": "^5.6.0" + }, + "dependencies": { + "ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + } + } + }, + "jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "requires": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "requires": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, + "lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==" + }, + "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==" + }, + "lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==" + }, + "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==" + }, + "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==" + }, + "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==" + }, + "lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==" + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", diff --git a/router/friends.js b/router/friends.js deleted file mode 100644 index 0b696b8d..00000000 --- a/router/friends.js +++ /dev/null @@ -1,47 +0,0 @@ -const express = require('express'); - -const router = express.Router(); - -let friends = { - "johnsmith@gamil.com": {"firstName": "John","lastName": "Doe","DOB":"22-12-1990"}, - "annasmith@gamil.com":{"firstName": "Anna","lastName": "smith","DOB":"02-07-1983"}, - "peterjones@gamil.com":{"firstName": "Peter","lastName": "Jones","DOB":"21-03-1989"} -}; - - -// GET request: Retrieve all friends -router.get("/",(req,res)=>{ - - // Update the code here - - res.send("Yet to be implemented")//This line is to be replaced with actual return value -}); - -// GET by specific ID request: Retrieve a single friend with email ID -router.get("/:email",(req,res)=>{ - // Update the code here - res.send("Yet to be implemented")//This line is to be replaced with actual return value -}); - - -// POST request: Add a new friend -router.post("/",(req,res)=>{ - // Update the code here - res.send("Yet to be implemented")//This line is to be replaced with actual return value -}); - - -// PUT request: Update the details of a friend with email id -router.put("/:email", (req, res) => { - // Update the code here - res.send("Yet to be implemented")//This line is to be replaced with actual return value -}); - - -// DELETE request: Delete a friend by email id -router.delete("/:email", (req, res) => { - // Update the code here - res.send("Yet to be implemented")//This line is to be replaced with actual return value -}); - -module.exports=router; diff --git a/routes/auth.js b/routes/auth.js new file mode 100644 index 00000000..e13954b5 --- /dev/null +++ b/routes/auth.js @@ -0,0 +1,12 @@ +const express = require("express"); +const router = express.Router(); + +const {login,register} = require("../controllers/authController"); + +// POST request to log in user +router.post("/login", login); + +// POST request to register new user +router.post("/register", register); + +module.exports = router; diff --git a/routes/friends.js b/routes/friends.js new file mode 100644 index 00000000..9ce8eea2 --- /dev/null +++ b/routes/friends.js @@ -0,0 +1,18 @@ +const express = require("express"); +const router = express.Router(); + +const { + getAllFriends, + getFriendByEmail, + addFriend, + updateFriend, + deleteFriend, +} = require("../controllers/friendsController"); + +router.get("/", getAllFriends); +router.get("/:email", getFriendByEmail); +router.post("/", addFriend); +router.put("/:email", updateFriend); +router.delete("/:email", deleteFriend); + +module.exports = router; diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 00000000..f771eef9 --- /dev/null +++ b/routes/index.js @@ -0,0 +1,13 @@ +const express = require("express"); +const router = express.Router(); +const friendsRoutes = require("./friends"); +const authController = require("./auth"); + +router.use("/friends", friendsRoutes); +router.use("/auth", authController); + +router.get("/", (req, res) => { + res.send("Welcome to my app!"); +}); + +module.exports = router; \ No newline at end of file