diff --git a/client/package-lock.json b/client/package-lock.json index 7b97d30..bfeec03 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -9,14 +9,16 @@ "version": "0.1.0", "dependencies": { "axios": "^1.7.7", + "jsonwebtoken": "^9.0.2", "mongodb": "^6.8.0", "mongoose": "^8.6.1", - "next": "14.2.5", + "next": "^14.2.14", "react": "^18", "react-dom": "^18", "zod": "^3.23.8" }, "devDependencies": { + "@types/jsonwebtoken": "^9.0.7", "@types/node": "^20", "@types/react": "^18", "@types/react-dom": "^18", @@ -241,9 +243,9 @@ } }, "node_modules/@next/env": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/env/-/env-14.2.5.tgz", - "integrity": "sha512-/zZGkrTOsraVfYjGP8uM0p6r0BDT6xWpkjdVbcz66PJVSpwXX3yNiRycxAuDfBKGWBrZBXRuK/YVlkNgxHGwmA==" + "version": "14.2.14", + "resolved": "https://registry.npmjs.org/@next/env/-/env-14.2.14.tgz", + "integrity": "sha512-/0hWQfiaD5//LvGNgc8PjvyqV50vGK0cADYzaoOOGN8fxzBn3iAiaq3S0tCRnFBldq0LVveLcxCTi41ZoYgAgg==" }, "node_modules/@next/eslint-plugin-next": { "version": "14.2.5", @@ -255,9 +257,9 @@ } }, "node_modules/@next/swc-darwin-arm64": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.2.5.tgz", - "integrity": "sha512-/9zVxJ+K9lrzSGli1///ujyRfon/ZneeZ+v4ptpiPoOU+GKZnm8Wj8ELWU1Pm7GHltYRBklmXMTUqM/DqQ99FQ==", + "version": "14.2.14", + "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.2.14.tgz", + "integrity": "sha512-bsxbSAUodM1cjYeA4o6y7sp9wslvwjSkWw57t8DtC8Zig8aG8V6r+Yc05/9mDzLKcybb6EN85k1rJDnMKBd9Gw==", "cpu": [ "arm64" ], @@ -270,9 +272,9 @@ } }, "node_modules/@next/swc-darwin-x64": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-14.2.5.tgz", - "integrity": "sha512-vXHOPCwfDe9qLDuq7U1OYM2wUY+KQ4Ex6ozwsKxp26BlJ6XXbHleOUldenM67JRyBfVjv371oneEvYd3H2gNSA==", + "version": "14.2.14", + "resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-14.2.14.tgz", + "integrity": "sha512-cC9/I+0+SK5L1k9J8CInahduTVWGMXhQoXFeNvF0uNs3Bt1Ub0Azb8JzTU9vNCr0hnaMqiWu/Z0S1hfKc3+dww==", "cpu": [ "x64" ], @@ -285,9 +287,9 @@ } }, "node_modules/@next/swc-linux-arm64-gnu": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-14.2.5.tgz", - "integrity": "sha512-vlhB8wI+lj8q1ExFW8lbWutA4M2ZazQNvMWuEDqZcuJJc78iUnLdPPunBPX8rC4IgT6lIx/adB+Cwrl99MzNaA==", + "version": "14.2.14", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-14.2.14.tgz", + "integrity": "sha512-RMLOdA2NU4O7w1PQ3Z9ft3PxD6Htl4uB2TJpocm+4jcllHySPkFaUIFacQ3Jekcg6w+LBaFvjSPthZHiPmiAUg==", "cpu": [ "arm64" ], @@ -300,9 +302,9 @@ } }, "node_modules/@next/swc-linux-arm64-musl": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-14.2.5.tgz", - "integrity": "sha512-NpDB9NUR2t0hXzJJwQSGu1IAOYybsfeB+LxpGsXrRIb7QOrYmidJz3shzY8cM6+rO4Aojuef0N/PEaX18pi9OA==", + "version": "14.2.14", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-14.2.14.tgz", + "integrity": "sha512-WgLOA4hT9EIP7jhlkPnvz49iSOMdZgDJVvbpb8WWzJv5wBD07M2wdJXLkDYIpZmCFfo/wPqFsFR4JS4V9KkQ2A==", "cpu": [ "arm64" ], @@ -315,9 +317,9 @@ } }, "node_modules/@next/swc-linux-x64-gnu": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-14.2.5.tgz", - "integrity": "sha512-8XFikMSxWleYNryWIjiCX+gU201YS+erTUidKdyOVYi5qUQo/gRxv/3N1oZFCgqpesN6FPeqGM72Zve+nReVXQ==", + "version": "14.2.14", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-14.2.14.tgz", + "integrity": "sha512-lbn7svjUps1kmCettV/R9oAvEW+eUI0lo0LJNFOXoQM5NGNxloAyFRNByYeZKL3+1bF5YE0h0irIJfzXBq9Y6w==", "cpu": [ "x64" ], @@ -330,9 +332,9 @@ } }, "node_modules/@next/swc-linux-x64-musl": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-14.2.5.tgz", - "integrity": "sha512-6QLwi7RaYiQDcRDSU/os40r5o06b5ue7Jsk5JgdRBGGp8l37RZEh9JsLSM8QF0YDsgcosSeHjglgqi25+m04IQ==", + "version": "14.2.14", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-14.2.14.tgz", + "integrity": "sha512-7TcQCvLQ/hKfQRgjxMN4TZ2BRB0P7HwrGAYL+p+m3u3XcKTraUFerVbV3jkNZNwDeQDa8zdxkKkw2els/S5onQ==", "cpu": [ "x64" ], @@ -345,9 +347,9 @@ } }, "node_modules/@next/swc-win32-arm64-msvc": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-14.2.5.tgz", - "integrity": "sha512-1GpG2VhbspO+aYoMOQPQiqc/tG3LzmsdBH0LhnDS3JrtDx2QmzXe0B6mSZZiN3Bq7IOMXxv1nlsjzoS1+9mzZw==", + "version": "14.2.14", + "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-14.2.14.tgz", + "integrity": "sha512-8i0Ou5XjTLEje0oj0JiI0Xo9L/93ghFtAUYZ24jARSeTMXLUx8yFIdhS55mTExq5Tj4/dC2fJuaT4e3ySvXU1A==", "cpu": [ "arm64" ], @@ -360,9 +362,9 @@ } }, "node_modules/@next/swc-win32-ia32-msvc": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.2.5.tgz", - "integrity": "sha512-Igh9ZlxwvCDsu6438FXlQTHlRno4gFpJzqPjSIBZooD22tKeI4fE/YMRoHVJHmrQ2P5YL1DoZ0qaOKkbeFWeMg==", + "version": "14.2.14", + "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.2.14.tgz", + "integrity": "sha512-2u2XcSaDEOj+96eXpyjHjtVPLhkAFw2nlaz83EPeuK4obF+HmtDJHqgR1dZB7Gb6V/d55FL26/lYVd0TwMgcOQ==", "cpu": [ "ia32" ], @@ -375,9 +377,9 @@ } }, "node_modules/@next/swc-win32-x64-msvc": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-14.2.5.tgz", - "integrity": "sha512-tEQ7oinq1/CjSG9uSTerca3v4AZ+dFa+4Yu6ihaG8Ud8ddqLQgFGcnwYls13H5X5CPDPZJdYxyeMui6muOLd4g==", + "version": "14.2.14", + "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-14.2.14.tgz", + "integrity": "sha512-MZom+OvZ1NZxuRovKt1ApevjiUJTcU2PmdJKL66xUPaJeRywnbGGRWUlaAOwunD6dX+pm83vj979NTC8QXjGWg==", "cpu": [ "x64" ], @@ -472,6 +474,15 @@ "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "dev": true }, + "node_modules/@types/jsonwebtoken": { + "version": "9.0.7", + "resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-9.0.7.tgz", + "integrity": "sha512-ugo316mmTYBl2g81zDFnZ7cfxlut3o+/EQdaP7J8QN2kY6lJ22hmQYCK5EHcJHbrW+dkCGSCPgbG8JtYj6qSrg==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/node": { "version": "20.14.14", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.14.tgz", @@ -1108,6 +1119,11 @@ "node": ">=16.20.1" } }, + "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/busboy": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", @@ -1599,6 +1615,14 @@ "integrity": "sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==", "dev": true }, + "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/emoji-regex": { "version": "9.2.2", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-9.2.2.tgz", @@ -3535,6 +3559,27 @@ "json5": "lib/cli.js" } }, + "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/jsx-ast-utils": { "version": "3.3.5", "resolved": "https://registry.npmjs.org/jsx-ast-utils/-/jsx-ast-utils-3.3.5.tgz", @@ -3550,6 +3595,25 @@ "node": ">=4.0" } }, + "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/kareem": { "version": "2.6.3", "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.6.3.tgz", @@ -3784,12 +3848,47 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "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.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==" + }, "node_modules/log-update": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/log-update/-/log-update-6.1.0.tgz", @@ -3957,9 +4056,9 @@ } }, "node_modules/micromatch": { - "version": "4.0.7", - "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.7.tgz", - "integrity": "sha512-LPP/3KorzCwBxfeUuZmaR6bG2kdeHSbe0P2tY3FLRU4vYrjYz5hI4QZwV0njUx3jeuKe67YukQ1LSPZBKDqO/Q==", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dev": true, "dependencies": { "braces": "^3.0.3", @@ -4181,11 +4280,11 @@ "dev": true }, "node_modules/next": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/next/-/next-14.2.5.tgz", - "integrity": "sha512-0f8aRfBVL+mpzfBjYfQuLWh2WyAwtJXCRfkPF4UJ5qd2YwrHczsrSzXU4tRMV0OAxR8ZJZWPFn6uhSC56UTsLA==", + "version": "14.2.14", + "resolved": "https://registry.npmjs.org/next/-/next-14.2.14.tgz", + "integrity": "sha512-Q1coZG17MW0Ly5x76shJ4dkC23woLAhhnDnw+DfTc7EpZSGuWrlsZ3bZaO8t6u1Yu8FVfhkqJE+U8GC7E0GLPQ==", "dependencies": { - "@next/env": "14.2.5", + "@next/env": "14.2.14", "@swc/helpers": "0.5.5", "busboy": "1.6.0", "caniuse-lite": "^1.0.30001579", @@ -4200,15 +4299,15 @@ "node": ">=18.17.0" }, "optionalDependencies": { - "@next/swc-darwin-arm64": "14.2.5", - "@next/swc-darwin-x64": "14.2.5", - "@next/swc-linux-arm64-gnu": "14.2.5", - "@next/swc-linux-arm64-musl": "14.2.5", - "@next/swc-linux-x64-gnu": "14.2.5", - "@next/swc-linux-x64-musl": "14.2.5", - "@next/swc-win32-arm64-msvc": "14.2.5", - "@next/swc-win32-ia32-msvc": "14.2.5", - "@next/swc-win32-x64-msvc": "14.2.5" + "@next/swc-darwin-arm64": "14.2.14", + "@next/swc-darwin-x64": "14.2.14", + "@next/swc-linux-arm64-gnu": "14.2.14", + "@next/swc-linux-arm64-musl": "14.2.14", + "@next/swc-linux-x64-gnu": "14.2.14", + "@next/swc-linux-x64-musl": "14.2.14", + "@next/swc-win32-arm64-msvc": "14.2.14", + "@next/swc-win32-ia32-msvc": "14.2.14", + "@next/swc-win32-x64-msvc": "14.2.14" }, "peerDependencies": { "@opentelemetry/api": "^1.1.0", @@ -5183,6 +5282,25 @@ "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" + } + ] + }, "node_modules/safe-regex-test": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/safe-regex-test/-/safe-regex-test-1.0.3.tgz", @@ -5212,7 +5330,6 @@ "version": "7.6.3", "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz", "integrity": "sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==", - "dev": true, "bin": { "semver": "bin/semver.js" }, diff --git a/client/package.json b/client/package.json index cb4c6cd..3178938 100644 --- a/client/package.json +++ b/client/package.json @@ -19,14 +19,16 @@ }, "dependencies": { "axios": "^1.7.7", + "jsonwebtoken": "^9.0.2", "mongodb": "^6.8.0", "mongoose": "^8.6.1", - "next": "14.2.5", + "next": "^14.2.14", "react": "^18", "react-dom": "^18", "zod": "^3.23.8" }, "devDependencies": { + "@types/jsonwebtoken": "^9.0.7", "@types/node": "^20", "@types/react": "^18", "@types/react-dom": "^18", diff --git a/client/src/components/LoginForm.tsx b/client/src/components/LoginForm.tsx index 84f760f..882e857 100644 --- a/client/src/components/LoginForm.tsx +++ b/client/src/components/LoginForm.tsx @@ -19,14 +19,12 @@ export function LoginForm() { try { const token = await login(email, password); - console.log("Logged in User:", token); - if (token) { - router.replace("/"); - // Add token to the request header axios.defaults.headers.common["Authorization"] = `Bearer ${token}`; window.localStorage.setItem("token", token); + + router.replace("/hereeast"); } } catch (err) { // Err if username exists diff --git a/client/src/components/layouts/AuthLayout.tsx b/client/src/components/layouts/AuthLayout.tsx new file mode 100644 index 0000000..b6289d2 --- /dev/null +++ b/client/src/components/layouts/AuthLayout.tsx @@ -0,0 +1,30 @@ +import { useRouter } from "next/router"; +import { ReactNode, useEffect } from "react"; +import jwt from "jsonwebtoken"; + +import { useAuth } from "~/hooks"; + +interface AuthLayoutProps { + children: ReactNode; +} + +export function AuthLayout({ children }: AuthLayoutProps) { + const router = useRouter(); + + const { isAuth } = useAuth(); + + useEffect(() => { + if (isAuth) { + const token = localStorage.getItem("token") || ""; + const decodedUser = jwt.decode(token); + + console.log(decodedUser); + } else { + console.log("Auth", isAuth); + + // router.replace("/"); + } + }, [isAuth, router]); + + return <>{isAuth ? children : null}; +} diff --git a/client/src/components/layouts/AuthRedirect.tsx b/client/src/components/layouts/AuthRedirect.tsx deleted file mode 100644 index 3568c9e..0000000 --- a/client/src/components/layouts/AuthRedirect.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import { useRouter } from "next/router"; -import { ReactNode, useEffect, useState } from "react"; - -interface AuthRedirectProps { - children: ReactNode; -} - -export function AuthRedirect({ children }: AuthRedirectProps) { - const router = useRouter(); - - const [isAuth, setIsAuth] = useState(null); - - useEffect(() => { - if (typeof window !== "undefined") { - const token = localStorage.getItem("token"); - - if (token) { - setIsAuth(true); - } else { - router.replace("/"); - } - - } - }, [router]); - - if (isAuth === null) { - return null; - } - - return <>{isAuth ? children : null}; -} diff --git a/client/src/components/layouts/Header.tsx b/client/src/components/layouts/Header.tsx index bb35d81..21d1e70 100644 --- a/client/src/components/layouts/Header.tsx +++ b/client/src/components/layouts/Header.tsx @@ -1,11 +1,17 @@ import Link from "next/link"; -import { Button } from "../ui/Button"; import { useRouter } from "next/router"; +import { Button } from "../ui/Button"; +import { useAuth } from "~/hooks"; + export function Header() { const router = useRouter(); + // const { setIsAuth } = useAuth(); + function handleLogout() { + // setIsAuth(null); + localStorage.removeItem("token"); router.replace("/"); } diff --git a/client/src/components/layouts/Login.tsx b/client/src/components/layouts/Login.tsx new file mode 100644 index 0000000..83db3ff --- /dev/null +++ b/client/src/components/layouts/Login.tsx @@ -0,0 +1,42 @@ +import { useEffect } from "react"; +import Link from "next/link"; + +import { LoginForm } from "../LoginForm"; +import { useAuth } from "~/hooks"; +import { useRouter } from "next/router"; + +export function Login() { + const router = useRouter(); + + // const { isAuth } = useAuth(); + + // useEffect(() => { + // if (isAuth) { + // router.replace("/"); + // } + // }, [isAuth]); + + return ( +
+
+

