From 1aa26471e88a641dd6a110ed9acbcdd48caacaa1 Mon Sep 17 00:00:00 2001 From: AmrMohamed27 Date: Mon, 5 Feb 2024 06:01:44 +0200 Subject: [PATCH 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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;