diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 00000000..bb13dfc4 --- /dev/null +++ b/.github/workflows/test.yml-template @@ -0,0 +1,23 @@ +name: Test + +on: + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [20.x] + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - run: npm install + - run: npm test diff --git a/.gitignore b/.gitignore index ed48a299..bd6a178a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,3 @@ node_modules # MacOS .DS_Store - -# env files -*.env -.env* diff --git a/package-lock.json b/package-lock.json index 288d83dd..ca6a9c71 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,16 @@ "version": "1.0.0", "hasInstallScript": true, "license": "GPL-3.0", + "dependencies": { + "bcryptjs": "^2.4.3", + "express": "^4.18.2", + "jsonwebtoken": "^9.0.2", + "nodemailer": "^6.9.9", + "uuid": "^9.0.1" + }, "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^2.1.3", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", @@ -1467,10 +1474,11 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.3.tgz", + "integrity": "sha512-a07wHTj/1QUK2Aac5zHad+sGw4rIvcNl5lJmJpAD7OxeSbnCdyI6RXUHwXhjF5MaVo9YHrJ0xVahyERS2IIyBQ==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", @@ -2198,6 +2206,19 @@ "integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==", "dev": true }, + "node_modules/accepts": { + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", + "integrity": "sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==", + "license": "MIT", + "dependencies": { + "mime-types": "~2.1.34", + "negotiator": "0.6.3" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/acorn": { "version": "8.12.1", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.12.1.tgz", @@ -2306,6 +2327,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==", + "license": "MIT" + }, "node_modules/array-includes": { "version": "3.1.8", "resolved": "https://registry.npmjs.org/array-includes/-/array-includes-3.1.8.tgz", @@ -2620,6 +2647,12 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "dev": true }, + "node_modules/bcryptjs": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", + "integrity": "sha512-V/Hy/X9Vt7f3BbPJEi8BdVFMByHi+jNXrYkW3huaybV/kQ0KJg0Y6PkEMbn+zeT+i+SiKZ/HMqJGIIt4LZDqNQ==", + "license": "MIT" + }, "node_modules/before-after-hook": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-3.0.2.tgz", @@ -2627,6 +2660,45 @@ "dev": true, "peer": true }, + "node_modules/body-parser": { + "version": "1.20.4", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.4.tgz", + "integrity": "sha512-ZTgYYLMOXY9qKU/57FAo8F+HA2dGX7bqGc71txDRC1rS4frdFI5R7NhluHxH6M0YItAP0sHB4uqAOcYKxO6uGA==", + "license": "MIT", + "dependencies": { + "bytes": "~3.1.2", + "content-type": "~1.0.5", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "~1.2.0", + "http-errors": "~2.0.1", + "iconv-lite": "~0.4.24", + "on-finished": "~2.4.1", + "qs": "~6.14.0", + "raw-body": "~2.5.3", + "type-is": "~1.6.18", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, + "node_modules/body-parser/node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "license": "MIT", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/body-parser/node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", + "license": "MIT" + }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -2690,12 +2762,27 @@ "node-int64": "^0.4.0" } }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==", + "license": "BSD-3-Clause" + }, "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", "dev": true }, + "node_modules/bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/call-bind": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.7.tgz", @@ -2715,6 +2802,35 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/call-bound": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz", + "integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "get-intrinsic": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/callsites": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", @@ -2857,12 +2973,48 @@ "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", "dev": true }, + "node_modules/content-disposition": { + "version": "0.5.4", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", + "integrity": "sha512-FveZTNuGw04cxlAiWbzi6zTAL/lhehaWbTtgluJh4/E95DqMwTmha3KZN1aAWA8cFIhHzMZUvLevkw5Rqk+tSQ==", + "license": "MIT", + "dependencies": { + "safe-buffer": "5.2.1" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/content-type": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/convert-source-map": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==", "dev": true }, + "node_modules/cookie": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz", + "integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie-signature": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.7.tgz", + "integrity": "sha512-NXdYc3dLr47pBkpUCHtKSwIOQXLVn8dZEuywboCOJY/osA0wFSLlSawr3KN8qXJEyX66FcONTH8EIlVuK0yyFA==", + "license": "MIT" + }, "node_modules/create-jest": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/create-jest/-/create-jest-29.7.0.tgz", @@ -3117,12 +3269,31 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/depd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/deprecation": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/deprecation/-/deprecation-2.3.1.tgz", "integrity": "sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==", "dev": true }, + "node_modules/destroy": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", + "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==", + "license": "MIT", + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, "node_modules/detect-newline": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/detect-newline/-/detect-newline-3.1.0.tgz", @@ -3183,6 +3354,35 @@ "node": ">=10" } }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "license": "Apache-2.0", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, + "node_modules/ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==", + "license": "MIT" + }, "node_modules/electron-to-chromium": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.0.tgz", @@ -3207,6 +3407,15 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, + "node_modules/encodeurl": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz", + "integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/end-of-stream": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", @@ -3286,13 +3495,10 @@ } }, "node_modules/es-define-property": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.0.tgz", - "integrity": "sha512-jxayLKShrEqqzJ0eumQbVhTYQM27CfT1T35+gCgDFoL82JLsXqTJ76zv6A0YLOgEnLUMvLzsDsGIrl8NFpT2gQ==", - "dev": true, - "dependencies": { - "get-intrinsic": "^1.2.4" - }, + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", "engines": { "node": ">= 0.4" } @@ -3301,16 +3507,15 @@ "version": "1.3.0", "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", - "dev": true, "engines": { "node": ">= 0.4" } }, "node_modules/es-object-atoms": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.0.0.tgz", - "integrity": "sha512-MZ4iQ6JwHOBQjahnjwaC1ZtIBH+2ohjamzAO3oaHcXYup7qxjF2fixyH+Q71voWHeOkI2q/TnJao/KfXYIZWbw==", - "dev": true, + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", + "integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==", + "license": "MIT", "dependencies": { "es-errors": "^1.3.0" }, @@ -3367,6 +3572,12 @@ "node": ">=6" } }, + "node_modules/escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==", + "license": "MIT" + }, "node_modules/escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", @@ -3974,6 +4185,15 @@ "node": ">=0.10.0" } }, + "node_modules/etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/execa": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz", @@ -4022,6 +4242,73 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, + "node_modules/express": { + "version": "4.22.1", + "resolved": "https://registry.npmjs.org/express/-/express-4.22.1.tgz", + "integrity": "sha512-F2X8g9P1X7uCPZMA3MVf9wcTqlyNp7IhH5qPCI0izhaOIYXaW9L535tGA3qmjRzpH+bZczqq7hVKxTR4NWnu+g==", + "license": "MIT", + "dependencies": { + "accepts": "~1.3.8", + "array-flatten": "1.1.1", + "body-parser": "~1.20.3", + "content-disposition": "~0.5.4", + "content-type": "~1.0.4", + "cookie": "~0.7.1", + "cookie-signature": "~1.0.6", + "debug": "2.6.9", + "depd": "2.0.0", + "encodeurl": "~2.0.0", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "~1.3.1", + "fresh": "~0.5.2", + "http-errors": "~2.0.0", + "merge-descriptors": "1.0.3", + "methods": "~1.1.2", + "on-finished": "~2.4.1", + "parseurl": "~1.3.3", + "path-to-regexp": "~0.1.12", + "proxy-addr": "~2.0.7", + "qs": "~6.14.0", + "range-parser": "~1.2.1", + "safe-buffer": "5.2.1", + "send": "~0.19.0", + "serve-static": "~1.16.2", + "setprototypeof": "1.2.0", + "statuses": "~2.0.1", + "type-is": "~1.6.18", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + }, + "engines": { + "node": ">= 0.10.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/express/node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "license": "MIT", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/express/node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", + "license": "MIT" + }, + "node_modules/express/node_modules/path-to-regexp": { + "version": "0.1.12", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.12.tgz", + "integrity": "sha512-RA1GjUVMnvYFxuqovrEqZoxxW5NUZqbwKtYz/Tt7nXerk0LbLblQmrsgdeOxV5SFHf0UDggjS/bSeOZwt1pmEQ==", + "license": "MIT" + }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -4116,6 +4403,39 @@ "node": ">=8" } }, + "node_modules/finalhandler": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.3.2.tgz", + "integrity": "sha512-aA4RyPcd3badbdABGDuTXCMTtOneUCAYH/gxoYRTZlIJdF0YPWuGqiAsIrhNnnqdXGswYk6dGujem4w80UJFhg==", + "license": "MIT", + "dependencies": { + "debug": "2.6.9", + "encodeurl": "~2.0.0", + "escape-html": "~1.0.3", + "on-finished": "~2.4.1", + "parseurl": "~1.3.3", + "statuses": "~2.0.2", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/finalhandler/node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "license": "MIT", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/finalhandler/node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", + "license": "MIT" + }, "node_modules/find-up": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", @@ -4161,6 +4481,24 @@ "is-callable": "^1.1.3" } }, + "node_modules/forwarded": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", + "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/fs-extra": { "version": "9.1.0", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", @@ -4200,7 +4538,6 @@ "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", - "dev": true, "funding": { "url": "https://github.com/sponsors/ljharb" } @@ -4251,16 +4588,21 @@ } }, "node_modules/get-intrinsic": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.4.tgz", - "integrity": "sha512-5uYhsJH8VJBTv7oslg4BznJYhDoRI6waYCxMmCdnTrcCrHA/fCFKoTFz2JKKE0HdDFUF7/oQuhzumXJK7paBRQ==", - "dev": true, + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "license": "MIT", "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", "function-bind": "^1.1.2", - "has-proto": "^1.0.1", - "has-symbols": "^1.0.3", - "hasown": "^2.0.0" + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" }, "engines": { "node": ">= 0.4" @@ -4290,6 +4632,19 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/get-stream": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz", @@ -4398,12 +4753,12 @@ } }, "node_modules/gopd": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz", - "integrity": "sha512-d65bNlIadxvpb/A2abVdlqKqV563juRnZ1Wtk6s1sIR8uNsXR70xqIzVqxVf1eTqDunwT2MkczEeaezCKTZhwA==", - "dev": true, - "dependencies": { - "get-intrinsic": "^1.1.3" + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", + "engines": { + "node": ">= 0.4" }, "funding": { "url": "https://github.com/sponsors/ljharb" @@ -4464,10 +4819,10 @@ } }, "node_modules/has-symbols": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.3.tgz", - "integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==", - "dev": true, + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "license": "MIT", "engines": { "node": ">= 0.4" }, @@ -4494,7 +4849,6 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", - "dev": true, "dependencies": { "function-bind": "^1.1.2" }, @@ -4508,6 +4862,26 @@ "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==", "dev": true }, + "node_modules/http-errors": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz", + "integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==", + "license": "MIT", + "dependencies": { + "depd": "~2.0.0", + "inherits": "~2.0.4", + "setprototypeof": "~1.2.0", + "statuses": "~2.0.2", + "toidentifier": "~1.0.1" + }, + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, "node_modules/human-signals": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/human-signals/-/human-signals-2.1.0.tgz", @@ -4517,6 +4891,18 @@ "node": ">=10.17.0" } }, + "node_modules/iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/ignore": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.1.tgz", @@ -4584,8 +4970,7 @@ "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "node_modules/internal-slot": { "version": "1.0.7", @@ -4601,6 +4986,15 @@ "node": ">= 0.4" } }, + "node_modules/ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", + "license": "MIT", + "engines": { + "node": ">= 0.10" + } + }, "node_modules/is-array-buffer": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/is-array-buffer/-/is-array-buffer-3.0.4.tgz", @@ -6673,12 +7067,67 @@ "graceful-fs": "^4.1.6" } }, + "node_modules/jsonwebtoken": { + "version": "9.0.3", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.3.tgz", + "integrity": "sha512-MT/xP0CrubFRNLNKvxJ2BYfy53Zkm++5bX9dtuPbqAeQpTVe0MQTFhao8+Cp//EmJp244xt6Drw/GVEGCUj40g==", + "license": "MIT", + "dependencies": { + "jws": "^4.0.1", + "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/semver": { + "version": "7.7.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.4.tgz", + "integrity": "sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==", + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/just-extend": { "version": "4.2.1", "resolved": "https://registry.npmjs.org/just-extend/-/just-extend-4.2.1.tgz", "integrity": "sha512-g3UB796vUFIY90VIv/WX3L2c8CS2MdWUww3CNrYmqza1Fg0DURc2K/O4YrnklBdQarSJ/y8JnJYDGc+1iumQjg==", "dev": true }, + "node_modules/jwa": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-2.0.1.tgz", + "integrity": "sha512-hRF04fqJIP8Abbkq5NKGN0Bbr3JxlQ+qhZufXVr0DvujKy93ZCbXZMHDL4EOtodSbCWxOqR8MS1tXA5hwqCXDg==", + "license": "MIT", + "dependencies": { + "buffer-equal-constant-time": "^1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/jws/-/jws-4.0.1.tgz", + "integrity": "sha512-EKI/M/yqPncGUUh44xz0PxSidXFr/+r0pA70+gIYhjv+et7yxM+s29Y+VGDkovRofQem0fs7Uvf4+YmAdyRduA==", + "license": "MIT", + "dependencies": { + "jwa": "^2.0.1", + "safe-buffer": "^5.0.1" + } + }, "node_modules/keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", @@ -6746,12 +7195,54 @@ "integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==", "dev": true }, + "node_modules/lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==", + "license": "MIT" + }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==", + "license": "MIT" + }, + "node_modules/lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==", + "license": "MIT" + }, + "node_modules/lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==", + "license": "MIT" + }, + "node_modules/lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==", + "license": "MIT" + }, + "node_modules/lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==", + "license": "MIT" + }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==", "dev": true }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==", + "license": "MIT" + }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -6809,6 +7300,33 @@ "tmpl": "1.0.5" } }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/merge-descriptors": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.3.tgz", + "integrity": "sha512-gaNvAS7TZ897/rVaZ0nMtAyxNyi/pdbjbAwUpFQpN70GqnVfOiXpeUUMKRBmzXaSQ8DdTX4/0ms62r2K+hE6mQ==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/merge-stream": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", @@ -6824,6 +7342,15 @@ "node": ">= 8" } }, + "node_modules/methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/micromatch": { "version": "4.0.7", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.7.tgz", @@ -6837,6 +7364,39 @@ "node": ">=8.6" } }, + "node_modules/mime": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", + "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==", + "license": "MIT", + "bin": { + "mime": "cli.js" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/mimic-fn": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", @@ -6870,8 +7430,7 @@ "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==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/natural-compare": { "version": "1.4.0", @@ -6879,6 +7438,15 @@ "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", "dev": true }, + "node_modules/negotiator": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", + "integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/nice-try": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", @@ -6948,6 +7516,15 @@ "integrity": "sha512-d9VeXT4SJ7ZeOqGX6R5EM022wpL+eWPooLI+5UpWn2jCT1aosUQEhQP214x33Wkwx3JQMvIm+tIoVOdodFS40g==", "dev": true }, + "node_modules/nodemailer": { + "version": "6.10.1", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.10.1.tgz", + "integrity": "sha512-Z+iLaBGVaSjbIzQ4pX6XV41HrooLsQ10ZWPUehGmuantvzWoDVBnmsdUcOIDM1t+yPor5pDhVlDESgOMEGxhHA==", + "license": "MIT-0", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", @@ -6970,10 +7547,10 @@ } }, "node_modules/object-inspect": { - "version": "1.13.2", - "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.2.tgz", - "integrity": "sha512-IRZSRuzJiynemAXPYtPe5BoI/RESNYR7TYm50MC5Mqbd3Jmw5y790sErYw3V6SryFJD64b74qQQs9wn5Bg/k3g==", - "dev": true, + "version": "1.13.4", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz", + "integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==", + "license": "MIT", "engines": { "node": ">= 0.4" }, @@ -7057,6 +7634,18 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/on-finished": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz", + "integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==", + "license": "MIT", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -7205,6 +7794,15 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/path-exists": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", @@ -7437,6 +8035,19 @@ "node": ">= 6" } }, + "node_modules/proxy-addr": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", + "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==", + "license": "MIT", + "dependencies": { + "forwarded": "0.2.0", + "ipaddr.js": "1.9.1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/pump": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", @@ -7472,6 +8083,21 @@ } ] }, + "node_modules/qs": { + "version": "6.14.2", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.14.2.tgz", + "integrity": "sha512-V/yCWTTF7VJ9hIh18Ugr2zhJMP01MY7c5kh4J870L7imm6/DIzBsNLTXzMwUA3yZ5b/KBqLx8Kp3uRvd7xSe3Q==", + "license": "BSD-3-Clause", + "dependencies": { + "side-channel": "^1.1.0" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", @@ -7492,6 +8118,30 @@ } ] }, + "node_modules/range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/raw-body": { + "version": "2.5.3", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.3.tgz", + "integrity": "sha512-s4VSOf6yN0rvbRZGxs8Om5CWj6seneMwK3oDb4lWDH0UPhWcxwOWw5+qk24bxq87szX1ydrwylIOp2uG1ojUpA==", + "license": "MIT", + "dependencies": { + "bytes": "~3.1.2", + "http-errors": "~2.0.1", + "iconv-lite": "~0.4.24", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/react-is": { "version": "18.3.1", "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz", @@ -7660,6 +8310,26 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, "node_modules/safe-regex-test": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/safe-regex-test/-/safe-regex-test-1.0.3.tgz", @@ -7677,6 +8347,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "license": "MIT" + }, "node_modules/semver": { "version": "6.3.1", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", @@ -7686,6 +8362,66 @@ "semver": "bin/semver.js" } }, + "node_modules/send": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/send/-/send-0.19.2.tgz", + "integrity": "sha512-VMbMxbDeehAxpOtWJXlcUS5E8iXh6QmN+BkRX1GARS3wRaXEEgzCcB10gTQazO42tpNIya8xIyNx8fll1OFPrg==", + "license": "MIT", + "dependencies": { + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "encodeurl": "~2.0.0", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "~0.5.2", + "http-errors": "~2.0.1", + "mime": "1.6.0", + "ms": "2.1.3", + "on-finished": "~2.4.1", + "range-parser": "~1.2.1", + "statuses": "~2.0.2" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/send/node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "license": "MIT", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/send/node_modules/debug/node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", + "license": "MIT" + }, + "node_modules/send/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/serve-static": { + "version": "1.16.3", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.16.3.tgz", + "integrity": "sha512-x0RTqQel6g5SY7Lg6ZreMmsOzncHFU7nhnRWkKgWuMTu5NN0DR5oruckMqRvacAN9d5w6ARnRBXl9xhDCgfMeA==", + "license": "MIT", + "dependencies": { + "encodeurl": "~2.0.0", + "escape-html": "~1.0.3", + "parseurl": "~1.3.3", + "send": "~0.19.1" + }, + "engines": { + "node": ">= 0.8.0" + } + }, "node_modules/set-function-length": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.2.2.tgz", @@ -7718,6 +8454,12 @@ "node": ">= 0.4" } }, + "node_modules/setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==", + "license": "ISC" + }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", @@ -7740,15 +8482,69 @@ } }, "node_modules/side-channel": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.6.tgz", - "integrity": "sha512-fDW/EZ6Q9RiO8eFG8Hj+7u/oW+XrPTIChwCOM2+th2A6OblDtYYIpve9m+KvI9Z4C9qSEXlaGR6bTEYHReuglA==", - "dev": true, + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.0.tgz", + "integrity": "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==", + "license": "MIT", "dependencies": { - "call-bind": "^1.0.7", "es-errors": "^1.3.0", - "get-intrinsic": "^1.2.4", - "object-inspect": "^1.13.1" + "object-inspect": "^1.13.3", + "side-channel-list": "^1.0.0", + "side-channel-map": "^1.0.1", + "side-channel-weakmap": "^1.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-list": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.0.tgz", + "integrity": "sha512-FCLHtRD/gnpCiCHEiJLOwdmFP+wzCmDEkc9y7NsYxeF4u7Btsn1ZuwgwJGxImImHicJArLP4R0yX4c2KCrMrTA==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-map": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz", + "integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-weakmap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz", + "integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3", + "side-channel-map": "^1.0.1" }, "engines": { "node": ">= 0.4" @@ -7882,6 +8678,15 @@ "node": ">=8" } }, + "node_modules/statuses": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz", + "integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/string-length": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/string-length/-/string-length-4.0.2.tgz", @@ -8096,6 +8901,15 @@ "node": ">=8.0" } }, + "node_modules/toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==", + "license": "MIT", + "engines": { + "node": ">=0.6" + } + }, "node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", @@ -8195,6 +9009,19 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "license": "MIT", + "dependencies": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/typed-array-buffer": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/typed-array-buffer/-/typed-array-buffer-1.0.2.tgz", @@ -8319,6 +9146,15 @@ "node": ">= 10.0.0" } }, + "node_modules/unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/update-browserslist-db": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.1.0.tgz", @@ -8358,6 +9194,28 @@ "punycode": "^2.1.0" } }, + "node_modules/utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==", + "license": "MIT", + "engines": { + "node": ">= 0.4.0" + } + }, + "node_modules/uuid": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz", + "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "license": "MIT", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/v8-to-istanbul": { "version": "9.3.0", "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.3.0.tgz", @@ -8372,6 +9230,15 @@ "node": ">=10.12.0" } }, + "node_modules/vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/walker": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/walker/-/walker-1.0.8.tgz", diff --git a/package.json b/package.json index 5e195a15..0ea9f4bb 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,16 @@ }, "author": "Mate academy", "license": "GPL-3.0", + "dependencies": { + "bcryptjs": "^2.4.3", + "express": "^4.18.2", + "jsonwebtoken": "^9.0.2", + "nodemailer": "^6.9.9", + "uuid": "^9.0.1" + }, "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^2.1.3", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", diff --git a/src/app.js b/src/app.js new file mode 100644 index 00000000..d226f6a8 --- /dev/null +++ b/src/app.js @@ -0,0 +1,32 @@ +'use strict'; + +const express = require('express'); +const authRouter = require('./routes/auth.router'); +const userRouter = require('./routes/user.router'); +const errorHandler = require('./middlewares/errorHandler.middleware'); + +function createApp() { + const app = express(); + + // ── Body parsing ─────────────────────────────────────────────────────────── + app.use(express.json()); + + // ── Health check ─────────────────────────────────────────────────────────── + app.get('/api/health', (req, res) => res.json({ status: 'ok' })); + + // ── Feature routers ──────────────────────────────────────────────────────── + app.use('/api/auth', authRouter); + app.use('/api/user', userRouter); + + // ── 404 catch-all ────────────────────────────────────────────────────────── + app.use((req, res) => { + res.status(404).json({ error: 'Not found' }); + }); + + // ── Central error handler ────────────────────────────────────────────────── + app.use(errorHandler); + + return app; +} + +module.exports = createApp; diff --git a/src/config/env.js b/src/config/env.js new file mode 100644 index 00000000..cbf1475f --- /dev/null +++ b/src/config/env.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = { + PORT: process.env.PORT || 3000, + JWT_SECRET: process.env.JWT_SECRET || 'dev-secret-change-in-production', + JWT_EXPIRES_IN: process.env.JWT_EXPIRES_IN || '7d', + FRONTEND_URL: process.env.FRONTEND_URL || 'http://localhost:5173', + NODE_ENV: process.env.NODE_ENV || 'development', +}; diff --git a/src/controllers/auth.controller.js b/src/controllers/auth.controller.js new file mode 100644 index 00000000..e0104b74 --- /dev/null +++ b/src/controllers/auth.controller.js @@ -0,0 +1,113 @@ +'use strict'; + +const authService = require('../services/auth.service'); + +/** + * POST /api/auth/register + * Body: { name, email, password } + */ +async function register(req, res, next) { + try { + const result = await authService.register(req.body); + + res.status(201).json(result); + } catch (err) { + next(err); + } +} + +/** + * POST /api/auth/activate/:token + * Activates the account and returns a JWT so the user is logged in immediately. + */ +function activate(req, res, next) { + try { + const result = authService.activate(req.params.token); + + res.json(result); + } catch (err) { + next(err); + } +} + +/** + * POST /api/auth/login + * Body: { email, password } + */ +async function login(req, res, next) { + try { + const result = await authService.login(req.body); + + res.json(result); + } catch (err) { + next(err); + } +} + +/** + * POST /api/auth/forgot-password + * Body: { email } + * Always responds 200 to prevent email enumeration. + */ +async function forgotPassword(req, res, next) { + try { + const result = await authService.forgotPassword(req.body); + + res.json(result); + } catch (err) { + next(err); + } +} + +/** + * GET /api/auth/validate-reset/:token + * Lets the frontend verify a reset token + * is still valid before rendering the form. + */ +function validateResetToken(req, res, next) { + try { + const result = authService.validateResetToken(req.params.token); + + res.json(result); + } catch (err) { + next(err); + } +} + +/** + * POST /api/auth/reset-password/:token + * Body: { password, confirmation } + */ +async function resetPassword(req, res, next) { + try { + const result = await authService.resetPassword(req.params.token, req.body); + + res.json(result); + } catch (err) { + next(err); + } +} + +/** + * POST /api/auth/logout + * Authenticated — instructs the client to discard its token. + */ +function logout(req, res, next) { + try { + const result = authService.logout(); + + res.json(result); + } catch (err) { + next(err); + } +} + +module.exports = { + register, + activate, + login, + forgotPassword, + validateResetToken, + resetPassword, + logout, +}; diff --git a/src/controllers/user.controller.js b/src/controllers/user.controller.js new file mode 100644 index 00000000..eaca26af --- /dev/null +++ b/src/controllers/user.controller.js @@ -0,0 +1,83 @@ +'use strict'; + +const userService = require('../services/user.service'); + +/** + * GET /api/user/profile + * Returns the authenticated user's public profile. + */ +function getProfile(req, res, next) { + try { + const result = userService.getProfile(req.userId); + + res.json(result); + } catch (err) { + next(err); + } +} + +/** + * PUT /api/user/name + * Body: { name } + */ +function updateName(req, res, next) { + try { + const result = userService.updateName(req.userId, req.body); + + res.json(result); + } catch (err) { + next(err); + } +} + +/** + * PUT /api/user/password + * Body: { currentPassword, newPassword, confirmation } + */ +async function changePassword(req, res, next) { + try { + const result = await userService.changePassword(req.userId, req.body); + + res.json(result); + } catch (err) { + next(err); + } +} + +/** + * POST /api/user/request-email-change + * Body: { password, newEmail } + * Sends a confirmation link to the new address + * and a notification to the old one. + */ +async function requestEmailChange(req, res, next) { + try { + const result = await userService.requestEmailChange(req.userId, req.body); + + res.json(result); + } catch (err) { + next(err); + } +} + +/** + * POST /api/user/confirm-email-change/:token + * Called when the user clicks the link sent to their new inbox. + */ +function confirmEmailChange(req, res, next) { + try { + const result = userService.confirmEmailChange(req.userId, req.params.token); + + res.json(result); + } catch (err) { + next(err); + } +} + +module.exports = { + getProfile, + updateName, + changePassword, + requestEmailChange, + confirmEmailChange, +}; diff --git a/src/index.js b/src/index.js index ad9a93a7..9c6510e0 100644 --- a/src/index.js +++ b/src/index.js @@ -1 +1,11 @@ 'use strict'; + +const createApp = require('./app'); +const { PORT } = require('./config/env'); + +const app = createApp(); + +app.listen(PORT, () => { + // eslint-disable-next-line no-console + console.log(`\n🔐 Auth API listening on http://localhost:${PORT}\n`); +}); diff --git a/src/middlewares/authenticate.middleware.js b/src/middlewares/authenticate.middleware.js new file mode 100644 index 00000000..4a883b7a --- /dev/null +++ b/src/middlewares/authenticate.middleware.js @@ -0,0 +1,32 @@ +'use strict'; + +const jwt = require('jsonwebtoken'); +const { JWT_SECRET } = require('../config/env'); + +/** + * Verify the JWT sent in the `Authorization: Bearer ` header. + * On success, attaches `req.userId` (string) and calls `next()`. + * On failure, responds immediately with 401. + * + * @type {import('express').RequestHandler} + */ +function authenticate(req, res, next) { + const authHeader = req.headers.authorization; + + if (!authHeader || !authHeader.startsWith('Bearer ')) { + return res.status(401).json({ error: 'Authentication required' }); + } + + const token = authHeader.slice(7); + + try { + const payload = jwt.verify(token, JWT_SECRET); + + req.userId = payload.userId; + next(); + } catch { + return res.status(401).json({ error: 'Invalid or expired token' }); + } +} + +module.exports = authenticate; diff --git a/src/middlewares/errorHandler.middleware.js b/src/middlewares/errorHandler.middleware.js new file mode 100644 index 00000000..424ccdec --- /dev/null +++ b/src/middlewares/errorHandler.middleware.js @@ -0,0 +1,38 @@ +'use strict'; + +/** + * Central Express error handler. + * + * Services throw plain Error objects with an optional `.status` number and + * an optional `.details` array (e.g. password-rule violations). This handler + * normalises all of that into a consistent JSON response shape: + * + * { error: string, details?: string[] } + * + * Unexpected errors (status 500) are logged to stderr in full so they're + * visible in the server console without leaking internals to the client. + * + * @type {import('express').ErrorRequestHandler} + */ +// eslint-disable-next-line no-unused-vars +function errorHandler(err, req, res, next) { + // Guard against non-Error values passed to next() (e.g. a raw string) + const isError = err instanceof Error; + const status = (isError && err.status) || 500; + const message = (isError && err.message) || 'Internal server error'; + + const body = { error: message }; + + if (isError && Array.isArray(err.details)) { + body.details = err.details; + } + + if (status === 500) { + // eslint-disable-next-line no-console + console.error(err); + } + + res.status(status).json(body); +} + +module.exports = errorHandler; diff --git a/src/models/Token.model.js b/src/models/Token.model.js new file mode 100644 index 00000000..434ed726 --- /dev/null +++ b/src/models/Token.model.js @@ -0,0 +1,152 @@ +'use strict'; + +const { randomBytes } = require('crypto'); + +/** + * In-memory token store. + * + * Shape of a stored record: + * { + * userId: string + * type: string (one of TOKEN_TYPES) + * data: any|null (optional payload, e.g. { newEmail }) + * expiresAt: number (unix ms timestamp) + * } + * + * Map key is the raw hex token string. + */ +const store = new Map(); + +const Token = { + // ── Token type constants ─────────────────────────────────────────────────── + + TOKEN_TYPES: { + ACTIVATION: 'activation', + PASSWORD_RESET: 'password_reset', + EMAIL_CHANGE: 'email_change', + }, + + // ── Write ────────────────────────────────────────────────────────────────── + + /** + * Create a token for `userId` of the given `type`. + * Any existing token of the same type for the same user is invalidated first, + * so there is at most one live token per (userId, type) pair at any time. + * + * @param {string} userId + * @param {string} type - one of TOKEN_TYPES values + * @param {number} ttlHours - how long until the token expires + * @param {any} [data] - optional extra payload stored alongside the token + * @returns {string} the generated token (64-char hex string) + */ + create(userId, type, ttlHours, data = null) { + // Revoke any previous token of the same type for this user + for (const [tok, rec] of store.entries()) { + if (rec.userId === userId && rec.type === type) { + store.delete(tok); + } + } + + const token = randomBytes(32).toString('hex'); + const expiresAt = Date.now() + ttlHours * 60 * 60 * 1000; + + store.set(token, { + userId, + type, + data, + expiresAt, + }); + + return token; + }, + + // ── Read ─────────────────────────────────────────────────────────────────── + + /** + * Look up a token without removing it. + * Returns `null` if the token doesn't exist, is + * the wrong type, or has expired. + * + * @param {string} token + * @param {string} type + * @returns {{ userId: string, data: any }|null} + */ + find(token, type) { + const record = store.get(token); + + if (!record) { + return null; + } + + if (record.type !== type || Date.now() > record.expiresAt) { + return null; + } + + return { userId: record.userId, data: record.data }; + }, + + /** + * Check whether a token exists, is the correct type, and has not expired. + * Does NOT consume the token. + * + * @param {string} token + * @param {string} type + * @returns {boolean} + */ + isValid(token, type) { + return this.find(token, type) !== null; + }, + + // ── Consume (read + delete) ──────────────────────────────────────────────── + + /** + * Validate a token and, if valid, delete it so it cannot be reused. + * Returns `null` if the token is missing, the wrong type, or expired. + * + * @param {string} token + * @param {string} type + * @returns {{ userId: string, data: any }|null} + */ + consume(token, type) { + const result = this.find(token, type); + + if (!result) { + // Also purge an expired/mismatched entry if it exists + store.delete(token); + + return null; + } + + store.delete(token); + + return result; + }, + + // ── Delete ───────────────────────────────────────────────────────────────── + + /** + * Remove all tokens belonging to a given user. + * Useful when a user is deleted or all their sessions should be revoked. + * + * @param {string} userId + */ + deleteByUserId(userId) { + for (const [tok, rec] of store.entries()) { + if (rec.userId === userId) { + store.delete(tok); + } + } + }, + + // ── Test helpers ─────────────────────────────────────────────────────────── + + /** Expose the underlying Map for assertions in tests. */ + _store: store, + + /** Reset the store between tests. */ + _clear() { + store.clear(); + }, +}; + +module.exports = Token; diff --git a/src/models/User.model.js b/src/models/User.model.js new file mode 100644 index 00000000..2472e073 --- /dev/null +++ b/src/models/User.model.js @@ -0,0 +1,117 @@ +'use strict'; + +const { v4: uuidv4 } = require('uuid'); + +/** + * In-memory user store. + * + * Shape of a stored user: + * { + * id: string (uuid v4) + * name: string + * email: string (always lowercase) + * password: string (bcrypt hash) + * isActive: boolean + * createdAt: string (ISO-8601) + * } + */ +const store = new Map(); + +const User = { + /** + * Persist a new user and return the full record. + * @param {{ name: string, email: string, password: string }} fields + * @returns {object} + */ + create({ name, email, password }) { + const user = { + id: uuidv4(), + name, + email: email.toLowerCase(), + password, + isActive: false, + createdAt: new Date().toISOString(), + }; + + store.set(user.id, user); + + return user; + }, + + /** + * @param {string} id + * @returns {object|null} + */ + findById(id) { + return store.get(id) || null; + }, + + /** + * Case-insensitive email lookup. + * @param {string} email + * @returns {object|null} + */ + findByEmail(email) { + const normalised = email.toLowerCase(); + + for (const user of store.values()) { + if (user.email === normalised) { + return user; + } + } + + return null; + }, + + /** + * Shallow-merge `fields` onto the stored user. + * @param {string} id + * @param {object} fields + * @returns {object|null} updated user, or null if not found + */ + update(id, fields) { + const user = store.get(id); + + if (!user) { + return null; + } + + const updated = { ...user, ...fields }; + + store.set(id, updated); + + return updated; + }, + + /** + * Remove a user from the store. + * @param {string} id + * @returns {boolean} true if the user existed and was removed + */ + delete(id) { + return store.delete(id); + }, + + /** + * Return a copy of the user without the `password` field. + * @param {object} user + * @returns {object} + */ + toPublic(user) { + const { password, ...rest } = user; + + return rest; + }, + + // ── Test helpers ─────────────────────────────────────────────────────────── + + /** Expose the underlying Map for assertions in tests. */ + _store: store, + + /** Reset the store between tests. */ + _clear() { + store.clear(); + }, +}; + +module.exports = User; diff --git a/src/routes/auth.router.js b/src/routes/auth.router.js new file mode 100644 index 00000000..0729ad6d --- /dev/null +++ b/src/routes/auth.router.js @@ -0,0 +1,30 @@ +'use strict'; + +const { Router } = require('express'); +const ctrl = require('../controllers/auth.controller'); +const authenticate = require('../middlewares/authenticate.middleware'); + +const router = Router(); + +// POST /api/auth/register +router.post('/register', ctrl.register); + +// POST /api/auth/activate/:token +router.post('/activate/:token', ctrl.activate); + +// POST /api/auth/login +router.post('/login', ctrl.login); + +// POST /api/auth/logout (authenticated) +router.post('/logout', authenticate, ctrl.logout); + +// POST /api/auth/forgot-password +router.post('/forgot-password', ctrl.forgotPassword); + +// GET /api/auth/validate-reset/:token (check before showing the reset form) +router.get('/validate-reset/:token', ctrl.validateResetToken); + +// POST /api/auth/reset-password/:token +router.post('/reset-password/:token', ctrl.resetPassword); + +module.exports = router; diff --git a/src/routes/user.router.js b/src/routes/user.router.js new file mode 100644 index 00000000..7fa27ad5 --- /dev/null +++ b/src/routes/user.router.js @@ -0,0 +1,27 @@ +'use strict'; + +const { Router } = require('express'); +const ctrl = require('../controllers/user.controller'); +const authenticate = require('../middlewares/authenticate.middleware'); + +const router = Router(); + +// All /api/user routes require a valid JWT +router.use(authenticate); + +// GET /api/user/profile +router.get('/profile', ctrl.getProfile); + +// PUT /api/user/name +router.put('/name', ctrl.updateName); + +// PUT /api/user/password +router.put('/password', ctrl.changePassword); + +// POST /api/user/request-email-change +router.post('/request-email-change', ctrl.requestEmailChange); + +// POST /api/user/confirm-email-change/:token +router.post('/confirm-email-change/:token', ctrl.confirmEmailChange); + +module.exports = router; diff --git a/src/services/auth.service.js b/src/services/auth.service.js new file mode 100644 index 00000000..360b13b2 --- /dev/null +++ b/src/services/auth.service.js @@ -0,0 +1,255 @@ +'use strict'; + +const bcrypt = require('bcryptjs'); +const jwt = require('jsonwebtoken'); + +const User = require('../models/User.model'); +const { validatePassword } = require('../utils/password.utils'); +const tokenService = require('./token.service'); +const emailService = require('./email.service'); +const { JWT_SECRET, JWT_EXPIRES_IN, NODE_ENV } = require('../config/env'); + +const SALT_ROUNDS = 12; + +function signJwt(userId) { + return jwt.sign({ userId }, JWT_SECRET, { expiresIn: JWT_EXPIRES_IN }); +} + +// ── Register ───────────────────────────────────────────────────────────────── + +async function register({ name, email, password }) { + if (!name || !name.trim()) { + throw Object.assign(new Error('Name is required'), { status: 400 }); + } + + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + + if (!email || !emailRegex.test(email)) { + throw Object.assign(new Error('Valid email is required'), { status: 400 }); + } + + const { valid, errors } = validatePassword(password || ''); + + if (!valid) { + throw Object.assign( + // eslint-disable-next-line + new Error('Password does not meet requirements'), + { status: 400, details: errors }, + ); + } + + if (User.findByEmail(email)) { + throw Object.assign( + new Error('An account with this email already exists'), + { status: 409 }, + ); + } + + const hash = await bcrypt.hash(password, SALT_ROUNDS); + const user = User.create({ name: name.trim(), email, password: hash }); + const token = tokenService.createActivationToken(user.id); + + // eslint-disable-next-line + const result = { + message: 'Account created. Please check your email to activate it.', + }; + + try { + const previewUrl = await emailService.sendActivationEmail( + user.email, + user.name, + token, + ); + + if (NODE_ENV !== 'production') { + result.emailPreview = previewUrl; + } + } catch { + // Don't block registration if email fails + } + + return result; +} + +// ── Activate ───────────────────────────────────────────────────────────────── + +function activate(token) { + const record = tokenService.consumeActivationToken(token); + + if (!record) { + throw Object.assign( + new Error('Activation link is invalid or has expired'), + { status: 400 }, + ); + } + + const user = User.findById(record.userId); + + if (!user) { + throw Object.assign(new Error('User not found'), { status: 404 }); + } + + if (user.isActive) { + // eslint-disable-next-line + throw Object.assign(new Error('Account is already active'), { + status: 400, + }); + } + + const updated = User.update(user.id, { isActive: true }); + + return { + token: signJwt(updated.id), + user: User.toPublic(updated), + }; +} + +// ── Login ──────────────────────────────────────────────────────────────────── + +async function login({ email, password }) { + if (!email || !password) { + throw Object.assign( + // eslint-disable-next-line + new Error('Email and password are required'), + { status: 400 }, + ); + } + + const user = User.findByEmail(email); + + if (!user) { + // eslint-disable-next-line + throw Object.assign(new Error('Invalid email or password'), { + status: 401, + }); + } + + const match = await bcrypt.compare(password, user.password); + + if (!match) { + // eslint-disable-next-line + throw Object.assign(new Error('Invalid email or password'), { + status: 401, + }); + } + + if (!user.isActive) { + throw Object.assign( + new Error( + // eslint-disable-next-line + 'Account not activated. Please check your email and click the activation link.', + ), + { status: 403 }, + ); + } + + return { + token: signJwt(user.id), + user: User.toPublic(user), + }; +} + +// ── Forgot password ────────────────────────────────────────────────────────── + +async function forgotPassword({ email }) { + if (!email) { + throw Object.assign(new Error('Email is required'), { status: 400 }); + } + + // Always resolve with success to prevent email enumeration + const user = User.findByEmail(email); + + if (user) { + const token = tokenService.createPasswordResetToken(user.id); + + try { + await emailService.sendPasswordResetEmail(user.email, token); + } catch { + // silent + } + } + + return { + message: 'If an account exists for that email, a reset link has been sent.', + }; +} + +// ── Validate reset token ───────────────────────────────────────────────────── + +function validateResetToken(token) { + const valid = tokenService.isPasswordResetTokenValid(token); + + if (!valid) { + throw Object.assign( + // eslint-disable-next-line + new Error('Reset link is invalid or has expired'), + { status: 400 }, + ); + } + + return { valid: true }; +} + +// ── Reset password ─────────────────────────────────────────────────────────── + +async function resetPassword(token, { password, confirmation }) { + if (!password || !confirmation) { + throw Object.assign( + // eslint-disable-next-line + new Error('Password and confirmation are required'), + { status: 400 }, + ); + } + + if (password !== confirmation) { + throw Object.assign(new Error('Passwords do not match'), { status: 400 }); + } + + const { valid, errors } = validatePassword(password); + + if (!valid) { + throw Object.assign( + // eslint-disable-next-line + new Error('Password does not meet requirements'), + { status: 400, details: errors }, + ); + } + + const record = tokenService.consumePasswordResetToken(token); + + if (!record) { + throw Object.assign( + // eslint-disable-next-line + new Error('Reset link is invalid or has expired'), + { status: 400 }, + ); + } + + const hash = await bcrypt.hash(password, SALT_ROUNDS); + + User.update(record.userId, { password: hash }); + + return { message: 'Password reset successfully. You can now sign in.' }; +} + +// ── Logout ─────────────────────────────────────────────────────────────────── + +/** + * JWTs are stateless — the token lives on the client and expiry is enforced + * by the `expiresIn` claim. Logout simply instructs the client to discard it. + * If token blocklisting is needed in future, it can be added here without + * changing the controller or router. + */ +function logout() { + return { message: 'Logged out successfully' }; +} + +module.exports = { + register, + activate, + login, + forgotPassword, + validateResetToken, + resetPassword, + logout, +}; diff --git a/src/services/email.service.js b/src/services/email.service.js new file mode 100644 index 00000000..777c7146 --- /dev/null +++ b/src/services/email.service.js @@ -0,0 +1,141 @@ +'use strict'; + +const nodemailer = require('nodemailer'); +const { FRONTEND_URL } = require('../config/env'); + +let transporter = null; + +async function getTransporter() { + if (transporter) { + return transporter; + } + + const testAccount = await nodemailer.createTestAccount(); + + /* eslint-disable no-console */ + console.log('\n╔══════════════════════════════════════════╗'); + console.log('║ Ethereal Test Email Account ║'); + console.log('╠══════════════════════════════════════════╣'); + console.log(`║ User: ${testAccount.user.padEnd(34)}║`); + console.log(`║ Pass: ${testAccount.pass.padEnd(34)}║`); + console.log('║ View: https://ethereal.email/messages ║'); + console.log('╚══════════════════════════════════════════╝\n'); + /* eslint-enable no-console */ + + transporter = nodemailer.createTransport({ + host: 'smtp.ethereal.email', + port: 587, + secure: false, + auth: { user: testAccount.user, pass: testAccount.pass }, + }); + + return transporter; +} + +async function sendMail({ to, subject, html }) { + const t = await getTransporter(); + const info = await t.sendMail({ + from: '"Auth App" ', + to, + subject, + html, + }); + + const previewUrl = nodemailer.getTestMessageUrl(info); + + /* eslint-disable-next-line no-console */ + console.log(`📧 ${subject} → ${to} | ${previewUrl}`); + + return previewUrl; +} + +// ── Templates ──────────────────────────────────────────────────────────────── + +function wrap(content) { + return ` +
${content}
`; +} + +async function sendActivationEmail(email, name, token) { + const url = `${FRONTEND_URL}/activate/${token}`; + + return sendMail({ + to: email, + subject: 'Activate your account', + html: wrap(` +

