From 1aa26471e88a641dd6a110ed9acbcdd48caacaa1 Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Mon, 5 Feb 2024 06:01:44 +0200 Subject: [PATCH 01/19] Added register controller and JWT authentication for login --- package-lock.json | 663 ++++++++++++++++++++++++++++++++++++++-- package.json | 3 + requests.rest | 22 ++ src/app.js | 18 +- src/controllers/user.js | 98 ++++-- src/routes/user.js | 8 +- 6 files changed, 757 insertions(+), 55 deletions(-) create mode 100644 requests.rest diff --git a/package-lock.json b/package-lock.json index 61fb92a..4f5c532 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,13 +10,49 @@ "license": "ISC", "dependencies": { "@prisma/client": "^5.8.1", + "bcrypt": "^5.1.1", "body-parser": "^1.20.2", + "cookie-parser": "^1.4.6", "dotenv": "^16.4.1", "express": "^4.18.2", + "jsonwebtoken": "^9.0.2", "nodemon": "^3.0.3", "prisma": "^5.8.1" } }, + "node_modules/@mapbox/node-pre-gyp": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@mapbox/node-pre-gyp/-/node-pre-gyp-1.0.11.tgz", + "integrity": "sha512-Yhlar6v9WQgUp/He7BdgzOz8lqMQ8sU+jkCq7Wx8Myc5YFJLbEe7lgui/V7G1qB1DJykHSGwreceSaD60Y0PUQ==", + "dependencies": { + "detect-libc": "^2.0.0", + "https-proxy-agent": "^5.0.0", + "make-dir": "^3.1.0", + "node-fetch": "^2.6.7", + "nopt": "^5.0.0", + "npmlog": "^5.0.1", + "rimraf": "^3.0.2", + "semver": "^7.3.5", + "tar": "^6.1.11" + }, + "bin": { + "node-pre-gyp": "bin/node-pre-gyp" + } + }, + "node_modules/@mapbox/node-pre-gyp/node_modules/nopt": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz", + "integrity": "sha512-Tbj67rffqceeLpcRXrT7vKAN8CwfPeIBgM7E6iBkmKLV7bEMwpGgYLGv0jACUsECaa/vuxP0IjEont6umdMgtQ==", + "dependencies": { + "abbrev": "1" + }, + "bin": { + "nopt": "bin/nopt.js" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/@prisma/client": { "version": "5.8.1", "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.8.1.tgz", @@ -91,6 +127,46 @@ "node": ">= 0.6" } }, + "node_modules/agent-base": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", + "integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==", + "dependencies": { + "debug": "4" + }, + "engines": { + "node": ">= 6.0.0" + } + }, + "node_modules/agent-base/node_modules/debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/agent-base/node_modules/ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + }, + "node_modules/ansi-regex": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", + "engines": { + "node": ">=8" + } + }, "node_modules/anymatch": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", @@ -103,6 +179,23 @@ "node": ">= 8" } }, + "node_modules/aproba": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/aproba/-/aproba-2.0.0.tgz", + "integrity": "sha512-lYe4Gx7QT+MKGbDsA+Z+he/Wtef0BiwDOlK/XkBrdfsh9J/jPPXbX0tE9x9cl27Tmu5gg3QUbUrQYa/y+KOHPQ==" + }, + "node_modules/are-we-there-yet": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/are-we-there-yet/-/are-we-there-yet-2.0.0.tgz", + "integrity": "sha512-Ci/qENmwHnsYo9xKIcUJN5LeDKdJ6R1Z1j9V/J5wyq8nh/mYPEpIKJbBZXtZjG04HiK7zV/p6Vs9952MrMeUIw==", + "dependencies": { + "delegates": "^1.0.0", + "readable-stream": "^3.6.0" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", @@ -113,6 +206,19 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, + "node_modules/bcrypt": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-5.1.1.tgz", + "integrity": "sha512-AGBHOG5hPYZ5Xl9KXzU5iKq9516yEmvCKDg3ecP5kX2aB6UqTeXZxk2ELnDgDm6BQSMlLt9rDB4LoSMx0rYwww==", + "hasInstallScript": true, + "dependencies": { + "@mapbox/node-pre-gyp": "^1.0.11", + "node-addon-api": "^5.0.0" + }, + "engines": { + "node": ">= 10.0.0" + } + }, "node_modules/binary-extensions": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", @@ -144,6 +250,15 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, "node_modules/braces": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", @@ -155,6 +270,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", @@ -213,11 +333,32 @@ "node": ">= 6" } }, + "node_modules/chownr": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/chownr/-/chownr-2.0.0.tgz", + "integrity": "sha512-bIomtDF5KGpdogkLd9VspvFzk9KfpyyGlS8YFVZl7TGPBHL5snIOnxeshwVgPteQ9b4Eydl+pVbIyE1DcvCWgQ==", + "engines": { + "node": ">=10" + } + }, + "node_modules/color-support": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-support/-/color-support-1.1.3.tgz", + "integrity": "sha512-qiBjkpbMLO/HL68y+lh4q0/O1MZFj2RX6X/KmMa3+gJD3z+WwI1ZzDHysvqHGS3mP6mznPckpXmw1nI9cJjyRg==", + "bin": { + "color-support": "bin.js" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" }, + "node_modules/console-control-strings": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz", + "integrity": "sha512-ty/fTekppD2fIwRvnZAVdeOiGd1c7YXEixbgJTNzqcxJWKQnjJ/V1bNEEE6hygpM3WjwHFUVK6HTjWSzV4a8sQ==" + }, "node_modules/content-disposition": { "version": "0.5.4", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", @@ -245,6 +386,26 @@ "node": ">= 0.6" } }, + "node_modules/cookie-parser": { + "version": "1.4.6", + "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.6.tgz", + "integrity": "sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==", + "dependencies": { + "cookie": "0.4.1", + "cookie-signature": "1.0.6" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/cookie-parser/node_modules/cookie": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz", + "integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/cookie-signature": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", @@ -271,6 +432,11 @@ "node": ">= 0.4" } }, + "node_modules/delegates": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", + "integrity": "sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ==" + }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -288,6 +454,14 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/detect-libc": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.2.tgz", + "integrity": "sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==", + "engines": { + "node": ">=8" + } + }, "node_modules/dotenv": { "version": "16.4.1", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.1.tgz", @@ -299,11 +473,24 @@ "url": "https://github.com/motdotla/dotenv?sponsor=1" } }, + "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", "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==" }, + "node_modules/emoji-regex": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==" + }, "node_modules/encodeurl": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", @@ -447,6 +634,33 @@ "node": ">= 0.6" } }, + "node_modules/fs-minipass": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-2.1.0.tgz", + "integrity": "sha512-V/JgOLFCS+R6Vcq0slCuaeWEdNC3ouDlJMNIsacH2VtALiu9mV4LPrHc5cDl8k5aw6J8jwgWWpiTo5RYhmIzvg==", + "dependencies": { + "minipass": "^3.0.0" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/fs-minipass/node_modules/minipass": { + "version": "3.3.6", + "resolved": "https://registry.npmjs.org/minipass/-/minipass-3.3.6.tgz", + "integrity": "sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw==", + "dependencies": { + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==" + }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", @@ -468,6 +682,25 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/gauge": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/gauge/-/gauge-3.0.2.tgz", + "integrity": "sha512-+5J6MS/5XksCuXq++uFRsnUd7Ovu1XenbeuIuNRJxYWjgQbPuFhT14lAvsWfqfAmnwluf1OwMjz39HjfLPci0Q==", + "dependencies": { + "aproba": "^1.0.3 || ^2.0.0", + "color-support": "^1.1.2", + "console-control-strings": "^1.0.0", + "has-unicode": "^2.0.1", + "object-assign": "^4.1.1", + "signal-exit": "^3.0.0", + "string-width": "^4.2.3", + "strip-ansi": "^6.0.1", + "wide-align": "^1.1.2" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/get-intrinsic": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.2.tgz", @@ -482,6 +715,25 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/glob": { + "version": "7.2.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", + "integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==", + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.1.1", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/gopd": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz", @@ -526,6 +778,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/has-unicode": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-2.0.1.tgz", + "integrity": "sha512-8Rf9Y83NBReMnx0gFzA8JImQACstCYWUplepDa9xprwwtmgEZUF0h/i5xSA625zB/I37EtrswSST6OXxwaaIJQ==" + }, "node_modules/hasown": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.0.tgz", @@ -552,6 +809,39 @@ "node": ">= 0.8" } }, + "node_modules/https-proxy-agent": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz", + "integrity": "sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA==", + "dependencies": { + "agent-base": "6", + "debug": "4" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/https-proxy-agent/node_modules/debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/https-proxy-agent/node_modules/ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + }, "node_modules/iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", @@ -568,6 +858,15 @@ "resolved": "https://registry.npmjs.org/ignore-by-default/-/ignore-by-default-1.0.1.tgz", "integrity": "sha512-Ius2VYcGNk7T90CppJqcIkS5ooHUZyIQK+ClZfMfMNFEF9VSE73Fq+906u/CWu92x4gzZMWOwfFYckPObzdEbA==" }, + "node_modules/inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", + "dependencies": { + "once": "^1.3.0", + "wrappy": "1" + } + }, "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", @@ -600,6 +899,14 @@ "node": ">=0.10.0" } }, + "node_modules/is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "engines": { + "node": ">=8" + } + }, "node_modules/is-glob": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", @@ -619,6 +926,108 @@ "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==", + "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==" + }, + "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/make-dir": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz", + "integrity": "sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==", + "dependencies": { + "semver": "^6.0.0" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/make-dir/node_modules/semver": { + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", + "bin": { + "semver": "bin/semver.js" + } + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -670,6 +1079,59 @@ "node": ">= 0.6" } }, + "node_modules/minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, + "node_modules/minipass": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/minipass/-/minipass-5.0.0.tgz", + "integrity": "sha512-3FnjYuehv9k6ovOEbyOswadCDPX1piCfhV8ncmYtHOjuPwylVWsghTLo7rabjC3Rx5xD4HDx8Wm1xnMF7S5qFQ==", + "engines": { + "node": ">=8" + } + }, + "node_modules/minizlib": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/minizlib/-/minizlib-2.1.2.tgz", + "integrity": "sha512-bAxsR8BVfj60DWXHE3u30oHzfl4G7khkSuPW+qvpd7jFRHm7dLxOjUk1EHACJ/hxLY8phGJ0YhYHZo7jil7Qdg==", + "dependencies": { + "minipass": "^3.0.0", + "yallist": "^4.0.0" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/minizlib/node_modules/minipass": { + "version": "3.3.6", + "resolved": "https://registry.npmjs.org/minipass/-/minipass-3.3.6.tgz", + "integrity": "sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw==", + "dependencies": { + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/mkdirp": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", + "bin": { + "mkdirp": "bin/cmd.js" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -683,6 +1145,30 @@ "node": ">= 0.6" } }, + "node_modules/node-addon-api": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-5.1.0.tgz", + "integrity": "sha512-eh0GgfEkpnoWDq+VY8OyvYhFEzBk6jIYbRKdIlyTiAXIVJ8PyBaKb0rp7oDtoddbdoHWhq8wwr+XZ81F1rpNdA==" + }, + "node_modules/node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, "node_modules/nodemon": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.3.tgz", @@ -710,15 +1196,6 @@ "url": "https://opencollective.com/nodemon" } }, - "node_modules/nodemon/node_modules/brace-expansion": { - "version": "1.1.11", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", - "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dependencies": { - "balanced-match": "^1.0.0", - "concat-map": "0.0.1" - } - }, "node_modules/nodemon/node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -743,17 +1220,6 @@ "node": ">=4" } }, - "node_modules/nodemon/node_modules/minimatch": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", - "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", - "dependencies": { - "brace-expansion": "^1.1.7" - }, - "engines": { - "node": "*" - } - }, "node_modules/nodemon/node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", @@ -792,6 +1258,25 @@ "node": ">=0.10.0" } }, + "node_modules/npmlog": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/npmlog/-/npmlog-5.0.1.tgz", + "integrity": "sha512-AqZtDUWOMKs1G/8lwylVjrdYgqA4d9nu8hc+0gzRxlDb1I10+FHBGMXs6aiQHFdCUUlqH99MUMuLfzWDNDtfxw==", + "dependencies": { + "are-we-there-yet": "^2.0.0", + "console-control-strings": "^1.1.0", + "gauge": "^3.0.0", + "set-blocking": "^2.0.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==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-inspect": { "version": "1.13.1", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.1.tgz", @@ -811,6 +1296,14 @@ "node": ">= 0.8" } }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "dependencies": { + "wrappy": "1" + } + }, "node_modules/parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -819,6 +1312,14 @@ "node": ">= 0.8" } }, + "node_modules/path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/path-to-regexp": { "version": "0.1.7", "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", @@ -903,6 +1404,19 @@ "node": ">= 0.8" } }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/readdirp": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", @@ -914,6 +1428,20 @@ "node": ">=8.10.0" } }, + "node_modules/rimraf": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", + "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==", + "dependencies": { + "glob": "^7.1.3" + }, + "bin": { + "rimraf": "bin.js" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -1005,6 +1533,11 @@ "node": ">= 0.8.0" } }, + "node_modules/set-blocking": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", + "integrity": "sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw==" + }, "node_modules/set-function-length": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.2.0.tgz", @@ -1038,6 +1571,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/signal-exit": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz", + "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==" + }, "node_modules/simple-update-notifier": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/simple-update-notifier/-/simple-update-notifier-2.0.0.tgz", @@ -1057,6 +1595,54 @@ "node": ">= 0.8" } }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, + "node_modules/string-width": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "dependencies": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/strip-ansi": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", + "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", + "dependencies": { + "ansi-regex": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/tar": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/tar/-/tar-6.2.0.tgz", + "integrity": "sha512-/Wo7DcT0u5HUV486xg675HtjNd3BXZ6xDbzsCUZPt5iw8bTQ63bP0Raut3mvro9u+CUyq7YQd8Cx55fsZXxqLQ==", + "dependencies": { + "chownr": "^2.0.0", + "fs-minipass": "^2.0.0", + "minipass": "^5.0.0", + "minizlib": "^2.1.1", + "mkdirp": "^1.0.3", + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/to-regex-range": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", @@ -1087,6 +1673,11 @@ "nodetouch": "bin/nodetouch.js" } }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, "node_modules/type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", @@ -1112,6 +1703,11 @@ "node": ">= 0.8" } }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + }, "node_modules/utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", @@ -1128,6 +1724,33 @@ "node": ">= 0.8" } }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, + "node_modules/wide-align": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.5.tgz", + "integrity": "sha512-eDMORYaPNZ4sQIuuYPDHdQvf4gyCF9rEEV/yPxGfwPkRodwEgiMUUXTx/dex+Me0wxx53S+NgUHaP7y3MGlDmg==", + "dependencies": { + "string-width": "^1.0.2 || 2 || 3 || 4" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" + }, "node_modules/yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", diff --git a/package.json b/package.json index 896dea7..4bba0b6 100644 --- a/package.json +++ b/package.json @@ -20,9 +20,12 @@ "type": "module", "dependencies": { "@prisma/client": "^5.8.1", + "bcrypt": "^5.1.1", "body-parser": "^1.20.2", + "cookie-parser": "^1.4.6", "dotenv": "^16.4.1", "express": "^4.18.2", + "jsonwebtoken": "^9.0.2", "nodemon": "^3.0.3", "prisma": "^5.8.1" } diff --git a/requests.rest b/requests.rest new file mode 100644 index 0000000..726e1e9 --- /dev/null +++ b/requests.rest @@ -0,0 +1,22 @@ +POST http://localhost:3000/register +Content-Type: application/json + +{ + "email": "amr10@g.com", + "username": "amr", + "password": "1234" +} + +### + +POST http://localhost:3000/login +Content-Type: application/json + +{ + "email": "amr10@g.com", + "password": "1234" +} + +### + +GET http://localhost:3000/ \ No newline at end of file diff --git a/src/app.js b/src/app.js index 591ca25..75a30ac 100644 --- a/src/app.js +++ b/src/app.js @@ -1,9 +1,8 @@ import express from "express"; -import login from './routes/login.js'; -const bodyParser = require('body-parser'); -const register = require('./routes/user.js'); - - +import bodyParser from "body-parser"; +import register from "./routes/user.js"; +import cookieParser from "cookie-parser"; +import { tokenAuth } from "./controllers/user.js"; const app = express(); const port = 3000; @@ -11,12 +10,11 @@ const port = 3000; app.use(express.json()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); +app.use(cookieParser()); +app.use("/", register); -app.use('/',login); -app.use('/', register); - -app.get("/", (req, res) => { - res.send("Response OK"); +app.get("/", tokenAuth, (req, res) => { + res.send(`Logged in as: ${req.user.email}`); }); app.listen(port, () => { diff --git a/src/controllers/user.js b/src/controllers/user.js index 0c48a54..475d9f1 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -1,26 +1,82 @@ -export const loginController = (req,res) => { - const ref_email = "1234@gmail.com" - const ref_pass = "1234"; - let {email, pass } = req.body; - if (email === ref_email && pass == ref_pass) - { - res.send('true\n'); - } - else{ - res.send('entre valid email and password\n') +import jwt from "jsonwebtoken"; +import { PrismaClient } from "@prisma/client"; +import bcrypt from "bcrypt"; +const prisma = new PrismaClient(); +const secretKey = process.env.SECRET_KEY; + +export const tokenAuth = (req, res, next) => { + const token = req.cookies["access-token"]; + if (!token) { + return res.status(400).send("Unauthenticated access token"); } -} + jwt.verify(token, secretKey, (err, user) => { + if (err) { + return res.status(403).json({ error: err }); + } + req.user = user; + req.authenticated = true; + return next(); + }); +}; +export const loginController = async (req, res) => { + const { email, password } = req.body; + const user = await prisma.user.findUnique({ + where: { + email: email, + }, + }); + if (user === null) { + res.status(400).send("Email does not exist"); + } else { + try { + if (await bcrypt.compare(password, user.password)) { + const token = jwt.sign( + { email: email, password: password }, + secretKey, + { + expiresIn: "1h", + } + ); + res + .cookie("access-token", token, { + maxAge: 3600000, + httpOnly: true, + }) + .json({ accessToken: token }); + } + } catch (err) { + res.status(400).send("Invalid password"); + } finally { + prisma.$disconnect; + } + } +}; -const users = []; -export const registerController = (req, res) => { +export const registerController = async (req, res) => { const { email, username, password } = req.body; - - if (users.find((user) => user.email === email)) { - res.send('Email is already registered.'); + const userExists = await prisma.user.findUnique({ + where: { + email: email, + }, + }); + if (userExists) { + res.send("This Email already exists"); + } else { + const hashedPassword = await bcrypt.hash(password, 10); + try { + await prisma.user.create({ + data: { + email: email, + name: username, + password: hashedPassword, + }, + }); + res.status(201).send("User created successfully"); + } catch { + res.status(500).send(); + } finally { + prisma.$disconnect; + } } - - users.push({ email, username, password }); - - res.status(201).send('User registered successfully.'); -}; \ No newline at end of file +}; diff --git a/src/routes/user.js b/src/routes/user.js index e00f96e..4b97097 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -1,8 +1,8 @@ import express from "express"; -import { loginController, registerController } from '../controllers/user.js'; +import { loginController, registerController } from "../controllers/user.js"; const route = express.Router(); -route.post('/login',loginController); -route.post('/register', registerController); +route.post("/login", loginController); +route.post("/register", registerController); -export default route; \ No newline at end of file +export default route; From e6f5dce137640640835eccda85368ef22f38e11f Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Mon, 5 Feb 2024 06:11:46 +0200 Subject: [PATCH 02/19] Added logout controller --- requests.rest | 6 +++++- src/controllers/user.js | 5 +++++ src/routes/user.js | 7 ++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/requests.rest b/requests.rest index 726e1e9..325bb67 100644 --- a/requests.rest +++ b/requests.rest @@ -19,4 +19,8 @@ Content-Type: application/json ### -GET http://localhost:3000/ \ No newline at end of file +GET http://localhost:3000/ + +### + +POST http://localhost:3000/logout \ No newline at end of file diff --git a/src/controllers/user.js b/src/controllers/user.js index 475d9f1..cfcf3aa 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -80,3 +80,8 @@ export const registerController = async (req, res) => { } } }; + +export const logoutController = async (req, res) => { + res.clearCookie("access-token"); + res.status(200).send(`Logged out successfully`); +}; diff --git a/src/routes/user.js b/src/routes/user.js index 4b97097..46577ee 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -1,8 +1,13 @@ import express from "express"; -import { loginController, registerController } from "../controllers/user.js"; +import { + loginController, + registerController, + logoutController, +} from "../controllers/user.js"; const route = express.Router(); route.post("/login", loginController); route.post("/register", registerController); +route.post("/logout", logoutController); export default route; From ee9c56098c688923c2d181a39b26d699574e47b7 Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Tue, 6 Feb 2024 23:40:26 +0200 Subject: [PATCH 03/19] Added the usage of models in the controller --- src/controllers/user.js | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/controllers/user.js b/src/controllers/user.js index cfcf3aa..3917bc7 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -1,8 +1,7 @@ import jwt from "jsonwebtoken"; -import { PrismaClient } from "@prisma/client"; import bcrypt from "bcrypt"; -const prisma = new PrismaClient(); const secretKey = process.env.SECRET_KEY; +import * as userModel from "../models/userModel.js"; export const tokenAuth = (req, res, next) => { const token = req.cookies["access-token"]; @@ -21,11 +20,7 @@ export const tokenAuth = (req, res, next) => { export const loginController = async (req, res) => { const { email, password } = req.body; - const user = await prisma.user.findUnique({ - where: { - email: email, - }, - }); + const user = await userModel.getUser(email); if (user === null) { res.status(400).send("Email does not exist"); } else { @@ -47,36 +42,26 @@ export const loginController = async (req, res) => { } } catch (err) { res.status(400).send("Invalid password"); - } finally { - prisma.$disconnect; } } }; export const registerController = async (req, res) => { const { email, username, password } = req.body; - const userExists = await prisma.user.findUnique({ - where: { - email: email, - }, - }); + const userExists = await userModel.getUser(email); if (userExists) { res.send("This Email already exists"); } else { const hashedPassword = await bcrypt.hash(password, 10); try { - await prisma.user.create({ - data: { - email: email, - name: username, - password: hashedPassword, - }, + const createdUser = await userModel.createUser({ + email: email, + name: username, + password: hashedPassword, }); - res.status(201).send("User created successfully"); + res.status(201).send(`User ${createdUser.email} created successfully`); } catch { res.status(500).send(); - } finally { - prisma.$disconnect; } } }; From be649d661e5cd53202ffb17148fb724efd3b93fd Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Thu, 8 Feb 2024 01:00:48 +0200 Subject: [PATCH 04/19] Added createProject and joinProject functions + fixes --- .env.example | 3 +- .../20240207163006_uuid/migration.sql | 8 ++ prisma/schema.prisma | 1 + requests.rest | 41 ++++++- src/app.js | 9 +- src/controllers/projectController.js | 58 ++++++++++ .../{user.js => userController.js} | 43 +++----- src/middlewares/authMiddleware.js | 17 +++ src/models/projectModel.js | 85 +++++++++----- src/models/teamModel.js | 104 +++++++++--------- src/models/userModel.js | 64 +++++------ src/routes/projectRoute.js | 19 ++++ src/routes/{user.js => userRoute.js} | 2 +- 13 files changed, 306 insertions(+), 148 deletions(-) create mode 100644 prisma/migrations/20240207163006_uuid/migration.sql create mode 100644 src/controllers/projectController.js rename src/controllers/{user.js => userController.js} (56%) create mode 100644 src/middlewares/authMiddleware.js create mode 100644 src/routes/projectRoute.js rename src/routes/{user.js => userRoute.js} (86%) diff --git a/.env.example b/.env.example index ddcc11c..72f7e2b 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ -DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE" \ No newline at end of file +DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE" +SECRET_KEY="fe9e750d4588e66b16c391980502d8f0afb006bb513ada864334f2853cc3a8fce25ec0bbcd4d1c68966b287de1847d28545eb658dabb870b9e8e32f8fc11d627" \ No newline at end of file diff --git a/prisma/migrations/20240207163006_uuid/migration.sql b/prisma/migrations/20240207163006_uuid/migration.sql new file mode 100644 index 0000000..9d6acd1 --- /dev/null +++ b/prisma/migrations/20240207163006_uuid/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - The required column `uuid` was added to the `Project` table with a prisma-level default value. This is not possible if the table is not empty. Please add this column as optional, then populate it before making it required. + +*/ +-- AlterTable +ALTER TABLE "Project" ADD COLUMN "uuid" TEXT NOT NULL; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 3292094..0cc89c6 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -39,6 +39,7 @@ model Project { teamId Int team Team @relation(fields: [teamId], references: [id], onDelete: Cascade) databases Database[] + uuid String @default(uuid()) } model Database { diff --git a/requests.rest b/requests.rest index 325bb67..9dd4c0a 100644 --- a/requests.rest +++ b/requests.rest @@ -1,26 +1,61 @@ +// Register user POST http://localhost:3000/register Content-Type: application/json { - "email": "amr10@g.com", + "email": "amr11@g.com", "username": "amr", "password": "1234" } ### +// Login user POST http://localhost:3000/login Content-Type: application/json { - "email": "amr10@g.com", + "email": "amr11@g.com", "password": "1234" } ### +// Home Page GET http://localhost:3000/ ### +// Logout user -POST http://localhost:3000/logout \ No newline at end of file +POST http://localhost:3000/logout + +### +// Create Project + +POST http://localhost:3000/project/create +Content-Type: application/json + +{ + "teamId": 1, + "projectName": "newProject" +} + +### +// Join Project using uuid + +POST http://localhost:3000/project/join +Content-Type: application/json + +{ + "uuid": "d742cdcd-4802-41e3-9f95-f5bb25a7c774" +} + +### +// List Projects of team + +GET http://localhost:3000/project/ +Content-Type: application/json + +{ + "teamId": 1 +} \ No newline at end of file diff --git a/src/app.js b/src/app.js index 75a30ac..7fbd987 100644 --- a/src/app.js +++ b/src/app.js @@ -1,8 +1,9 @@ import express from "express"; import bodyParser from "body-parser"; -import register from "./routes/user.js"; +import userRoute from "./routes/userRoute.js"; import cookieParser from "cookie-parser"; -import { tokenAuth } from "./controllers/user.js"; +import { tokenAuth } from "./middlewares/authMiddleware.js"; +import projectRoute from "./routes/projectRoute.js"; const app = express(); const port = 3000; @@ -11,12 +12,14 @@ app.use(express.json()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser()); -app.use("/", register); +app.use("/", userRoute); app.get("/", tokenAuth, (req, res) => { res.send(`Logged in as: ${req.user.email}`); }); +app.use("/project", projectRoute); + app.listen(port, () => { console.log(`Server is running on port ${port}`); }); diff --git a/src/controllers/projectController.js b/src/controllers/projectController.js new file mode 100644 index 0000000..cfba459 --- /dev/null +++ b/src/controllers/projectController.js @@ -0,0 +1,58 @@ +import * as projectModel from "../models/projectModel.js"; +import * as teamModel from "../models/teamModel.js"; +import * as userModel from "../models/userModel.js"; + +export const createProject = async (req, res) => { + const user = req.user; // user is added to the request inside the token authorization middleware + if (!(await userModel.getUser(user.email))) { + res.status(400).json({ error: "User not found" }); + } + + const team = await teamModel.getTeam(parseInt(req.body.teamId)); + if (!team) { + res.send(403).json({ error: "Team not found" }); + } + + const leader = team.members.find((member) => member.role === "LEADER").user; + + if (user.id === leader.id) { + const project = await projectModel.createProject( + team.id, + req.body.projectName + ); + res.status(201).json({ project: project }); + } else { + res.status(401).json({ error: "Unauthorized: user is not LEADER" }); + } +}; +export const updateProject = async (req, res) => {}; +export const joinProject = async (req, res) => { + const uuid = req.body.uuid; + const project = await projectModel.getProjectUUID(uuid); + if (!project) { + res.status(400).json({ error: "Project not found" }); + } + + const user = req.user; + if (!(await userModel.getUser(user.email))) { + res.status(400).json({ error: "User not found" }); + } + + const team = await teamModel.getTeam(project.teamId); + if (!team) { + res.status(400).json({ error: "Team not found" }); + } + const addMember = await teamModel.addMember(team.id, user.email); + if (!addMember) { + res.status(400).json({ error: "User already exists in the team" }); + } else { + res.status(201).json({ + message: `User ${user.email} added successfully to project ${project.name} and team ${team.name}`, + }); + } +}; +export const deleteProject = async (req, res) => {}; +export const listProjects = async (req, res) => { + const activeTeam = req.body.teamId; + res.json({ projects: await projectModel.allProjects(activeTeam) }); +}; diff --git a/src/controllers/user.js b/src/controllers/userController.js similarity index 56% rename from src/controllers/user.js rename to src/controllers/userController.js index 3917bc7..9431941 100644 --- a/src/controllers/user.js +++ b/src/controllers/userController.js @@ -2,37 +2,19 @@ import jwt from "jsonwebtoken"; import bcrypt from "bcrypt"; const secretKey = process.env.SECRET_KEY; import * as userModel from "../models/userModel.js"; - -export const tokenAuth = (req, res, next) => { - const token = req.cookies["access-token"]; - if (!token) { - return res.status(400).send("Unauthenticated access token"); - } - jwt.verify(token, secretKey, (err, user) => { - if (err) { - return res.status(403).json({ error: err }); - } - req.user = user; - req.authenticated = true; - return next(); - }); -}; +import * as teamModel from "../models/teamModel.js"; export const loginController = async (req, res) => { const { email, password } = req.body; const user = await userModel.getUser(email); if (user === null) { - res.status(400).send("Email does not exist"); + res.status(400).json({ error: "Email does not exist" }); } else { try { if (await bcrypt.compare(password, user.password)) { - const token = jwt.sign( - { email: email, password: password }, - secretKey, - { - expiresIn: "1h", - } - ); + const token = jwt.sign(user, secretKey, { + expiresIn: "1h", + }); res .cookie("access-token", token, { maxAge: 3600000, @@ -41,7 +23,7 @@ export const loginController = async (req, res) => { .json({ accessToken: token }); } } catch (err) { - res.status(400).send("Invalid password"); + res.status(400).json({ error: "Invalid password" }); } } }; @@ -50,7 +32,7 @@ export const registerController = async (req, res) => { const { email, username, password } = req.body; const userExists = await userModel.getUser(email); if (userExists) { - res.send("This Email already exists"); + res.json({ message: "This Email already exists" }); } else { const hashedPassword = await bcrypt.hash(password, 10); try { @@ -59,14 +41,17 @@ export const registerController = async (req, res) => { name: username, password: hashedPassword, }); - res.status(201).send(`User ${createdUser.email} created successfully`); - } catch { - res.status(500).send(); + await teamModel.createTeam(createdUser.id, createdUser.name); + res + .status(201) + .json({ message: `User ${createdUser.email} created successfully` }); + } catch (err) { + res.status(500).json({ error: err }); } } }; export const logoutController = async (req, res) => { res.clearCookie("access-token"); - res.status(200).send(`Logged out successfully`); + res.status(200).json({ message: `Logged out successfully` }); }; diff --git a/src/middlewares/authMiddleware.js b/src/middlewares/authMiddleware.js new file mode 100644 index 0000000..a4839f6 --- /dev/null +++ b/src/middlewares/authMiddleware.js @@ -0,0 +1,17 @@ +import jwt from "jsonwebtoken"; +const secretKey = process.env.SECRET_KEY; + +export const tokenAuth = (req, res, next) => { + const token = req.cookies["access-token"]; + if (!token) { + return res.status(400).send("Unauthenticated access token"); + } + jwt.verify(token, secretKey, (err, user) => { + if (err) { + return res.status(403).json({ error: err }); + } + req.user = user; + req.authenticated = true; + return next(); + }); +}; diff --git a/src/models/projectModel.js b/src/models/projectModel.js index 37b8c80..f89b132 100644 --- a/src/models/projectModel.js +++ b/src/models/projectModel.js @@ -1,53 +1,82 @@ -import { prisma } from '../utils/db.js' +import { prisma } from "../utils/db.js"; -export async function createProject (teamId, projectName) { +export async function createProject(teamId, projectName) { const proj = await prisma.project.create({ data: { team: { connect: { - id: teamId - } + id: teamId, + }, }, - name: projectName - } - }) - return proj + name: projectName, + }, + }); + return proj; } -export async function getProject (projectId) { +export async function getProject(projectId) { const proj = await prisma.project.findUnique({ where: { - id: projectId - } - }) - return proj + id: projectId, + }, + }); + return proj; +} + +export async function getProjectUUID(projectUUID) { + const proj = await prisma.project.findFirst({ + where: { + uuid: projectUUID, + }, + }); + return proj; } -export async function allProjects (teamId) { +export async function allProjects(teamId) { const projs = await prisma.project.findMany({ where: { - teamId: teamId - } - }) - return projs + teamId: teamId, + }, + include: { + team: true, + team: { + include: { + members: { + include: { + user: true, + user: { + select: { + id: true, + email: true, + name: true, + role: true, + }, + }, + }, + }, + }, + }, + }, + }); + return projs; } -export async function deleteProject (projectId) { +export async function deleteProject(projectId) { await prisma.project.delete({ where: { - id: projectId - } - }) + id: projectId, + }, + }); } -export async function updateProject (projectId, projectName) { +export async function updateProject(projectId, projectName) { const proj = await prisma.project.update({ where: { - id: projectId + id: projectId, }, data: { - name: projectName - } - }) - return proj + name: projectName, + }, + }); + return proj; } diff --git a/src/models/teamModel.js b/src/models/teamModel.js index a8dd0b0..d1684d5 100644 --- a/src/models/teamModel.js +++ b/src/models/teamModel.js @@ -1,99 +1,101 @@ -import { prisma } from '../utils/db.js' -import { getUserId } from './userModel.js' +import { prisma } from "../utils/db.js"; +import { getUserId } from "./userModel.js"; -export async function createTeam (leader_id, Name) { +export async function createTeam(leader_id, Name) { const team = await prisma.team.create({ data: { name: Name, members: { create: [ { - role: 'LEADER', + role: "LEADER", user: { - connect: { id: leader_id } - } - } - ] - } - } - }) - return team + connect: { id: leader_id }, + }, + }, + ], + }, + }, + }); + return team; } -export async function memberExist (team_id, user_id) { +export async function memberExist(team_id, user_id) { const exist = await prisma.teamMembers.findUnique({ where: { teamId_userId: { teamId: team_id, - userId: user_id - } - } - }) - return exist + userId: user_id, + }, + }, + }); + return exist; } -export async function addMember (team_id, memberEmail) { - const user_id = await getUserId(memberEmail) +export async function addMember(team_id, memberEmail) { + const user_id = await getUserId(memberEmail); if (!user_id) { - return false + return false; } if (!(await memberExist(team_id, user_id.id))) { - const team = await prisma.teamMembers.create({ + await prisma.teamMembers.create({ data: { user: { - connect: { id: user_id.id } + connect: { id: user_id.id }, }, team: { - connect: { id: team_id } - } - } - }) + connect: { id: team_id }, + }, + }, + }); + return true; + } else { + return false; } - return true } -export async function deleteMember (team_id, e_mail) { - const user_id = await getUserId(e_mail) +export async function deleteMember(team_id, e_mail) { + const user_id = await getUserId(e_mail); if (!user_id) { - return false + return false; } const team = await prisma.teamMembers.delete({ where: { teamId_userId: { teamId: team_id, - userId: user_id.id - } - } - }) - return true + userId: user_id.id, + }, + }, + }); + return true; } -export async function getTeam (team_id) { +export async function getTeam(team_id) { const team = await prisma.team.findUnique({ where: { - id: team_id + id: team_id, }, - include: { members: { include: { user: true } } } - }) - return team + include: { members: { include: { user: true } } }, + }); + return team; } -export async function updateTeam (team_id, Name) { +export async function updateTeam(team_id, Name) { const team = await prisma.team.update({ where: { - id: team_id + id: team_id, }, data: { - name: Name - } - }) - return team + name: Name, + }, + }); + return team; } -export async function deleteTeam (team_id) { +export async function deleteTeam(team_id) { await prisma.team.delete({ where: { - id: team_id - } - }) + id: team_id, + }, + }); } diff --git a/src/models/userModel.js b/src/models/userModel.js index a644e94..b831955 100644 --- a/src/models/userModel.js +++ b/src/models/userModel.js @@ -1,6 +1,6 @@ -import { prisma } from '../utils/db.js' +import { prisma } from "../utils/db.js"; -export async function createUser (userData) { +export async function createUser(userData) { /* userData is an object with the following properties: { name: "NAME", @@ -9,53 +9,53 @@ export async function createUser (userData) { } */ ///try const user = await prisma.user.create({ - data: userData - }) - return user + data: userData, + }); + return user; } -export async function allUsers () { - const users = await prisma.user.findMany() - return users +export async function allUsers() { + const users = await prisma.user.findMany(); + return users; } -export async function getUser (e_mail) { +export async function getUser(email) { const user = await prisma.user.findUnique({ where: { - email: e_mail + email: email, }, - include: { teams: { include: { team: true } } } - }) - return user + include: { teams: { include: { team: true } } }, + }); + return user; } -export async function getUserId (e_mail) { +export async function getUserId(e_mail) { const user_id = await prisma.user.findUnique({ where: { - email: e_mail + email: e_mail, }, select: { - id: true - } - }) - return user_id + id: true, + }, + }); + return user_id; } -export async function deleteUser (e_mail, pass) { - const user = await getUser(e_mail) +export async function deleteUser(e_mail, pass) { + const user = await getUser(e_mail); if (user && user.password === pass) { await prisma.user.delete({ where: { email: e_mail, - password: pass - } - }) - return true + password: pass, + }, + }); + return true; } - return false + return false; } -export async function updateUser (e_mail, userData) { +export async function updateUser(e_mail, userData) { /* userData is an object with the following optional properties: { name: "NAME", @@ -64,11 +64,11 @@ export async function updateUser (e_mail, userData) { if (await getUser(e_mail)) { const updated_user = await prisma.user.update({ where: { - email: e_mail + email: e_mail, }, - data: userData - }) - return updated_user + data: userData, + }); + return updated_user; } - return false + return false; } diff --git a/src/routes/projectRoute.js b/src/routes/projectRoute.js new file mode 100644 index 0000000..1c0f038 --- /dev/null +++ b/src/routes/projectRoute.js @@ -0,0 +1,19 @@ +import express from "express"; +import { + createProject, + updateProject, + deleteProject, + joinProject, + listProjects, +} from "../controllers/projectController.js"; +import { tokenAuth } from "../middlewares/authMiddleware.js"; + +const route = express.Router(); + +route.post("/create", tokenAuth, createProject); +route.post("/update", tokenAuth, updateProject); +route.post("/delete", tokenAuth, deleteProject); +route.post("/join", tokenAuth, joinProject); +route.get("/", tokenAuth, listProjects); + +export default route; diff --git a/src/routes/user.js b/src/routes/userRoute.js similarity index 86% rename from src/routes/user.js rename to src/routes/userRoute.js index 46577ee..7ab6394 100644 --- a/src/routes/user.js +++ b/src/routes/userRoute.js @@ -3,7 +3,7 @@ import { loginController, registerController, logoutController, -} from "../controllers/user.js"; +} from "../controllers/userController.js"; const route = express.Router(); route.post("/login", loginController); From 7c4aec6d2364d8a067b1f6992844862cd8ca0c7c Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Sat, 10 Feb 2024 21:00:24 +0200 Subject: [PATCH 05/19] Edit: removed unnecessary semicolons in model files --- src/controllers/projectController.js | 8 ++--- src/models/databaseModel.js | 45 ++++++++++++------------- src/models/projectModel.js | 33 ++++++++----------- src/models/teamModel.js | 49 +++++++++++++--------------- src/models/userModel.js | 34 +++++++++---------- 5 files changed, 76 insertions(+), 93 deletions(-) diff --git a/src/controllers/projectController.js b/src/controllers/projectController.js index cfba459..2bcde17 100644 --- a/src/controllers/projectController.js +++ b/src/controllers/projectController.js @@ -7,14 +7,11 @@ export const createProject = async (req, res) => { if (!(await userModel.getUser(user.email))) { res.status(400).json({ error: "User not found" }); } - const team = await teamModel.getTeam(parseInt(req.body.teamId)); if (!team) { res.send(403).json({ error: "Team not found" }); } - const leader = team.members.find((member) => member.role === "LEADER").user; - if (user.id === leader.id) { const project = await projectModel.createProject( team.id, @@ -25,6 +22,7 @@ export const createProject = async (req, res) => { res.status(401).json({ error: "Unauthorized: user is not LEADER" }); } }; + export const updateProject = async (req, res) => {}; export const joinProject = async (req, res) => { const uuid = req.body.uuid; @@ -32,12 +30,10 @@ export const joinProject = async (req, res) => { if (!project) { res.status(400).json({ error: "Project not found" }); } - const user = req.user; if (!(await userModel.getUser(user.email))) { res.status(400).json({ error: "User not found" }); } - const team = await teamModel.getTeam(project.teamId); if (!team) { res.status(400).json({ error: "Team not found" }); @@ -51,7 +47,9 @@ export const joinProject = async (req, res) => { }); } }; + export const deleteProject = async (req, res) => {}; + export const listProjects = async (req, res) => { const activeTeam = req.body.teamId; res.json({ projects: await projectModel.allProjects(activeTeam) }); diff --git a/src/models/databaseModel.js b/src/models/databaseModel.js index c048d72..9cfb144 100644 --- a/src/models/databaseModel.js +++ b/src/models/databaseModel.js @@ -1,54 +1,49 @@ -import { prisma } from '../utils/db.js' - -export async function createDatabase (projectId, databaseName) { +import { prisma } from "../utils/db.js" +export async function createDatabase(projectId, databaseName) { const db = await prisma.database.create({ data: { project: { connect: { - id: projectId - } + id: projectId, + }, }, name: databaseName, - storage: 0 - } + storage: 0, + }, }) return db } - -export async function getDatabase (databaseId) { +export async function getDatabase(databaseId) { const db = await prisma.database.findUnique({ where: { - id: databaseId - } + id: databaseId, + }, }) return db } - -export async function allDatabases (projectId) { +export async function allDatabases(projectId) { const dbs = await prisma.database.findMany({ where: { - projectId: projectId - } + projectId: projectId, + }, }) return dbs } - -export async function deleteDatabase (databaseId) { +export async function deleteDatabase(databaseId) { await prisma.database.delete({ where: { - id: databaseId - } + id: databaseId, + }, }) } - -export async function updateDatabase (databaseId, databaseName) { +export async function updateDatabase(databaseId, databaseName) { const db = await prisma.database.update({ where: { - id: databaseId + id: databaseId, }, data: { - name: databaseName - } + name: databaseName, + }, }) return db -} \ No newline at end of file +} diff --git a/src/models/projectModel.js b/src/models/projectModel.js index f89b132..2a16360 100644 --- a/src/models/projectModel.js +++ b/src/models/projectModel.js @@ -1,5 +1,4 @@ -import { prisma } from "../utils/db.js"; - +import { prisma } from "../utils/db.js" export async function createProject(teamId, projectName) { const proj = await prisma.project.create({ data: { @@ -10,28 +9,25 @@ export async function createProject(teamId, projectName) { }, name: projectName, }, - }); - return proj; + }) + return proj } - export async function getProject(projectId) { const proj = await prisma.project.findUnique({ where: { id: projectId, }, - }); - return proj; + }) + return proj } - export async function getProjectUUID(projectUUID) { const proj = await prisma.project.findFirst({ where: { uuid: projectUUID, }, - }); - return proj; + }) + return proj } - export async function allProjects(teamId) { const projs = await prisma.project.findMany({ where: { @@ -57,18 +53,17 @@ export async function allProjects(teamId) { }, }, }, - }); - return projs; + }) + return projs } - export async function deleteProject(projectId) { - await prisma.project.delete({ + const deletedProject = await prisma.project.delete({ where: { id: projectId, }, - }); + }) + return deletedProject ? true : false } - export async function updateProject(projectId, projectName) { const proj = await prisma.project.update({ where: { @@ -77,6 +72,6 @@ export async function updateProject(projectId, projectName) { data: { name: projectName, }, - }); - return proj; + }) + return proj } diff --git a/src/models/teamModel.js b/src/models/teamModel.js index d1684d5..1b376a5 100644 --- a/src/models/teamModel.js +++ b/src/models/teamModel.js @@ -1,6 +1,5 @@ -import { prisma } from "../utils/db.js"; -import { getUserId } from "./userModel.js"; - +import { prisma } from "../utils/db.js" +import { getUserId } from "./userModel.js" export async function createTeam(leader_id, Name) { const team = await prisma.team.create({ data: { @@ -16,10 +15,9 @@ export async function createTeam(leader_id, Name) { ], }, }, - }); - return team; + }) + return team } - export async function memberExist(team_id, user_id) { const exist = await prisma.teamMembers.findUnique({ where: { @@ -28,14 +26,13 @@ export async function memberExist(team_id, user_id) { userId: user_id, }, }, - }); - return exist; + }) + return exist } - export async function addMember(team_id, memberEmail) { - const user_id = await getUserId(memberEmail); + const user_id = await getUserId(memberEmail) if (!user_id) { - return false; + return false } if (!(await memberExist(team_id, user_id.id))) { await prisma.teamMembers.create({ @@ -47,27 +44,27 @@ export async function addMember(team_id, memberEmail) { connect: { id: team_id }, }, }, - }); - return true; + }) + return true } else { - return false; + return false } } export async function deleteMember(team_id, e_mail) { - const user_id = await getUserId(e_mail); - if (!user_id) { - return false; + const user = await getUserId(e_mail) + if (!user) { + return false } - const team = await prisma.teamMembers.delete({ + await prisma.teamMembers.delete({ where: { teamId_userId: { teamId: team_id, - userId: user_id.id, + userId: user.id, }, }, - }); - return true; + }) + return true } export async function getTeam(team_id) { @@ -76,8 +73,8 @@ export async function getTeam(team_id) { id: team_id, }, include: { members: { include: { user: true } } }, - }); - return team; + }) + return team } export async function updateTeam(team_id, Name) { @@ -88,8 +85,8 @@ export async function updateTeam(team_id, Name) { data: { name: Name, }, - }); - return team; + }) + return team } export async function deleteTeam(team_id) { @@ -97,5 +94,5 @@ export async function deleteTeam(team_id) { where: { id: team_id, }, - }); + }) } diff --git a/src/models/userModel.js b/src/models/userModel.js index b831955..0bdb375 100644 --- a/src/models/userModel.js +++ b/src/models/userModel.js @@ -1,5 +1,4 @@ -import { prisma } from "../utils/db.js"; - +import { prisma } from "../utils/db.js" export async function createUser(userData) { /* userData is an object with the following properties: { @@ -7,16 +6,15 @@ export async function createUser(userData) { email: "EMAIL", password: "PASSWORD" } */ - ///try const user = await prisma.user.create({ data: userData, - }); - return user; + }) + return user } export async function allUsers() { - const users = await prisma.user.findMany(); - return users; + const users = await prisma.user.findMany() + return users } export async function getUser(email) { @@ -25,8 +23,8 @@ export async function getUser(email) { email: email, }, include: { teams: { include: { team: true } } }, - }); - return user; + }) + return user } export async function getUserId(e_mail) { @@ -37,22 +35,22 @@ export async function getUserId(e_mail) { select: { id: true, }, - }); - return user_id; + }) + return user_id } export async function deleteUser(e_mail, pass) { - const user = await getUser(e_mail); + const user = await getUser(e_mail) if (user && user.password === pass) { await prisma.user.delete({ where: { email: e_mail, password: pass, }, - }); - return true; + }) + return true } - return false; + return false } export async function updateUser(e_mail, userData) { @@ -67,8 +65,8 @@ export async function updateUser(e_mail, userData) { email: e_mail, }, data: userData, - }); - return updated_user; + }) + return updated_user } - return false; + return false } From fdb57f17d2e72a32456a5daa4c19c6e0a104edbb Mon Sep 17 00:00:00 2001 From: Reem-Kamal-Ghoniem Date: Sun, 11 Feb 2024 05:01:38 +0200 Subject: [PATCH 06/19] adding deleteProject, updateProject function to other project part --- src/controllers/projectController.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/controllers/projectController.js b/src/controllers/projectController.js index 2bcde17..f9ebca4 100644 --- a/src/controllers/projectController.js +++ b/src/controllers/projectController.js @@ -23,7 +23,10 @@ export const createProject = async (req, res) => { } }; -export const updateProject = async (req, res) => {}; +export const updateProject = async (req, res) => { + const { projectID, newName } = req.body; + res.send(await updateProject(projectID, newName)); +}; export const joinProject = async (req, res) => { const uuid = req.body.uuid; const project = await projectModel.getProjectUUID(uuid); @@ -48,7 +51,11 @@ export const joinProject = async (req, res) => { } }; -export const deleteProject = async (req, res) => {}; +export const deleteProject = async (req, res) => { + const { projectID } = req.body; + await deleteProject(projectID); + res.send('project deleted'); +}; export const listProjects = async (req, res) => { const activeTeam = req.body.teamId; From 92bd075c28ea47fc44341eb619c7d6b402ba8e7b Mon Sep 17 00:00:00 2001 From: reem Date: Sun, 11 Feb 2024 19:00:44 +0200 Subject: [PATCH 07/19] database endpoints --- src/app.js | 2 + src/controllers/databaseController.js | 96 +++++++++++++++++++++++++++ src/routes/databaseRoute.js | 12 ++++ 3 files changed, 110 insertions(+) create mode 100644 src/controllers/databaseController.js create mode 100644 src/routes/databaseRoute.js diff --git a/src/app.js b/src/app.js index 7fbd987..3f00993 100644 --- a/src/app.js +++ b/src/app.js @@ -4,6 +4,7 @@ import userRoute from "./routes/userRoute.js"; import cookieParser from "cookie-parser"; import { tokenAuth } from "./middlewares/authMiddleware.js"; import projectRoute from "./routes/projectRoute.js"; +import DatabaseRoute from "./routes/databaseRoute.js"; const app = express(); const port = 3000; @@ -19,6 +20,7 @@ app.get("/", tokenAuth, (req, res) => { }); app.use("/project", projectRoute); +app.use("/database", DatabaseRoute); app.listen(port, () => { console.log(`Server is running on port ${port}`); diff --git a/src/controllers/databaseController.js b/src/controllers/databaseController.js new file mode 100644 index 0000000..db75b65 --- /dev/null +++ b/src/controllers/databaseController.js @@ -0,0 +1,96 @@ +import { getProject } from "../models/projectModel.js"; +import { memberExist } from "../models/teamModel.js"; +import * as dataModel from "../models/databaseModel.js"; + +export const createDatabase = async (req, res) => { + const { projectId, databaseName } = req.body; + const project = await getProject(projectId); + if (!project) { + return res.status(404).json({ message: "project doesn't exist" }); + } + const user_id = req.user.id; + const teamMember = await memberExist(project.teamId, user_id); + if (!teamMember) { + return res + .status(403) + .json({ message: "user doesn't exist in the project" }); + } + if (teamMember.role === "MEMBER") { + return res + .status(403) + .json({ message: "user doesn't have permissions to create a database" }); + } + const database = await dataModel.createDatabase(projectId, databaseName); + res.status(201).json({ + message: "database created successfully", + database: database, + }); +}; + +export const getDatabase = async (req, res) => { + const { projectId } = req.query; + const project = await getProject(+projectId); + if (!project) { + return res.status(404).json({ message: "project doesn't exist" }); + } + const user_id = req.user.id; + if (!(await memberExist(project.teamId, user_id))) { + return res + .status(403) + .json({ message: "user doesn't exist in the project" }); + } + const databases = await dataModel.allDatabases(+projectId); + res.status(200).json({ databases: databases }); +}; + +export const updateDatabase = async (req, res) => { + const { databaseId, databaseName } = req.body; + const database = await dataModel.getDatabase(databaseId); + if (!database) { + return res.status(404).json({ message: "database doesn't exist" }); + } + const project = await getProject(database.projectId); + const user_id = req.user.id; + const teamMember = await memberExist(project.teamId, user_id); + if (!teamMember) { + return res + .status(403) + .json({ message: "user doesn't exist in the project" }); + } + if (teamMember.role === "MEMBER") { + return res + .status(403) + .json({ message: "user doesn't have permissions to update database" }); + } + const updated_database = await dataModel.updateDatabase( + databaseId, + databaseName + ); + res.status(200).json({ + message: "database updated successfully", + updatedDatabase: updated_database, + }); +}; + +export const deleteDatabase = async (req, res) => { + const { databaseId } = req.body; + const database = await dataModel.getDatabase(databaseId); + if (!database) { + return res.status(404).json({ message: "database doesn't exist" }); + } + const project = await getProject(database.projectId); + const user_id = req.user.id; + const teamMember = await memberExist(project.teamId, user_id); + if (!teamMember) { + return res + .status(403) + .json({ message: "user doesn't exist in the project" }); + } + if (teamMember.role === "MEMBER") { + return res + .status(403) + .json({ message: "user doesn't have permissions to delete database" }); + } + await dataModel.deleteDatabase(databaseId); + res.status(204); +}; diff --git a/src/routes/databaseRoute.js b/src/routes/databaseRoute.js new file mode 100644 index 0000000..321fb97 --- /dev/null +++ b/src/routes/databaseRoute.js @@ -0,0 +1,12 @@ +import express from "express"; +import * as control from "../controllers/databaseController.js"; +import { tokenAuth } from "../middlewares/authMiddleware.js"; + +const route = express.Router(); + +route.get("/", tokenAuth, control.getDatabase); +route.post("/create", tokenAuth, control.createDatabase); +route.put("/update", tokenAuth, control.updateDatabase); +route.delete("/delete", tokenAuth, control.deleteDatabase); + +export default route; \ No newline at end of file From 9728eb9cf323a5186efb54f06e70140f7e67220c Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Mon, 12 Feb 2024 21:18:54 +0200 Subject: [PATCH 08/19] Added doc file and ERD diagram image --- docs/prisma-erd.svg | 2 + docs/schema.md | 226 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 docs/prisma-erd.svg create mode 100644 docs/schema.md diff --git a/docs/prisma-erd.svg b/docs/prisma-erd.svg new file mode 100644 index 0000000..eb59b41 --- /dev/null +++ b/docs/prisma-erd.svg @@ -0,0 +1,2 @@ + +UserIntidStringemailStringnameStringpasswordUserRoleroleTeamIntidStringnameTeamMembersMemberRoleroleProjectIntidStringnameStringuuidDatabaseStringidStringnameDateTimecreatedAtIntstoragestatusstatusteamuserteamproject \ No newline at end of file diff --git a/docs/schema.md b/docs/schema.md new file mode 100644 index 0000000..91c3ee8 --- /dev/null +++ b/docs/schema.md @@ -0,0 +1,226 @@ +# Prisma Schema Documentation + +## Overview + +This is a a comprehensive overview of the database schema used in the platform, including entities, relationships, enums, diagrams and additional notes for proper usage and maintenance. + +### Technologies Used + +**Database Management System:** PostgreSQL + +- PostgreSQL is scalable open-source relational database management system known for SQL compliance, + advanced concurrency control, extensibility, and strong security. + +**ORM:** Prisma + +- Prisma is a Node.js and Typescript ORM with an intuitive data model, automated migrations, type-safety, + and auto-completion. It defines the main configuration of the data models in a file called `schema.prisma` + +## Schema Description + +### Generator + +Specifies how Prisma generates client code for interacting with the database, such as Prisma Client. + +```prisma +generator client { + provider = "prisma-client-js" +} +``` + +### Data Source + +Defines the database connection details, including the provider (in our case PostgreSQL), and URL, which is stored in an environment variable. + +```prisma +datasource db { + provider = "postgresql" + url = env("DATABASE_URL") +} +``` + +### Entities + +#### Structure + +The schema consists of the following entities: + +- **User:** Represents users of the system. + + ```prisma + model User { + id Int @id @default(autoincrement()) + email String @unique + name String + password String + role UserRole @default(USER) + teams TeamMembers[] + } + ``` + +- **Team:** Represents teams with members and projects. + + ```prisma + model Team { + id Int @id @default(autoincrement()) + name String + members TeamMembers[] + projects Project[] + } + ``` + +- **TeamMembers:** Represents the relationship between users and teams, including their roles. + + ```prisma + model TeamMembers { + teamId Int + userId Int + team Team @relation(fields: [teamId], references: [id], onDelete: Cascade) + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + role MemberRole @default(MEMBER) + + @@id([teamId, userId]) + } + ``` + +- **Project:** Represents projects belonging to teams. + + ```prisma + model Project { + id Int @id @default(autoincrement()) + name String + teamId Int + team Team @relation(fields: [teamId], references: [id], onDelete: Cascade) + databases Database[] + uuid String @default(uuid()) + } + ``` + +- **Database:** Represents databases associated with projects. + + ```prisma + model Database { + id String @id @default(uuid()) + name String + createdAt DateTime @default(now()) + storage Int + projectId Int + project Project @relation(fields: [projectId], references: [id], onDelete: Cascade) + status status @default(INACTIVE) + } + ``` + +#### Entity Details + +##### User + +- `id`: Auto-incremented unique identifier for the user. +- `email`: Unique email address of the user. +- `name`: Name of the user +- `password`: Hashed password of the user +- `role`: Role of the user, default: `USER` +- Relationships: + `teams` Many-to-many relationship with Team entity through TeamMembers property + +###### Suggestions + +- implementing a unique constraint on the email column +- Adding an image column for profile pictures of users +- Adding a column to store the user's preferred language and another for his preferred timezone + +##### Team + +- `id` Auto-incremented unique identifier for the team +- `name` Name of the team +- Relationships: + `members` One-to-many relationship with team members + `projects` One-to-many relationship with projects + +###### Suggestions + +- Adding a column to store a description of the team and its goals + +##### TeamMembers + +- `teamId` Foreign key referencing the id column of the Team table, pointing to the team that the member belongs to +- `userId` Foreign key referencing the id column of the User table, pointing to the user that is a member of the team +- `role` Role of the member in the team, default: `MEMBER` +- Relationships: + `team` Many-to-one relationship with teams + `user` Many-to-one relationship with users + +##### Project + +- `id` Auto-incremented unique identifier for the project +- `name` Name of the project +- `teamId` Foreign key referencing the id column of the Team table, pointing to the team that created the project +- `uuid` Unique UUID identifier for the project +- Relationships: + `team` Many-to-one relationship with the team that creates the projects + `databases` One-to-many relationship with databases associated with the project + +###### Suggestions + +- Adding a column to store a description of the project +- Combining the `id` and `uuid` columns in a single column `id` that uses a uuid to identify the project + +##### Database + +- `id` Unique UUID identifier for the database +- `name`Name of the database +- `createdAt` Timestamp indicating the creation time of the database +- `storage` Storage capacity of the database +- `projectId` Foreign key referencing the id column of the Project table, pointing to the project that the database belongs to +- Relationships: + `project` Many-to-one relationship with the project + +### Enums + +#### UserRole + +- `USER`: Regular user + - Regular user with standard access privileges on teams and projects. +- `ADMIN`: Admin user + - User with elevated access rights for system administration of the platform. + +```prisma +enum UserRole { + USER + ADMIN +} +``` + +#### MemberRole + +- `MEMBER`: Regular member + - Regular team member with access to the team's projects +- `LEADER`: Leader member + - The user that created the team and has the privileges to create and delete projects. + +```prisma +enum MemberRole { + LEADER + MEMBER +} +``` + +#### Status + +- `ACTIVE`: Active database + - Database that is online and fully responsive +- `INACTIVE`: Inactive database + - Database that hasn't been used for a while but still responsive +- `SLEEP`: Sleeping database + - Database that was `INACTIVE` for a certain amount of time and entered an offline mode, and it must be activated in order to be responsive for queries + +```prisma +enum status { + ACTIVE + INACTIVE + SLEEP +} +``` + +### ERD Diagram + +![1707762267097](./prisma-erd.svg) From 493aa943f253c6eb71204b006bc8c9bcca9db445 Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Mon, 12 Feb 2024 21:23:19 +0200 Subject: [PATCH 09/19] Fixed relationships not showing as a list --- docs/schema.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/schema.md b/docs/schema.md index 91c3ee8..3e7bb23 100644 --- a/docs/schema.md +++ b/docs/schema.md @@ -120,7 +120,7 @@ The schema consists of the following entities: - `password`: Hashed password of the user - `role`: Role of the user, default: `USER` - Relationships: - `teams` Many-to-many relationship with Team entity through TeamMembers property + - `teams` Many-to-many relationship with Team entity through TeamMembers property ###### Suggestions @@ -133,8 +133,8 @@ The schema consists of the following entities: - `id` Auto-incremented unique identifier for the team - `name` Name of the team - Relationships: - `members` One-to-many relationship with team members - `projects` One-to-many relationship with projects + - `members` One-to-many relationship with team members + - `projects` One-to-many relationship with projects ###### Suggestions @@ -146,8 +146,8 @@ The schema consists of the following entities: - `userId` Foreign key referencing the id column of the User table, pointing to the user that is a member of the team - `role` Role of the member in the team, default: `MEMBER` - Relationships: - `team` Many-to-one relationship with teams - `user` Many-to-one relationship with users + - `team` Many-to-one relationship with teams + - `user` Many-to-one relationship with users ##### Project @@ -156,8 +156,8 @@ The schema consists of the following entities: - `teamId` Foreign key referencing the id column of the Team table, pointing to the team that created the project - `uuid` Unique UUID identifier for the project - Relationships: - `team` Many-to-one relationship with the team that creates the projects - `databases` One-to-many relationship with databases associated with the project + - `team` Many-to-one relationship with the team that creates the projects + - `databases` One-to-many relationship with databases associated with the project ###### Suggestions @@ -172,7 +172,7 @@ The schema consists of the following entities: - `storage` Storage capacity of the database - `projectId` Foreign key referencing the id column of the Project table, pointing to the project that the database belongs to - Relationships: - `project` Many-to-one relationship with the project + - `project` Many-to-one relationship with the project ### Enums From 3e841f00849bf2b0be26d8871affc79db2c785aa Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Wed, 14 Feb 2024 16:37:37 +0200 Subject: [PATCH 10/19] Added .vscode folder to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index c6bba59..84bf5a6 100644 --- a/.gitignore +++ b/.gitignore @@ -122,6 +122,9 @@ dist # Stores VSCode versions used for testing VSCode extensions .vscode-test +# VSCode +.vscode/ + # yarn v2 .yarn/cache .yarn/unplugged From a0585e0dbebdc967b1557cbe8cea0e5a56c6ab35 Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Wed, 14 Feb 2024 16:38:39 +0200 Subject: [PATCH 11/19] Removed suggestions and moved diagram to top --- docs/schema.md | 123 ++++++++++++++++++++++--------------------------- 1 file changed, 54 insertions(+), 69 deletions(-) diff --git a/docs/schema.md b/docs/schema.md index 3e7bb23..e0a1ded 100644 --- a/docs/schema.md +++ b/docs/schema.md @@ -16,6 +16,10 @@ This is a a comprehensive overview of the database schema used in the platform, - Prisma is a Node.js and Typescript ORM with an intuitive data model, automated migrations, type-safety, and auto-completion. It defines the main configuration of the data models in a file called `schema.prisma` +### ERD Diagram + +![ERD diagram of the schema](./prisma-erd.svg "ERD Diagram of the schema") + ## Schema Description ### Generator @@ -47,68 +51,68 @@ The schema consists of the following entities: - **User:** Represents users of the system. - ```prisma - model User { - id Int @id @default(autoincrement()) - email String @unique - name String - password String - role UserRole @default(USER) - teams TeamMembers[] - } - ``` +```prisma +model User { + id Int @id @default(autoincrement()) + email String @unique + name String + password String + role UserRole @default(USER) + teams TeamMembers[] +} +``` - **Team:** Represents teams with members and projects. - ```prisma - model Team { - id Int @id @default(autoincrement()) - name String - members TeamMembers[] - projects Project[] - } - ``` +```prisma +model Team { + id Int @id @default(autoincrement()) + name String + members TeamMembers[] + projects Project[] +} +``` - **TeamMembers:** Represents the relationship between users and teams, including their roles. - ```prisma - model TeamMembers { - teamId Int - userId Int - team Team @relation(fields: [teamId], references: [id], onDelete: Cascade) - user User @relation(fields: [userId], references: [id], onDelete: Cascade) - role MemberRole @default(MEMBER) - - @@id([teamId, userId]) - } - ``` +```prisma +model TeamMembers { + teamId Int + userId Int + team Team @relation(fields: [teamId], references: [id], onDelete: Cascade) + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + role MemberRole @default(MEMBER) + + @@id([teamId, userId]) +} +``` - **Project:** Represents projects belonging to teams. - ```prisma - model Project { - id Int @id @default(autoincrement()) - name String - teamId Int - team Team @relation(fields: [teamId], references: [id], onDelete: Cascade) - databases Database[] - uuid String @default(uuid()) - } - ``` +```prisma +model Project { + id Int @id @default(autoincrement()) + name String + teamId Int + team Team @relation(fields: [teamId], references: [id], onDelete: Cascade) + databases Database[] + uuid String @default(uuid()) +} +``` - **Database:** Represents databases associated with projects. - ```prisma - model Database { - id String @id @default(uuid()) - name String - createdAt DateTime @default(now()) - storage Int - projectId Int - project Project @relation(fields: [projectId], references: [id], onDelete: Cascade) - status status @default(INACTIVE) - } - ``` +```prisma +model Database { + id String @id @default(uuid()) + name String + createdAt DateTime @default(now()) + storage Int + projectId Int + project Project @relation(fields: [projectId], references: [id], onDelete: Cascade) + status status @default(INACTIVE) +} +``` #### Entity Details @@ -122,12 +126,6 @@ The schema consists of the following entities: - Relationships: - `teams` Many-to-many relationship with Team entity through TeamMembers property -###### Suggestions - -- implementing a unique constraint on the email column -- Adding an image column for profile pictures of users -- Adding a column to store the user's preferred language and another for his preferred timezone - ##### Team - `id` Auto-incremented unique identifier for the team @@ -136,10 +134,6 @@ The schema consists of the following entities: - `members` One-to-many relationship with team members - `projects` One-to-many relationship with projects -###### Suggestions - -- Adding a column to store a description of the team and its goals - ##### TeamMembers - `teamId` Foreign key referencing the id column of the Team table, pointing to the team that the member belongs to @@ -159,11 +153,6 @@ The schema consists of the following entities: - `team` Many-to-one relationship with the team that creates the projects - `databases` One-to-many relationship with databases associated with the project -###### Suggestions - -- Adding a column to store a description of the project -- Combining the `id` and `uuid` columns in a single column `id` that uses a uuid to identify the project - ##### Database - `id` Unique UUID identifier for the database @@ -220,7 +209,3 @@ enum status { SLEEP } ``` - -### ERD Diagram - -![1707762267097](./prisma-erd.svg) From 13d3d4dee65041604368c82e7155614f1e9c7b44 Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Thu, 15 Feb 2024 01:15:28 +0200 Subject: [PATCH 12/19] ignoring .gitignore file --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 84bf5a6..0d62657 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ yarn-error.log* lerna-debug.log* .pnpm-debug.log* +# Ignore file +.gitignore + # Diagnostic reports (https://nodejs.org/api/report.html) report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json @@ -131,3 +134,4 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + From 193c62cf021180f092d6d52b2936908e6ce5a103 Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Thu, 15 Feb 2024 01:27:23 +0200 Subject: [PATCH 13/19] Added Prisma Components section --- docs/schema.md | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/schema.md b/docs/schema.md index e0a1ded..a6735cf 100644 --- a/docs/schema.md +++ b/docs/schema.md @@ -1,4 +1,4 @@ -# Prisma Schema Documentation +# Schema Documentation ## Overview @@ -16,11 +16,9 @@ This is a a comprehensive overview of the database schema used in the platform, - Prisma is a Node.js and Typescript ORM with an intuitive data model, automated migrations, type-safety, and auto-completion. It defines the main configuration of the data models in a file called `schema.prisma` -### ERD Diagram +## Prisma Components -![ERD diagram of the schema](./prisma-erd.svg "ERD Diagram of the schema") - -## Schema Description +We depend on Prisma as an ORM to connect our application to the database using the following components: ### Generator @@ -43,6 +41,8 @@ datasource db { } ``` +## Schema Description + ### Entities #### Structure @@ -114,9 +114,13 @@ model Database { } ``` -#### Entity Details +### ERD Diagram + +![ERD diagram of the schema](./prisma-erd.svg "ERD Diagram of the schema") + +### Entity Details -##### User +#### User - `id`: Auto-incremented unique identifier for the user. - `email`: Unique email address of the user. @@ -126,7 +130,7 @@ model Database { - Relationships: - `teams` Many-to-many relationship with Team entity through TeamMembers property -##### Team +#### Team - `id` Auto-incremented unique identifier for the team - `name` Name of the team @@ -134,7 +138,7 @@ model Database { - `members` One-to-many relationship with team members - `projects` One-to-many relationship with projects -##### TeamMembers +#### TeamMembers - `teamId` Foreign key referencing the id column of the Team table, pointing to the team that the member belongs to - `userId` Foreign key referencing the id column of the User table, pointing to the user that is a member of the team @@ -143,7 +147,7 @@ model Database { - `team` Many-to-one relationship with teams - `user` Many-to-one relationship with users -##### Project +#### Project - `id` Auto-incremented unique identifier for the project - `name` Name of the project @@ -153,7 +157,7 @@ model Database { - `team` Many-to-one relationship with the team that creates the projects - `databases` One-to-many relationship with databases associated with the project -##### Database +#### Database - `id` Unique UUID identifier for the database - `name`Name of the database From 80630fc2ddddec2e0974816aca16eca7054cf377 Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Thu, 15 Feb 2024 01:28:53 +0200 Subject: [PATCH 14/19] Added a caption above the ERD diagram --- docs/schema.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/schema.md b/docs/schema.md index a6735cf..ee1265d 100644 --- a/docs/schema.md +++ b/docs/schema.md @@ -116,6 +116,7 @@ model Database { ### ERD Diagram +This ERD Diagram helps visualize the relationships between each entity in the schema and how they depend on each other ![ERD diagram of the schema](./prisma-erd.svg "ERD Diagram of the schema") ### Entity Details From 2d4bef655fa5ff4fe4e056e619127873ef9993fa Mon Sep 17 00:00:00 2001 From: Salma-Mahmoudd Date: Thu, 15 Feb 2024 21:24:45 +0200 Subject: [PATCH 15/19] Added teamController file, modified endpoints and join function handled with some database modifications --- package-lock.json | 62 +++---- package.json | 3 +- .../20240207163006_uuid/migration.sql | 8 - .../migration.sql | 6 + prisma/schema.prisma | 2 + requests.rest | 130 +++++++++++++++ src/app.js | 10 +- src/controllers/teamController.js | 106 ++++++++++++ src/models/teamModel.js | 156 ++++++++++-------- src/routes/teamRoute.js | 21 +++ 10 files changed, 393 insertions(+), 111 deletions(-) delete mode 100644 prisma/migrations/20240207163006_uuid/migration.sql rename prisma/migrations/{20240205003520_init => 20240215185513_init}/migration.sql (93%) create mode 100644 src/controllers/teamController.js create mode 100644 src/routes/teamRoute.js diff --git a/package-lock.json b/package-lock.json index 4f5c532..f08e2bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,9 +15,10 @@ "cookie-parser": "^1.4.6", "dotenv": "^16.4.1", "express": "^4.18.2", + "express-async-handler": "^1.2.0", "jsonwebtoken": "^9.0.2", "nodemon": "^3.0.3", - "prisma": "^5.8.1" + "prisma": "^5.9.1" } }, "node_modules/@mapbox/node-pre-gyp": { @@ -71,43 +72,43 @@ } }, "node_modules/@prisma/debug": { - "version": "5.8.1", - "resolved": "https://registry.npmjs.org/@prisma/debug/-/debug-5.8.1.tgz", - "integrity": "sha512-tjuw7eA0Us3T42jx9AmAgL58rzwzpFGYc3R7Y4Ip75EBYrKMBA1YihuWMcBC92ILmjlQ/u3p8VxcIE0hr+fZfg==" + "version": "5.9.1", + "resolved": "https://registry.npmjs.org/@prisma/debug/-/debug-5.9.1.tgz", + "integrity": "sha512-yAHFSFCg8KVoL0oRUno3m60GAjsUKYUDkQ+9BA2X2JfVR3kRVSJFc/GpQ2fSORi4pSHZR9orfM4UC9OVXIFFTA==" }, "node_modules/@prisma/engines": { - "version": "5.8.1", - "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-5.8.1.tgz", - "integrity": "sha512-TJgYLRrZr56uhqcXO4GmP5be+zjCIHtLDK20Cnfg+o9d905hsN065QOL+3Z0zQAy6YD31Ol4u2kzSfRmbJv/uA==", + "version": "5.9.1", + "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-5.9.1.tgz", + "integrity": "sha512-gkdXmjxQ5jktxWNdDA5aZZ6R8rH74JkoKq6LD5mACSvxd2vbqWeWIOV0Py5wFC8vofOYShbt6XUeCIUmrOzOnQ==", "hasInstallScript": true, "dependencies": { - "@prisma/debug": "5.8.1", - "@prisma/engines-version": "5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2", - "@prisma/fetch-engine": "5.8.1", - "@prisma/get-platform": "5.8.1" + "@prisma/debug": "5.9.1", + "@prisma/engines-version": "5.9.0-32.23fdc5965b1e05fc54e5f26ed3de66776b93de64", + "@prisma/fetch-engine": "5.9.1", + "@prisma/get-platform": "5.9.1" } }, "node_modules/@prisma/engines-version": { - "version": "5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2", - "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2.tgz", - "integrity": "sha512-f5C3JM3l9yhGr3cr4FMqWloFaSCpNpMi58Om22rjD2DOz3owci2mFdFXMgnAGazFPKrCbbEhcxdsRfspEYRoFQ==" + "version": "5.9.0-32.23fdc5965b1e05fc54e5f26ed3de66776b93de64", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.9.0-32.23fdc5965b1e05fc54e5f26ed3de66776b93de64.tgz", + "integrity": "sha512-HFl7275yF0FWbdcNvcSRbbu9JCBSLMcurYwvWc8WGDnpu7APxQo2ONtZrUggU3WxLxUJ2uBX+0GOFIcJeVeOOQ==" }, "node_modules/@prisma/fetch-engine": { - "version": "5.8.1", - "resolved": "https://registry.npmjs.org/@prisma/fetch-engine/-/fetch-engine-5.8.1.tgz", - "integrity": "sha512-+bgjjoSFa6uYEbAPlklfoVSStOEfcpheOjoBoNsNNSQdSzcwE2nM4Q0prun0+P8/0sCHo18JZ9xqa8gObvgOUw==", + "version": "5.9.1", + "resolved": "https://registry.npmjs.org/@prisma/fetch-engine/-/fetch-engine-5.9.1.tgz", + "integrity": "sha512-l0goQOMcNVOJs1kAcwqpKq3ylvkD9F04Ioe1oJoCqmz05mw22bNAKKGWuDd3zTUoUZr97va0c/UfLNru+PDmNA==", "dependencies": { - "@prisma/debug": "5.8.1", - "@prisma/engines-version": "5.8.1-1.78caf6feeaed953168c64e15a249c3e9a033ebe2", - "@prisma/get-platform": "5.8.1" + "@prisma/debug": "5.9.1", + "@prisma/engines-version": "5.9.0-32.23fdc5965b1e05fc54e5f26ed3de66776b93de64", + "@prisma/get-platform": "5.9.1" } }, "node_modules/@prisma/get-platform": { - "version": "5.8.1", - "resolved": "https://registry.npmjs.org/@prisma/get-platform/-/get-platform-5.8.1.tgz", - "integrity": "sha512-wnA+6HTFcY+tkykMokix9GiAkaauPC5W/gg0O5JB0J8tCTNWrqpnQ7AsaGRfkYUbeOIioh6woDjQrGTTRf1Zag==", + "version": "5.9.1", + "resolved": "https://registry.npmjs.org/@prisma/get-platform/-/get-platform-5.9.1.tgz", + "integrity": "sha512-6OQsNxTyhvG+T2Ksr8FPFpuPeL4r9u0JF0OZHUBI/Uy9SS43sPyAIutt4ZEAyqWQt104ERh70EZedkHZKsnNbg==", "dependencies": { - "@prisma/debug": "5.8.1" + "@prisma/debug": "5.9.1" } }, "node_modules/abbrev": { @@ -553,6 +554,11 @@ "node": ">= 0.10.0" } }, + "node_modules/express-async-handler": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/express-async-handler/-/express-async-handler-1.2.0.tgz", + "integrity": "sha512-rCSVtPXRmQSW8rmik/AIb2P0op6l7r1fMW538yyvTMltCO4xQEWMmobfrIxN2V1/mVrgxB8Az3reYF6yUZw37w==" + }, "node_modules/express/node_modules/body-parser": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", @@ -1337,12 +1343,12 @@ } }, "node_modules/prisma": { - "version": "5.8.1", - "resolved": "https://registry.npmjs.org/prisma/-/prisma-5.8.1.tgz", - "integrity": "sha512-N6CpjzECnUHZ5beeYpDzkt2rYpEdAeqXX2dweu6BoQaeYkNZrC/WJHM+5MO/uidFHTak8QhkPKBWck1o/4MD4A==", + "version": "5.9.1", + "resolved": "https://registry.npmjs.org/prisma/-/prisma-5.9.1.tgz", + "integrity": "sha512-Hy/8KJZz0ELtkw4FnG9MS9rNWlXcJhf98Z2QMqi0QiVMoS8PzsBkpla0/Y5hTlob8F3HeECYphBjqmBxrluUrQ==", "hasInstallScript": true, "dependencies": { - "@prisma/engines": "5.8.1" + "@prisma/engines": "5.9.1" }, "bin": { "prisma": "build/index.js" diff --git a/package.json b/package.json index 4bba0b6..1f65ba7 100644 --- a/package.json +++ b/package.json @@ -25,8 +25,9 @@ "cookie-parser": "^1.4.6", "dotenv": "^16.4.1", "express": "^4.18.2", + "express-async-handler": "^1.2.0", "jsonwebtoken": "^9.0.2", "nodemon": "^3.0.3", - "prisma": "^5.8.1" + "prisma": "^5.9.1" } } diff --git a/prisma/migrations/20240207163006_uuid/migration.sql b/prisma/migrations/20240207163006_uuid/migration.sql deleted file mode 100644 index 9d6acd1..0000000 --- a/prisma/migrations/20240207163006_uuid/migration.sql +++ /dev/null @@ -1,8 +0,0 @@ -/* - Warnings: - - - The required column `uuid` was added to the `Project` table with a prisma-level default value. This is not possible if the table is not empty. Please add this column as optional, then populate it before making it required. - -*/ --- AlterTable -ALTER TABLE "Project" ADD COLUMN "uuid" TEXT NOT NULL; diff --git a/prisma/migrations/20240205003520_init/migration.sql b/prisma/migrations/20240215185513_init/migration.sql similarity index 93% rename from prisma/migrations/20240205003520_init/migration.sql rename to prisma/migrations/20240215185513_init/migration.sql index 65d9ce7..e4b8487 100644 --- a/prisma/migrations/20240205003520_init/migration.sql +++ b/prisma/migrations/20240215185513_init/migration.sql @@ -22,6 +22,8 @@ CREATE TABLE "User" ( CREATE TABLE "Team" ( "id" SERIAL NOT NULL, "name" TEXT NOT NULL, + "token" TEXT, + "expiry" TIMESTAMP(3), CONSTRAINT "Team_pkey" PRIMARY KEY ("id") ); @@ -40,6 +42,7 @@ CREATE TABLE "Project" ( "id" SERIAL NOT NULL, "name" TEXT NOT NULL, "teamId" INTEGER NOT NULL, + "uuid" TEXT NOT NULL, CONSTRAINT "Project_pkey" PRIMARY KEY ("id") ); @@ -59,6 +62,9 @@ CREATE TABLE "Database" ( -- CreateIndex CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); +-- CreateIndex +CREATE UNIQUE INDEX "Team_token_key" ON "Team"("token"); + -- AddForeignKey ALTER TABLE "TeamMembers" ADD CONSTRAINT "TeamMembers_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 0cc89c6..35c3a7c 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -19,6 +19,8 @@ model User { model Team { id Int @id @default(autoincrement()) name String + token String? @unique + expiry DateTime? members TeamMembers[] projects Project[] } diff --git a/requests.rest b/requests.rest index 9dd4c0a..61a1abb 100644 --- a/requests.rest +++ b/requests.rest @@ -58,4 +58,134 @@ Content-Type: application/json { "teamId": 1 +} + + +//______________________________________________________ + +### +// Register user + +POST http://localhost:3000/register +Content-Type: application/json + +{ + "email": "s@g.com", + "username": "salma", + "password": "1234" +} + +### +// Login user + +POST http://localhost:3000/login +Content-Type: application/json + +{ + "email": "s@g.com", + "password": "1234" +} + +### +// Create team + +POST http://localhost:3000/team +Content-Type: application/json + +{ + "teamName": "team5", + "leaderId": 1 +} + +### +// Add member to team + +PUT http://localhost:3000/team/member +Content-Type: application/json + +{ + "memberEmail": "s@g.com", + "teamId": 5, + "userId": 1 +} + +### +// Get code of team + +PUT http://localhost:3000/team/code +Content-Type: application/json + +{ + "teamId": 1, + "userId": 2 +} + + +### +// Join a team + +PUT http://localhost:3000/team/join +Content-Type: application/json + +{ + "token": "eaff7ffa", + "userId": 1 +} + + +### +// Update team + +PUT http://localhost:3000/team +Content-Type: application/json + +{ + "name": "sssss", + "teamId": 2, + "userId": 2 +} + +### +// Get all users of the team + +GET http://localhost:3000/team/users +Content-Type: application/json + +{ + "teamId": 5, + "userId": 1 +} + +### +// List all teams of the user + +GET http://localhost:3000/team +Content-Type: application/json + +{ + "email": "s@g.com" +} + +### +// Delete member from team + +DELETE http://localhost:3000/team/member +Content-Type: application/json + +{ + "memberEmail": "amr11@g.com", + "teamId": 1, + "userId": 2 +} + + +### +// Delete team + +DELETE http://localhost:3000/team +Content-Type: application/json + +{ + "teamId": 3, + "userId": 2 } \ No newline at end of file diff --git a/src/app.js b/src/app.js index 7fbd987..0eeb346 100644 --- a/src/app.js +++ b/src/app.js @@ -1,16 +1,15 @@ import express from "express"; -import bodyParser from "body-parser"; import userRoute from "./routes/userRoute.js"; import cookieParser from "cookie-parser"; import { tokenAuth } from "./middlewares/authMiddleware.js"; import projectRoute from "./routes/projectRoute.js"; +import teamRoute from "./routes/teamRoute.js"; const app = express(); const port = 3000; app.use(express.json()); -app.use(bodyParser.json()); -app.use(bodyParser.urlencoded({ extended: true })); +app.use(express.urlencoded({ extended: true })); app.use(cookieParser()); app.use("/", userRoute); @@ -19,7 +18,12 @@ app.get("/", tokenAuth, (req, res) => { }); app.use("/project", projectRoute); +app.use("/team", teamRoute); app.listen(port, () => { console.log(`Server is running on port ${port}`); }); + +app.use((err, req, res, next) => { + res.status(500).json({ message: "Internal server error" }); +}); diff --git a/src/controllers/teamController.js b/src/controllers/teamController.js new file mode 100644 index 0000000..fba3092 --- /dev/null +++ b/src/controllers/teamController.js @@ -0,0 +1,106 @@ +import * as teamModel from "../models/teamModel.js"; +import { getUser } from "../models/userModel.js"; +import asyncHandler from "express-async-handler"; + +export async function isLeader(teamId, userId) { + const user = await teamModel.memberExist(teamId, userId); + if (user.role === "LEADER") { + return true; + } + return false; +} + +export const createTeam = asyncHandler(async (req, res) => { + const { teamName, leaderId } = req.body; + const newTeam = await teamModel.createTeam(teamName, leaderId); + res.status(201).json({ message: "Team created successfully" }); +}); + +export const addMember = asyncHandler(async (req, res) => { + const { memberEmail, teamId, userId } = req.body; + if (await isLeader(teamId, userId)) { + const addedMember = await teamModel.addMember(memberEmail, teamId); + if (addedMember) { + res.status(201).json({ message: "Member added successfully" }); + } else { + res.status(409).json({ message: "Member already exists in the team" }); + } + } else { + res.status(401).json({ message: "Unauthorized: user is not LEADER" }); + } +}); + +export const deleteMember = asyncHandler(async (req, res) => { + const { memberEmail, teamId, userId } = req.body; + if (await isLeader(teamId, userId)) { + const deletedMember = await teamModel.deleteMember(memberEmail, teamId); + if (deletedMember) { + res.status(201).json({ message: "Member deleted successfully" }); + } else { + res.status(409).json({ message: "Member does not exist in the team" }); + } + } else { + res.status(401).json({ message: "Unauthorized: user is not LEADER" }); + } +}); + +export const updateTeam = asyncHandler(async (req, res) => { + const { newName, teamId, userId } = req.body; + if (await isLeader(teamId, userId)) { + await teamModel.updateTeam(newName, teamId); + res.status(201).json({ message: "Team updated successfully" }); + } else { + res.status(401).json({ message: "Unauthorized: user is not LEADER" }); + } +}); + +export const deleteTeam = asyncHandler(async (req, res) => { + const { teamId, userId } = req.body; + if (await isLeader(teamId, userId)) { + await teamModel.deleteTeam(teamId); + res.status(201).json({ message: "Team deleted successfully" }); + } else { + res.status(401).json({ message: "Unauthorized: user is not LEADER" }); + } +}); + +export const getUsersInTeam = asyncHandler(async (req, res) => { + const teamId = req.body.teamId; + const team = await teamModel.getTeam(teamId); + res.status(200).json(team[0].members.map((member) => member.user.email)); +}); + +export const allTeamsOfUser = asyncHandler(async (req, res) => { + const email = req.body.email; + const user = await getUser(email); + if (user.teams) { + res.status(200).json( + user.teams.map((team) => ({ + role: team.role, + teamName: team.team.name, + })) + ); + } else { + res.status(409).json({ message: "No teams found" }); + } +}); + +export const teamToken = asyncHandler(async (req, res) => { + const { teamId, userId } = req.body; + if (await isLeader(teamId, userId)) { + const token = await teamModel.tokenForTeam(teamId); + res.status(200).json({ Code: token }); + } else { + res.status(401).json({ message: "Unauthorized: user is not LEADER" }); + } +}); + +export const joinTeam = asyncHandler(async (req, res) => { + const { token, userId } = req.body; + const joinedTeam = await teamModel.joinTeam(token, userId); + if (joinedTeam) { + res.status(201).json({ message: "Joined team successfully" }); + } else { + res.status(402).json({ message: "Invalid code" }); + } +}); diff --git a/src/models/teamModel.js b/src/models/teamModel.js index 1b376a5..ee2195b 100644 --- a/src/models/teamModel.js +++ b/src/models/teamModel.js @@ -1,98 +1,112 @@ -import { prisma } from "../utils/db.js" -import { getUserId } from "./userModel.js" -export async function createTeam(leader_id, Name) { +import { prisma } from "../utils/db.js"; +import { getUserId } from "./userModel.js"; +import crypto from "crypto"; + +export async function memberExist(teamId, userId) { + const exist = await prisma.teamMembers.findUnique({ + where: { + teamId_userId: { + teamId: teamId, + userId: userId, + }, + }, + }); + return exist; +} + +export async function createTeam(teamName, leaderId) { const team = await prisma.team.create({ data: { - name: Name, + name: teamName, members: { create: [ { role: "LEADER", - user: { - connect: { id: leader_id }, - }, + user: { connect: { id: leaderId } }, }, ], }, }, - }) - return team -} -export async function memberExist(team_id, user_id) { - const exist = await prisma.teamMembers.findUnique({ - where: { - teamId_userId: { - teamId: team_id, - userId: user_id, - }, - }, - }) - return exist + }); + return team; } -export async function addMember(team_id, memberEmail) { - const user_id = await getUserId(memberEmail) - if (!user_id) { - return false - } - if (!(await memberExist(team_id, user_id.id))) { + +export async function addMember(memberEmail, teamId) { + const member = await getUserId(memberEmail); + if (!(await memberExist(teamId, member.id))) { await prisma.teamMembers.create({ data: { - user: { - connect: { id: user_id.id }, - }, - team: { - connect: { id: team_id }, - }, + user: { connect: { id: member.id } }, + team: { connect: { id: teamId } }, }, - }) - return true - } else { - return false + }); + return true; } + return false; } -export async function deleteMember(team_id, e_mail) { - const user = await getUserId(e_mail) - if (!user) { - return false - } - await prisma.teamMembers.delete({ - where: { - teamId_userId: { - teamId: team_id, - userId: user.id, +export async function deleteMember(memberEmail, teamId) { + const member = await getUserId(memberEmail); + if (await memberExist(teamId, member.id)) { + await prisma.teamMembers.delete({ + where: { + teamId_userId: { + teamId: teamId, + userId: member.id, + }, }, - }, - }) - return true + }); + return true; + } + return false; } -export async function getTeam(team_id) { - const team = await prisma.team.findUnique({ - where: { - id: team_id, - }, - include: { members: { include: { user: true } } }, - }) - return team +export async function getTeam(teamId) { + const team = await prisma.team.findMany({ + where: { id: teamId }, + select: { members: { select: { user: true } } }, + }); + return team; } -export async function updateTeam(team_id, Name) { +export async function updateTeam(newName, teamId) { const team = await prisma.team.update({ - where: { - id: team_id, - }, - data: { - name: Name, - }, - }) - return team + where: { id: teamId }, + data: { name: newName }, + }); + return team; } -export async function deleteTeam(team_id) { +export async function deleteTeam(teamId) { await prisma.team.delete({ - where: { - id: team_id, - }, - }) + where: { id: teamId }, + }); +} + +export async function tokenForTeam(teamId) { + let token; + do { + token = crypto.randomBytes(8).toString("hex").substring(0, 8); + } while (await prisma.team.findUnique({ where: { token } })); + await prisma.team.update({ + where: { id: teamId }, + data: { token: token, expiry: new Date(Date.now() + 24 * 60 * 60 * 1000) }, + }); + return token; +} + +export async function joinTeam(token, userId) { + const team = await prisma.team.findFirst({ + where: { token: token, expiry: { gt: new Date() } }, + }); + if (team) { + await prisma.teamMembers.create({ + data: { + user: { connect: { id: userId } }, + team: { connect: { id: team.id } }, + }, + }); + return true; + } + return false; } diff --git a/src/routes/teamRoute.js b/src/routes/teamRoute.js new file mode 100644 index 0000000..2958ce0 --- /dev/null +++ b/src/routes/teamRoute.js @@ -0,0 +1,21 @@ +import express from "express"; +import { tokenAuth } from "../middlewares/authMiddleware.js"; +import * as teamController from "../controllers/teamController.js"; + +const router = express.Router(); + +router + .route("/") + .get(tokenAuth, teamController.allTeamsOfUser) + .post(tokenAuth, teamController.createTeam) + .delete(tokenAuth, teamController.deleteTeam) + .put(tokenAuth, teamController.updateTeam); +router + .route("/member") + .put(tokenAuth, teamController.addMember) + .delete(tokenAuth, teamController.deleteMember); +router.get("/users", tokenAuth, teamController.getUsersInTeam); +router.put("/join", tokenAuth, teamController.joinTeam); +router.put("/code", tokenAuth, teamController.teamToken); + +export default router; From 903350c4f75eb3737a56a150c6b30e8bc125abdd Mon Sep 17 00:00:00 2001 From: Salma-Mahmoudd Date: Fri, 16 Feb 2024 19:48:44 +0200 Subject: [PATCH 16/19] Using the reg.user --- src/controllers/teamController.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/controllers/teamController.js b/src/controllers/teamController.js index fba3092..b4b8534 100644 --- a/src/controllers/teamController.js +++ b/src/controllers/teamController.js @@ -11,13 +11,15 @@ export async function isLeader(teamId, userId) { } export const createTeam = asyncHandler(async (req, res) => { - const { teamName, leaderId } = req.body; + const { teamName } = req.body; + const leaderId = req.user.id; const newTeam = await teamModel.createTeam(teamName, leaderId); res.status(201).json({ message: "Team created successfully" }); }); export const addMember = asyncHandler(async (req, res) => { - const { memberEmail, teamId, userId } = req.body; + const { memberEmail, teamId } = req.body; + const userId = req.user.id; if (await isLeader(teamId, userId)) { const addedMember = await teamModel.addMember(memberEmail, teamId); if (addedMember) { @@ -31,7 +33,8 @@ export const addMember = asyncHandler(async (req, res) => { }); export const deleteMember = asyncHandler(async (req, res) => { - const { memberEmail, teamId, userId } = req.body; + const { memberEmail, teamId } = req.body; + const userId = req.user.id; if (await isLeader(teamId, userId)) { const deletedMember = await teamModel.deleteMember(memberEmail, teamId); if (deletedMember) { @@ -45,7 +48,8 @@ export const deleteMember = asyncHandler(async (req, res) => { }); export const updateTeam = asyncHandler(async (req, res) => { - const { newName, teamId, userId } = req.body; + const { newName, teamId } = req.body; + const userId = req.user.id; if (await isLeader(teamId, userId)) { await teamModel.updateTeam(newName, teamId); res.status(201).json({ message: "Team updated successfully" }); @@ -55,7 +59,8 @@ export const updateTeam = asyncHandler(async (req, res) => { }); export const deleteTeam = asyncHandler(async (req, res) => { - const { teamId, userId } = req.body; + const { teamId } = req.body; + const userId = req.user.id; if (await isLeader(teamId, userId)) { await teamModel.deleteTeam(teamId); res.status(201).json({ message: "Team deleted successfully" }); @@ -65,13 +70,13 @@ export const deleteTeam = asyncHandler(async (req, res) => { }); export const getUsersInTeam = asyncHandler(async (req, res) => { - const teamId = req.body.teamId; + const { teamId } = req.body; const team = await teamModel.getTeam(teamId); res.status(200).json(team[0].members.map((member) => member.user.email)); }); export const allTeamsOfUser = asyncHandler(async (req, res) => { - const email = req.body.email; + const email = req.user.email; const user = await getUser(email); if (user.teams) { res.status(200).json( @@ -86,7 +91,8 @@ export const allTeamsOfUser = asyncHandler(async (req, res) => { }); export const teamToken = asyncHandler(async (req, res) => { - const { teamId, userId } = req.body; + const { teamId } = req.body; + const userId = req.user.id; if (await isLeader(teamId, userId)) { const token = await teamModel.tokenForTeam(teamId); res.status(200).json({ Code: token }); @@ -96,7 +102,8 @@ export const teamToken = asyncHandler(async (req, res) => { }); export const joinTeam = asyncHandler(async (req, res) => { - const { token, userId } = req.body; + const { token } = req.body; + const userId = req.user.id; const joinedTeam = await teamModel.joinTeam(token, userId); if (joinedTeam) { res.status(201).json({ message: "Joined team successfully" }); From 3f74eee45127bb673fcaaffe7c56851a296bc7c5 Mon Sep 17 00:00:00 2001 From: Salma-Mahmoudd Date: Fri, 16 Feb 2024 21:11:48 +0200 Subject: [PATCH 17/19] For testing team controller --- requests.rest | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/requests.rest b/requests.rest index 61a1abb..05b6638 100644 --- a/requests.rest +++ b/requests.rest @@ -93,8 +93,7 @@ POST http://localhost:3000/team Content-Type: application/json { - "teamName": "team5", - "leaderId": 1 + "teamName": "team9" } ### @@ -104,9 +103,8 @@ PUT http://localhost:3000/team/member Content-Type: application/json { - "memberEmail": "s@g.com", - "teamId": 5, - "userId": 1 + "memberEmail": "amr11@g.com", + "teamId": 5 } ### @@ -116,8 +114,7 @@ PUT http://localhost:3000/team/code Content-Type: application/json { - "teamId": 1, - "userId": 2 + "teamId": 9 } @@ -128,8 +125,7 @@ PUT http://localhost:3000/team/join Content-Type: application/json { - "token": "eaff7ffa", - "userId": 1 + "token": "2c1ce63e" } @@ -140,9 +136,8 @@ PUT http://localhost:3000/team Content-Type: application/json { - "name": "sssss", - "teamId": 2, - "userId": 2 + "newName": "sssss", + "teamId": 9 } ### @@ -152,8 +147,7 @@ GET http://localhost:3000/team/users Content-Type: application/json { - "teamId": 5, - "userId": 1 + "teamId": 9 } ### @@ -162,9 +156,7 @@ Content-Type: application/json GET http://localhost:3000/team Content-Type: application/json -{ - "email": "s@g.com" -} + ### // Delete member from team @@ -174,8 +166,7 @@ Content-Type: application/json { "memberEmail": "amr11@g.com", - "teamId": 1, - "userId": 2 + "teamId": 4 } @@ -186,6 +177,5 @@ DELETE http://localhost:3000/team Content-Type: application/json { - "teamId": 3, - "userId": 2 + "teamId": 8 } \ No newline at end of file From 6769c9fa563afdfdc6c4a72cc7242cac4a3720f5 Mon Sep 17 00:00:00 2001 From: Salma-Mahmoudd Date: Sat, 17 Feb 2024 03:08:05 +0200 Subject: [PATCH 18/19] Edit team route --- requests.rest | 4 ++-- src/controllers/teamController.js | 2 +- src/routes/teamRoute.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/requests.rest b/requests.rest index 05b6638..a461380 100644 --- a/requests.rest +++ b/requests.rest @@ -143,11 +143,11 @@ Content-Type: application/json ### // Get all users of the team -GET http://localhost:3000/team/users +GET http://localhost:3000/team/member Content-Type: application/json { - "teamId": 9 + "teamId": 7 } ### diff --git a/src/controllers/teamController.js b/src/controllers/teamController.js index b4b8534..5b6eb6e 100644 --- a/src/controllers/teamController.js +++ b/src/controllers/teamController.js @@ -69,7 +69,7 @@ export const deleteTeam = asyncHandler(async (req, res) => { } }); -export const getUsersInTeam = asyncHandler(async (req, res) => { +export const getMembers = asyncHandler(async (req, res) => { const { teamId } = req.body; const team = await teamModel.getTeam(teamId); res.status(200).json(team[0].members.map((member) => member.user.email)); diff --git a/src/routes/teamRoute.js b/src/routes/teamRoute.js index 2958ce0..3a0aba3 100644 --- a/src/routes/teamRoute.js +++ b/src/routes/teamRoute.js @@ -12,9 +12,9 @@ router .put(tokenAuth, teamController.updateTeam); router .route("/member") + .get(tokenAuth, teamController.getMembers) .put(tokenAuth, teamController.addMember) .delete(tokenAuth, teamController.deleteMember); -router.get("/users", tokenAuth, teamController.getUsersInTeam); router.put("/join", tokenAuth, teamController.joinTeam); router.put("/code", tokenAuth, teamController.teamToken); From 9beb9bdcdaa1bf3d7ac3910404215ee6e1aaad91 Mon Sep 17 00:00:00 2001 From: reem Date: Mon, 19 Feb 2024 18:51:45 +0200 Subject: [PATCH 19/19] adding middleware --- src/controllers/databaseController.js | 78 +++++---------------------- src/middlewares/databaseMiddleware.js | 31 +++++++++++ src/routes/databaseRoute.js | 32 +++++++++-- 3 files changed, 72 insertions(+), 69 deletions(-) create mode 100644 src/middlewares/databaseMiddleware.js diff --git a/src/controllers/databaseController.js b/src/controllers/databaseController.js index db75b65..2b2b4c9 100644 --- a/src/controllers/databaseController.js +++ b/src/controllers/databaseController.js @@ -1,25 +1,13 @@ -import { getProject } from "../models/projectModel.js"; -import { memberExist } from "../models/teamModel.js"; import * as dataModel from "../models/databaseModel.js"; +export const getDatabase = async (req, res) => { + const { projectId } = req.query; + const databases = await dataModel.allDatabases(+projectId); + res.status(200).json({ databases: databases }); +}; + export const createDatabase = async (req, res) => { const { projectId, databaseName } = req.body; - const project = await getProject(projectId); - if (!project) { - return res.status(404).json({ message: "project doesn't exist" }); - } - const user_id = req.user.id; - const teamMember = await memberExist(project.teamId, user_id); - if (!teamMember) { - return res - .status(403) - .json({ message: "user doesn't exist in the project" }); - } - if (teamMember.role === "MEMBER") { - return res - .status(403) - .json({ message: "user doesn't have permissions to create a database" }); - } const database = await dataModel.createDatabase(projectId, databaseName); res.status(201).json({ message: "database created successfully", @@ -27,40 +15,13 @@ export const createDatabase = async (req, res) => { }); }; -export const getDatabase = async (req, res) => { - const { projectId } = req.query; - const project = await getProject(+projectId); - if (!project) { - return res.status(404).json({ message: "project doesn't exist" }); - } - const user_id = req.user.id; - if (!(await memberExist(project.teamId, user_id))) { - return res - .status(403) - .json({ message: "user doesn't exist in the project" }); - } - const databases = await dataModel.allDatabases(+projectId); - res.status(200).json({ databases: databases }); -}; - export const updateDatabase = async (req, res) => { - const { databaseId, databaseName } = req.body; + const { projectId, databaseId, databaseName } = req.body; const database = await dataModel.getDatabase(databaseId); - if (!database) { - return res.status(404).json({ message: "database doesn't exist" }); - } - const project = await getProject(database.projectId); - const user_id = req.user.id; - const teamMember = await memberExist(project.teamId, user_id); - if (!teamMember) { - return res - .status(403) - .json({ message: "user doesn't exist in the project" }); - } - if (teamMember.role === "MEMBER") { + if (!database || database.projectId != projectId) { return res - .status(403) - .json({ message: "user doesn't have permissions to update database" }); + .status(404) + .json({ message: "database doesn't exist in the project" }); } const updated_database = await dataModel.updateDatabase( databaseId, @@ -73,23 +34,12 @@ export const updateDatabase = async (req, res) => { }; export const deleteDatabase = async (req, res) => { - const { databaseId } = req.body; + const { projectId, databaseId } = req.body; const database = await dataModel.getDatabase(databaseId); - if (!database) { - return res.status(404).json({ message: "database doesn't exist" }); - } - const project = await getProject(database.projectId); - const user_id = req.user.id; - const teamMember = await memberExist(project.teamId, user_id); - if (!teamMember) { - return res - .status(403) - .json({ message: "user doesn't exist in the project" }); - } - if (teamMember.role === "MEMBER") { + if (!database || database.projectId != projectId) { return res - .status(403) - .json({ message: "user doesn't have permissions to delete database" }); + .status(404) + .json({ message: "database doesn't exist in the project" }); } await dataModel.deleteDatabase(databaseId); res.status(204); diff --git a/src/middlewares/databaseMiddleware.js b/src/middlewares/databaseMiddleware.js new file mode 100644 index 0000000..7cc1f90 --- /dev/null +++ b/src/middlewares/databaseMiddleware.js @@ -0,0 +1,31 @@ +import { getProject } from "../models/projectModel.js"; +import { memberExist } from "../models/teamModel.js"; + +export const databaseAccess = async (req, res, next) => { + let { projectId } = req.body; + if (!projectId) { + projectId = req.query.projectId; + } + const project = await getProject(+projectId); + if (!project) { + return res.status(404).json({ message: "project doesn't exist" }); + } + const user_id = req.user.id; + const teamMember = await memberExist(project.teamId, user_id); + if (!teamMember) { + return res + .status(403) + .json({ message: "user doesn't exist in the project" }); + } + req.role = teamMember.role; + next(); +}; + +export const authorizedUser = async (req, res, next) => { + if (req.role != "LEADER") { + return res + .status(403) + .json({ message: "user doesn't have permissions to create a database" }); + } + next(); +}; diff --git a/src/routes/databaseRoute.js b/src/routes/databaseRoute.js index 321fb97..58d846b 100644 --- a/src/routes/databaseRoute.js +++ b/src/routes/databaseRoute.js @@ -1,12 +1,34 @@ import express from "express"; import * as control from "../controllers/databaseController.js"; import { tokenAuth } from "../middlewares/authMiddleware.js"; +import { + authorizedUser, + databaseAccess, +} from "../middlewares/databaseMiddleware.js"; const route = express.Router(); -route.get("/", tokenAuth, control.getDatabase); -route.post("/create", tokenAuth, control.createDatabase); -route.put("/update", tokenAuth, control.updateDatabase); -route.delete("/delete", tokenAuth, control.deleteDatabase); +route.get("/", tokenAuth, databaseAccess, control.getDatabase); +route.post( + "/", + tokenAuth, + databaseAccess, + authorizedUser, + control.createDatabase +); +route.put( + "/", + tokenAuth, + databaseAccess, + authorizedUser, + control.updateDatabase +); +route.delete( + "/", + tokenAuth, + databaseAccess, + authorizedUser, + control.deleteDatabase +); -export default route; \ No newline at end of file +export default route;