👋 Hey there!

+
+ +
+
+ +
+ +
+ Don't have an account yet? + + Create Account + +
+
+
+ ); +} diff --git a/client/src/components/layouts/Register.tsx b/client/src/components/layouts/Register.tsx new file mode 100644 index 0000000..7184793 --- /dev/null +++ b/client/src/components/layouts/Register.tsx @@ -0,0 +1,42 @@ +import { useEffect } from "react"; +import { useRouter } from "next/router"; +import Link from "next/link"; + +import { RegisterForm } from "../RegisterForm"; +import { useAuth } from "~/hooks"; + +export function Register() { + const router = useRouter(); + + // const { isAuth } = useAuth(); + + // useEffect(() => { + // if (isAuth) { + // router.replace("/"); + // } + // }, [isAuth, router]); + + return ( +
+
+

Register, please 😊

+
+ +
+
+ +
+ +
+ Been there already? + + Login + +
+
+
+ ); +} diff --git a/client/src/components/layouts/index.ts b/client/src/components/layouts/index.ts index dad18b1..06940ca 100644 --- a/client/src/components/layouts/index.ts +++ b/client/src/components/layouts/index.ts @@ -1,5 +1,6 @@ export * from "./Dashboard"; export * from "./Layout"; -export * from "./AuthRedirect"; +export * from "./AuthLayout"; export * from "./Header"; export * from "./Footer"; +export * from "./Login"; diff --git a/client/src/hooks/index.ts b/client/src/hooks/index.ts index df0f5d3..67a7119 100644 --- a/client/src/hooks/index.ts +++ b/client/src/hooks/index.ts @@ -4,3 +4,4 @@ export * from "./useEntries"; export * from "./useAppContext"; export * from "./useMonthContext"; export * from "./useMonthRating"; +export * from "./useAuth"; diff --git a/client/src/hooks/useAuth.tsx b/client/src/hooks/useAuth.tsx new file mode 100644 index 0000000..a756e3c --- /dev/null +++ b/client/src/hooks/useAuth.tsx @@ -0,0 +1,17 @@ +import { useEffect, useState } from "react"; + +export function useAuth() { + const [isAuth, setIsAuth] = useState(null); + + useEffect(() => { + if (typeof window !== "undefined") { + const token = localStorage.getItem("token"); + + if (token) { + setIsAuth(true); + } + } + }, []); + + return { isAuth, setIsAuth }; +} diff --git a/client/src/pages/[slug].tsx b/client/src/pages/[slug].tsx index 5ba21eb..206e4c6 100644 --- a/client/src/pages/[slug].tsx +++ b/client/src/pages/[slug].tsx @@ -1,21 +1,12 @@ -import Head from "next/head"; - -import { Dashboard, AuthRedirect } from "~/components/layouts"; +import { Dashboard, AuthLayout } from "~/components/layouts"; import { MonthContextProvider } from "~/context"; export default function DashboardPage() { return ( - - {/* - Habit Tracker - - - - */} - + - + ); } diff --git a/client/src/pages/_app.tsx b/client/src/pages/_app.tsx index 8a16321..8e603b1 100644 --- a/client/src/pages/_app.tsx +++ b/client/src/pages/_app.tsx @@ -1,10 +1,21 @@ import "~/styles/globals.css"; import type { AppProps } from "next/app"; +import { useEffect } from "react"; +import axios from "axios"; + import { AppContextProvider } from "~/context"; import { Layout } from "~/components/layouts"; export default function App({ Component, pageProps }: AppProps) { + useEffect(() => { + const token = localStorage.getItem("token"); + + if (token) { + axios.defaults.headers.common["Authorization"] = `Bearer ${token}`; + } + }, []); + return ( diff --git a/client/src/pages/login.tsx b/client/src/pages/login.tsx index 2e7acf8..ebbbd0a 100644 --- a/client/src/pages/login.tsx +++ b/client/src/pages/login.tsx @@ -1,44 +1,5 @@ -import Link from "next/link"; -import { useRouter } from "next/router"; -import { useEffect, useState } from "react"; - -import { LoginForm } from "~/components/LoginForm"; +import { Login } from "~/components/layouts"; export default function LoginPage() { - const router = useRouter(); - - const [view, setView] = useState<"login" | "register">("login"); - - const isAuthenticated = false; - const slug = "hereeast"; - - useEffect(() => { - if (isAuthenticated) { - router.replace(`/${slug}`); - } - }, [isAuthenticated, router]); - - return ( -
-
-

👋 Hey there!

-
- -
-
- -
- -
- Don't have an account yet? - - Create Account - -
-
-
- ); + return ; } diff --git a/client/src/pages/register.tsx b/client/src/pages/register.tsx index 5f582d1..35f1c0a 100644 --- a/client/src/pages/register.tsx +++ b/client/src/pages/register.tsx @@ -1,42 +1,5 @@ -import Link from "next/link"; -import { useRouter } from "next/router"; -import { useEffect } from "react"; - -import { RegisterForm } from "~/components/RegisterForm"; +import { Register } from "~/components/layouts/Register"; export default function RegisterPage() { - const router = useRouter(); - - const isAuthenticated = false; - const slug = "hereeast"; - - useEffect(() => { - if (isAuthenticated) { - router.replace(`/${slug}`); - } - }, [isAuthenticated, router]); - - return ( -
-
-

Register, please 😊

-
- -
-
- -
- -
- Been there already? - - Login - -
-
-
- ); + return ; } diff --git a/server/src/controllers/login.ts b/server/src/controllers/login.ts index b2e5459..ee03fb4 100644 --- a/server/src/controllers/login.ts +++ b/server/src/controllers/login.ts @@ -34,6 +34,7 @@ export async function login(req: Request, res: Response) { const userPayload = { _id: user._id, email: user.email, + username: user.username, }; const token = jwt.sign(userPayload, SECRET_KEY || "", { expiresIn: "7d" });