Welcome, ${name}.

+

Please confirm your email address to activate your account.

+ ACTIVATE ACCOUNT +

Or paste this link into your browser:
${url}

+

This link expires in 24 hours.

+
If you didn't create this account, you can safely ignore this email.
+ `), + }); +} + +async function sendPasswordResetEmail(email, token) { + const url = `${FRONTEND_URL}/reset-password/${token}`; + + return sendMail({ + to: email, + subject: 'Reset your password', + html: wrap(` +

Password Reset

+

We received a request to reset the password for your account.

+ RESET PASSWORD +

Or paste this link into your browser:
${url}

+

This link expires in 1 hour. If you didn't request this, no action is needed.

+
For security, this link can only be used once.
+ `), + }); +} + +async function sendEmailChangeNotification(oldEmail, newEmail) { + return sendMail({ + to: oldEmail, + subject: 'Your email address was changed', + html: wrap(` +

Email Changed

+

The email address on your account was changed to:

+

${newEmail}

+

If you made this change, no action is needed.

+

If you did not make this change, please contact support immediately.

+
This is an automated security notification.
+ `), + }); +} + +async function sendEmailChangeConfirmation(newEmail, token) { + const url = `${FRONTEND_URL}/confirm-email/${token}`; + + return sendMail({ + to: newEmail, + subject: 'Confirm your new email address', + html: wrap(` +

Confirm Email Change

+

Please confirm that you want to use this address for your account.

+ CONFIRM NEW EMAIL +

Or paste this link into your browser:
${url}

+

This link expires in 24 hours.

+
If you didn't request this change, please contact support.
+ `), + }); +} + +module.exports = { + sendActivationEmail, + sendPasswordResetEmail, + sendEmailChangeNotification, + sendEmailChangeConfirmation, +}; diff --git a/src/services/token.service.js b/src/services/token.service.js new file mode 100644 index 00000000..2b9b1c0a --- /dev/null +++ b/src/services/token.service.js @@ -0,0 +1,84 @@ +'use strict'; + +const Token = require('../models/Token.model'); + +/** + * Time-to-live values for each token type (in hours). + * Centralised here so they're easy to adjust without touching the model. + */ +const TTL = { + ACTIVATION: 24, // 24 h — plenty of time to check email + PASSWORD_RESET: 1, // 1 h — short window for security + EMAIL_CHANGE: 24, // 24 h — new inbox may have a delay +}; + +// ── Create ─────────────────────────────────────────────────────────────────── + +function createActivationToken(userId) { + return Token.create(userId, Token.TOKEN_TYPES.ACTIVATION, TTL.ACTIVATION); +} + +function createPasswordResetToken(userId) { + return Token.create( + userId, + Token.TOKEN_TYPES.PASSWORD_RESET, + TTL.PASSWORD_RESET, + ); +} + +/** + * @param {string} userId + * @param {string} newEmail - stored in the token payload, + * applied on confirmation + */ +function createEmailChangeToken(userId, newEmail) { + return Token.create( + userId, + Token.TOKEN_TYPES.EMAIL_CHANGE, + TTL.EMAIL_CHANGE, + { newEmail }, + ); +} + +// ── Find (peek — does NOT consume) ─────────────────────────────────────────── + +/** + * Check whether a password-reset token is still valid without consuming it. + * Used by the frontend to validate the token before showing the reset form. + * + * @param {string} token + * @returns {boolean} + */ +function isPasswordResetTokenValid(token) { + return Token.isValid(token, Token.TOKEN_TYPES.PASSWORD_RESET); +} + +// ── Consume (validates + deletes in one step) ──────────────────────────────── + +function consumeActivationToken(token) { + return Token.consume(token, Token.TOKEN_TYPES.ACTIVATION); +} + +function consumePasswordResetToken(token) { + return Token.consume(token, Token.TOKEN_TYPES.PASSWORD_RESET); +} + +/** + * @returns {{ userId: string, data: { newEmail: string } }|null} + */ +function consumeEmailChangeToken(token) { + return Token.consume(token, Token.TOKEN_TYPES.EMAIL_CHANGE); +} + +module.exports = { + // create + createActivationToken, + createPasswordResetToken, + createEmailChangeToken, + // peek + isPasswordResetTokenValid, + // consume + consumeActivationToken, + consumePasswordResetToken, + consumeEmailChangeToken, +}; diff --git a/src/services/user.service.js b/src/services/user.service.js new file mode 100644 index 00000000..ccad55a1 --- /dev/null +++ b/src/services/user.service.js @@ -0,0 +1,209 @@ +'use strict'; + +const bcrypt = require('bcryptjs'); + +const User = require('../models/User.model'); +const { validatePassword } = require('../utils/password.utils'); +const tokenService = require('./token.service'); +const emailService = require('./email.service'); +const { NODE_ENV } = require('../config/env'); + +const SALT_ROUNDS = 12; + +// ── Get profile ────────────────────────────────────────────────────────────── + +function getProfile(userId) { + const user = User.findById(userId); + + if (!user) { + throw Object.assign(new Error('User not found'), { status: 404 }); + } + + return { user: User.toPublic(user) }; +} + +// ── Update name ────────────────────────────────────────────────────────────── + +function updateName(userId, { name }) { + if (!name || !name.trim()) { + throw Object.assign(new Error('Name is required'), { status: 400 }); + } + + if (name.trim().length < 2) { + // eslint-disable-next-line + throw Object.assign(new Error('Name must be at least 2 characters'), { + status: 400, + }); + } + + const updated = User.update(userId, { name: name.trim() }); + + return { message: 'Name updated successfully', user: User.toPublic(updated) }; +} + +// ── Change password ────────────────────────────────────────────────────────── + +async function changePassword( + userId, + { currentPassword, newPassword, confirmation }, +) { + if (!currentPassword || !newPassword || !confirmation) { + throw Object.assign(new Error('All fields are required'), { status: 400 }); + } + + const user = User.findById(userId); + + if (!user) { + throw Object.assign(new Error('User not found'), { status: 404 }); + } + + const match = await bcrypt.compare(currentPassword, user.password); + + if (!match) { + // eslint-disable-next-line + throw Object.assign(new Error('Current password is incorrect'), { + status: 401, + }); + } + + if (newPassword !== confirmation) { + // eslint-disable-next-line + throw Object.assign(new Error('New passwords do not match'), { + status: 400, + }); + } + + if (newPassword === currentPassword) { + throw Object.assign( + new Error('New password must differ from the current one'), + { status: 400 }, + ); + } + + const { valid, errors } = validatePassword(newPassword); + + if (!valid) { + throw Object.assign( + // eslint-disable-next-line + new Error('Password does not meet requirements'), + { status: 400, details: errors }, + ); + } + + const hash = await bcrypt.hash(newPassword, SALT_ROUNDS); + + User.update(userId, { password: hash }); + + return { message: 'Password updated successfully' }; +} + +// ── Request email change ───────────────────────────────────────────────────── + +async function requestEmailChange(userId, { password, newEmail }) { + if (!password || !newEmail) { + throw Object.assign( + // eslint-disable-next-line + new Error('Password and new email are required'), + { status: 400 }, + ); + } + + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + + if (!emailRegex.test(newEmail)) { + throw Object.assign(new Error('Invalid email address'), { status: 400 }); + } + + const user = User.findById(userId); + + if (!user) { + throw Object.assign(new Error('User not found'), { status: 404 }); + } + + const match = await bcrypt.compare(password, user.password); + + if (!match) { + throw Object.assign(new Error('Password is incorrect'), { status: 401 }); + } + + if (newEmail.toLowerCase() === user.email) { + throw Object.assign( + new Error('New email must differ from the current one'), + { status: 400 }, + ); + } + + if (User.findByEmail(newEmail)) { + throw Object.assign( + // eslint-disable-next-line + new Error('That email address is already in use'), + { status: 409 }, + ); + } + + // eslint-disable-next-line + const token = tokenService.createEmailChangeToken( + userId, + newEmail.toLowerCase(), + ); + + const result = { + message: 'A confirmation link has been sent to your new email address.', + }; + + try { + // eslint-disable-next-line + await emailService.sendEmailChangeNotification( + user.email, + newEmail.toLowerCase(), + ); + + const previewUrl = await emailService.sendEmailChangeConfirmation( + newEmail.toLowerCase(), + token, + ); + + if (NODE_ENV !== 'production') { + result.emailPreview = previewUrl; + } + } catch { + // Don't block on email failure — token is already created + } + + return result; +} + +// ── Confirm email change ───────────────────────────────────────────────────── + +function confirmEmailChange(userId, token) { + const record = tokenService.consumeEmailChangeToken(token); + + if (!record) { + throw Object.assign( + new Error('Confirmation link is invalid or has expired'), + { status: 400 }, + ); + } + + if (record.userId !== userId) { + throw Object.assign( + new Error('This link does not belong to your account'), + { status: 403 }, + ); + } + + const updated = User.update(userId, { email: record.data.newEmail }); + + return { + message: 'Email address updated successfully', + user: User.toPublic(updated), + }; +} + +module.exports = { + getProfile, + updateName, + changePassword, + requestEmailChange, + confirmEmailChange, +}; diff --git a/src/utils/password.utils.js b/src/utils/password.utils.js new file mode 100644 index 00000000..4801313f --- /dev/null +++ b/src/utils/password.utils.js @@ -0,0 +1,44 @@ +'use strict'; + +const PASSWORD_RULES = [ + { + id: 'length', + label: 'At least 8 characters', + test: (p) => p.length >= 8, + }, + { + id: 'uppercase', + label: 'At least one uppercase letter (A–Z)', + test: (p) => /[A-Z]/.test(p), + }, + { + id: 'lowercase', + label: 'At least one lowercase letter (a–z)', + test: (p) => /[a-z]/.test(p), + }, + { + id: 'number', + label: 'At least one number (0–9)', + test: (p) => /\d/.test(p), + }, + { + id: 'special', + label: 'At least one special character (!@#$…)', + test: (p) => /[!@#$%^&*()\-_=+[\]{};':"\\|,.<>/?]/.test(p), + }, +]; + +/** + * @param {string} password + * @returns {{ valid: boolean, errors: string[] }} + */ +function validatePassword(password) { + const failed = PASSWORD_RULES.filter((rule) => !rule.test(password)); + + return { + valid: failed.length === 0, + errors: failed.map((rule) => rule.label), + }; +} + +module.exports = { validatePassword, PASSWORD_RULES };