diff --git a/client/package-lock.json b/client/package-lock.json index 4c8bc5c..4621219 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -2562,7 +2562,7 @@ "version": "0.19.2", "resolved": "https://registry.npmjs.org/@eslint/config-array/-/config-array-0.19.2.tgz", "integrity": "sha512-GNKqxfHG2ySmJOBSHg7LxeUx4xpuCoFjacmlCoYWEbaPXLwvfIjixRI12xCQZeULksQb23uiA8F40w5TojpV7w==", - "dev": true, + "devOptional": true, "license": "Apache-2.0", "dependencies": { "@eslint/object-schema": "^2.1.6", @@ -2601,7 +2601,7 @@ "version": "0.12.0", "resolved": "https://registry.npmjs.org/@eslint/core/-/core-0.12.0.tgz", "integrity": "sha512-cmrR6pytBuSMTaBweKoGMwu3EiHiEC+DoyupPmlZ0HxBJBtIxwe+j/E4XPIKNx+Q74c8lXKPwYawBf5glsTkHg==", - "dev": true, + "devOptional": true, "license": "Apache-2.0", "dependencies": { "@types/json-schema": "^7.0.15" @@ -2614,7 +2614,7 @@ "version": "3.3.0", "resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-3.3.0.tgz", "integrity": "sha512-yaVPAiNAalnCZedKLdR21GOGILMLKPyqSLWaAjQFvYA2i/ciDi8ArYVr69Anohb6cH2Ukhqti4aFnYyPm8wdwQ==", - "dev": true, + "devOptional": true, "license": "MIT", "dependencies": { "ajv": "^6.12.4", @@ -2675,7 +2675,7 @@ "version": "9.21.0", "resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.21.0.tgz", "integrity": "sha512-BqStZ3HX8Yz6LvsF5ByXYrtigrV5AXADWLAGc7PH/1SxOb7/FIYYMszZZWiUou/GB9P2lXWk2SV4d+Z8h0nknw==", - "dev": true, + "devOptional": true, "license": "MIT", "engines": { "node": "^18.18.0 || ^20.9.0 || >=21.1.0" @@ -2685,7 +2685,7 @@ "version": "2.1.6", "resolved": "https://registry.npmjs.org/@eslint/object-schema/-/object-schema-2.1.6.tgz", "integrity": "sha512-RBMg5FRL0I0gs51M/guSAj5/e14VQ4tpZnQNWwuDT66P14I43ItmPfIZRhO9fUVIPOAQXU47atlywZ/czoqFPA==", - "dev": true, + "devOptional": true, "license": "Apache-2.0", "engines": { "node": "^18.18.0 || ^20.9.0 || >=21.1.0" @@ -2695,7 +2695,7 @@ "version": "0.2.7", "resolved": "https://registry.npmjs.org/@eslint/plugin-kit/-/plugin-kit-0.2.7.tgz", "integrity": "sha512-JubJ5B2pJ4k4yGxaNLdbjrnk9d/iDz6/q8wOilpIowd6PJPgaxCuHBnBszq7Ce2TyMrywm5r4PnKm6V3iiZF+g==", - "dev": true, + "devOptional": true, "license": "Apache-2.0", "dependencies": { "@eslint/core": "^0.12.0", @@ -2826,7 +2826,7 @@ "version": "0.3.1", "resolved": "https://registry.npmjs.org/@humanwhocodes/retry/-/retry-0.3.1.tgz", "integrity": "sha512-JBxkERygn7Bv/GbN5Rv8Ul6LVknS+5Bp6RgDC/O8gEBU/yeH5Ui5C/OlWrTb6qct7LjjfT6Re2NxB0ln0yYybA==", - "dev": true, + "devOptional": true, "license": "Apache-2.0", "engines": { "node": ">=18.18" @@ -2854,7 +2854,7 @@ "version": "0.4.2", "resolved": "https://registry.npmjs.org/@humanwhocodes/retry/-/retry-0.4.2.tgz", "integrity": "sha512-xeO57FpIu4p1Ri3Jq/EXq4ClRm86dVF2z/+kvFnyqVYRavTZmaFaUBbWCOuuTh0o/g7DSsk6kc2vrS4Vl5oPOQ==", - "dev": true, + "devOptional": true, "license": "Apache-2.0", "engines": { "node": ">=18.18" @@ -3973,7 +3973,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -3987,7 +3986,6 @@ "cpu": [ "loong64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -4132,7 +4130,7 @@ "version": "1.11.5", "resolved": "https://registry.npmjs.org/@swc/core/-/core-1.11.5.tgz", "integrity": "sha512-EVY7zfpehxhTZXOfy508gb3D78ihoGGmvyiTWtlBPjgIaidP1Xw0naHMD78CWiFlZmeDjKXJufGtsEGOnZdmNA==", - "dev": true, + "devOptional": true, "hasInstallScript": true, "license": "Apache-2.0", "dependencies": { @@ -4338,7 +4336,7 @@ "version": "0.1.19", "resolved": "https://registry.npmjs.org/@swc/types/-/types-0.1.19.tgz", "integrity": "sha512-WkAZaAfj44kh/UFdAQcrMP1I0nwRqpt27u+08LMBYMqmQfwwMofYoMh/48NGkMMRfC4ynpfwRbJuu8ErfNloeA==", - "dev": true, + "devOptional": true, "license": "Apache-2.0", "dependencies": { "@swc/counter": "^0.1.3" @@ -4687,7 +4685,6 @@ "version": "18.3.18", "resolved": "https://registry.npmjs.org/@types/react/-/react-18.3.18.tgz", "integrity": "sha512-t4yC+vtgnkYjNSKlFx1jkAhH8LgTo2N/7Qvi83kdEaUtMDiwpbLAktKDaAMlRcJ5eSxZkH74eEGt1ky31d7kfQ==", - "dev": true, "license": "MIT", "dependencies": { "@types/prop-types": "*", @@ -5123,6 +5120,19 @@ "node": ">= 8" } }, + "node_modules/anymatch/node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/arg": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", @@ -5519,7 +5529,7 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", - "dev": true, + "devOptional": true, "license": "MIT" }, "node_modules/brace-expansion": { @@ -5854,7 +5864,7 @@ "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", - "dev": true, + "devOptional": true, "license": "MIT" }, "node_modules/convert-source-map": { @@ -5893,6 +5903,15 @@ "node": ">=10" } }, + "node_modules/cosmiconfig/node_modules/yaml": { + "version": "1.10.2", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.10.2.tgz", + "integrity": "sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==", + "license": "ISC", + "engines": { + "node": ">= 6" + } + }, "node_modules/create-jest": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/create-jest/-/create-jest-29.7.0.tgz", @@ -6662,7 +6681,7 @@ "version": "9.21.0", "resolved": "https://registry.npmjs.org/eslint/-/eslint-9.21.0.tgz", "integrity": "sha512-KjeihdFqTPhOMXTt7StsDxriV4n66ueuF/jfPNC3j/lduHwr/ijDwJMsF+wyMJethgiKi5wniIE243vi07d3pg==", - "dev": true, + "devOptional": true, "license": "MIT", "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", @@ -6718,59 +6737,6 @@ } } }, - "node_modules/eslint-plugin-eslint-comments": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-eslint-comments/-/eslint-plugin-eslint-comments-3.2.0.tgz", - "integrity": "sha512-0jkOl0hfojIHHmEHgmNdqv4fmh7300NdpA9FFpF7zaoLvB/QeXOGNLIo86oAveJFrfB1p05kC8hpEMHM8DwWVQ==", - "dev": true, - "dependencies": { - "escape-string-regexp": "^1.0.5", - "ignore": "^5.0.5" - }, - "engines": { - "node": ">=6.5.0" - }, - "funding": { - "url": "https://github.com/sponsors/mysticatea" - }, - "peerDependencies": { - "eslint": ">=4.19.1" - } - }, - "node_modules/eslint-plugin-eslint-comments/node_modules/escape-string-regexp": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==", - "dev": true, - "engines": { - "node": ">=0.8.0" - } - }, - "node_modules/eslint-plugin-jest": { - "version": "28.11.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-jest/-/eslint-plugin-jest-28.11.0.tgz", - "integrity": "sha512-QAfipLcNCWLVocVbZW8GimKn5p5iiMcgGbRzz8z/P5q7xw+cNEpYqyzFMtIF/ZgF2HLOyy+dYBut+DoYolvqig==", - "dev": true, - "dependencies": { - "@typescript-eslint/utils": "^6.0.0 || ^7.0.0 || ^8.0.0" - }, - "engines": { - "node": "^16.10.0 || ^18.12.0 || >=20.0.0" - }, - "peerDependencies": { - "@typescript-eslint/eslint-plugin": "^6.0.0 || ^7.0.0 || ^8.0.0", - "eslint": "^7.0.0 || ^8.0.0 || ^9.0.0", - "jest": "*" - }, - "peerDependenciesMeta": { - "@typescript-eslint/eslint-plugin": { - "optional": true - }, - "jest": { - "optional": true - } - } - }, "node_modules/eslint-plugin-react": { "version": "7.37.4", "resolved": "https://registry.npmjs.org/eslint-plugin-react/-/eslint-plugin-react-7.37.4.tgz", @@ -6869,21 +6835,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/eslint-plugin-unused-imports": { - "version": "4.1.4", - "resolved": "https://registry.npmjs.org/eslint-plugin-unused-imports/-/eslint-plugin-unused-imports-4.1.4.tgz", - "integrity": "sha512-YptD6IzQjDardkl0POxnnRBhU1OEePMV0nd6siHaRBbd+lyh6NAhFEobiznKU7kTsSsDeSD62Pe7kAM1b7dAZQ==", - "dev": true, - "peerDependencies": { - "@typescript-eslint/eslint-plugin": "^8.0.0-0 || ^7.0.0 || ^6.0.0 || ^5.0.0", - "eslint": "^9.0.0 || ^8.0.0" - }, - "peerDependenciesMeta": { - "@typescript-eslint/eslint-plugin": { - "optional": true - } - } - }, "node_modules/eslint-scope": { "version": "8.2.0", "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-8.2.0.tgz", @@ -7275,7 +7226,7 @@ "version": "3.3.3", "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.3.3.tgz", "integrity": "sha512-GX+ysw4PBCz0PzosHDepZGANEuFCMLrnRTiEy9McGjmkCQYwRq4A/X786G/fjM/+OjsWSU1ZrY5qyARZmO/uwg==", - "dev": true, + "devOptional": true, "license": "ISC" }, "node_modules/follow-redirects": { @@ -7340,7 +7291,6 @@ "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", - "dev": true, "hasInstallScript": true, "license": "MIT", "optional": true, @@ -8049,7 +7999,7 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", - "dev": true, + "devOptional": true, "license": "MIT", "engines": { "node": ">=0.10.0" @@ -8114,7 +8064,7 @@ "version": "4.0.3", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", - "dev": true, + "devOptional": true, "license": "MIT", "dependencies": { "is-extglob": "^2.1.1" @@ -9321,6 +9271,18 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, + "node_modules/jest-util/node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "license": "MIT", + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/jest-validate": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/jest-validate/-/jest-validate-29.7.0.tgz", @@ -9574,6 +9536,7 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz", "integrity": "sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA==", + "license": "MIT", "engines": { "node": ">=18" } @@ -10378,6 +10341,18 @@ "node": ">=8.6" } }, + "node_modules/micromatch/node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "license": "MIT", + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", @@ -10785,7 +10760,7 @@ "version": "3.1.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", - "dev": true, + "devOptional": true, "license": "MIT", "engines": { "node": ">=8" @@ -10825,12 +10800,12 @@ "license": "ISC" }, "node_modules/picomatch": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", - "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.2.tgz", + "integrity": "sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==", "license": "MIT", "engines": { - "node": ">=8.6" + "node": ">=12" }, "funding": { "url": "https://github.com/sponsors/jonschlinkert" @@ -10929,7 +10904,6 @@ "version": "8.5.3", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.3.tgz", "integrity": "sha512-dle9A3yYxlBSrt8Fu+IpjGT8SY8hN0mlaA6GY8t0P5PjIOZemULz/E2Bnm/2dcUOena75OTNkHI76uZBNUUq3A==", - "dev": true, "funding": [ { "type": "opencollective", @@ -11487,7 +11461,6 @@ "version": "4.34.8", "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.34.8.tgz", "integrity": "sha512-489gTVMzAYdiZHFVA/ig/iYFllCcWFHMvUHI1rpFmkoUtRlQxqh6/yiNqnYibjMZ2b/+FUQwldG+aLsEt6bglQ==", - "dev": true, "license": "MIT", "dependencies": { "@types/estree": "1.0.6" @@ -12221,18 +12194,6 @@ "url": "https://github.com/sponsors/SuperchupuDev" } }, - "node_modules/tinyglobby/node_modules/picomatch": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.2.tgz", - "integrity": "sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==", - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/jonschlinkert" - } - }, "node_modules/tmpl": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/tmpl/-/tmpl-1.0.5.tgz", @@ -12524,7 +12485,6 @@ "version": "5.7.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.7.3.tgz", "integrity": "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw==", - "dev": true, "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", @@ -12859,7 +12819,6 @@ "version": "6.2.0", "resolved": "https://registry.npmjs.org/vite/-/vite-6.2.0.tgz", "integrity": "sha512-7dPxoo+WsT/64rDcwoOjk76XHj+TqNTIvHKcuMQ1k4/SeHDaQt5GFAeLYzrimZrMpn/O6DtdI03WUjdxuPM0oQ==", - "dev": true, "license": "MIT", "dependencies": { "esbuild": "^0.25.0", @@ -13028,18 +12987,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/vite-plugin-checker/node_modules/picomatch": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.2.tgz", - "integrity": "sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==", - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/jonschlinkert" - } - }, "node_modules/vite-plugin-checker/node_modules/strip-ansi": { "version": "7.1.0", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.0.tgz", @@ -13360,12 +13307,17 @@ "license": "ISC" }, "node_modules/yaml": { - "version": "1.10.2", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.10.2.tgz", - "integrity": "sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==", + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.7.0.tgz", + "integrity": "sha512-+hSoy/QHluxmC9kCIJyL/uyFmLmc+e5CFR5Wa+bpIhIj85LVb9ZH2nVnqrHoSvKogwODv0ClqZkmiSSaIH5LTA==", "license": "ISC", + "optional": true, + "peer": true, + "bin": { + "yaml": "bin.mjs" + }, "engines": { - "node": ">= 6" + "node": ">= 14" } }, "node_modules/yargs": { diff --git a/client/src/__tests__/services/WebsocketService.test.tsx b/client/src/__tests__/services/WebsocketService.test.tsx index cb34276..d21423e 100644 --- a/client/src/__tests__/services/WebsocketService.test.tsx +++ b/client/src/__tests__/services/WebsocketService.test.tsx @@ -38,7 +38,7 @@ describe('WebSocketService', () => { test('createRoom should emit create-room event', () => { const roomName = 'Test Room'; - WebsocketService.connect(ENV_VARIABLES.VITE_BACKEND_SOCKET_URL); + WebsocketService.connect(ENV_VARIABLES.VITE_BACKEND_URL); WebsocketService.createRoom(roomName); expect(mockSocket.emit).toHaveBeenCalledWith('create-room', roomName); }); diff --git a/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx b/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx index 35e267b..3a67c27 100644 --- a/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx +++ b/client/src/pages/Teacher/ManageRoom/ManageRoom.tsx @@ -105,7 +105,7 @@ const ManageRoom: React.FC = () => { }; const createWebSocketRoom = () => { - const socket = webSocketService.connect(ENV_VARIABLES.VITE_BACKEND_SOCKET_URL); + const socket = webSocketService.connect(ENV_VARIABLES.VITE_BACKEND_URL); const roomNameUpper = roomName.toUpperCase(); setFormattedRoomName(roomNameUpper); console.log(`Creating WebSocket room named ${roomNameUpper}`); diff --git a/client/src/services/ApiService.tsx b/client/src/services/ApiService.tsx index 3511258..f1c4134 100644 --- a/client/src/services/ApiService.tsx +++ b/client/src/services/ApiService.tsx @@ -1,9 +1,10 @@ import axios, { AxiosError, AxiosResponse } from 'axios'; +import { jwtDecode } from 'jwt-decode'; +import { ENV_VARIABLES } from '../constants'; import { FolderType } from 'src/Types/FolderType'; import { QuizType } from 'src/Types/QuizType'; import { RoomType } from 'src/Types/RoomType'; -import { ENV_VARIABLES } from 'src/constants'; type ApiResponse = boolean | string; @@ -35,7 +36,7 @@ class ApiService { } // Helpers - private saveToken(token: string): void { + public saveToken(token: string): void { const now = new Date(); const object = { @@ -153,6 +154,7 @@ class ApiService { } public logout(): void { + localStorage.removeItem("username"); return localStorage.removeItem("jwt"); } @@ -162,21 +164,25 @@ class ApiService { * @returns true if successful * @returns A error string if unsuccessful, */ - public async register(email: string, password: string): Promise { + public async register(name: string, email: string, password: string, roles: string[]): Promise { try { if (!email || !password) { throw new Error(`L'email et le mot de passe sont requis.`); } - const url: string = this.constructRequestUrl(`/user/register`); + const url: string = this.constructRequestUrl(`/auth/simple-auth/register`); const headers = this.constructRequestHeaders(); - const body = { email, password }; + const body = { name, email, password, roles }; const result: AxiosResponse = await axios.post(url, body, { headers: headers }); - if (result.status !== 200) { - throw new Error(`L'enregistrement a échoué. Status: ${result.status}`); + console.log(result); + if (result.status == 200) { + window.location.href = result.request.responseURL; + } + else { + throw new Error(`La connexion a échoué. Status: ${result.status}`); } return true; @@ -198,44 +204,52 @@ class ApiService { * @returns true if successful * @returns A error string if unsuccessful, */ - public async login(email: string, password: string): Promise { - try { - - if (!email || !password) { - throw new Error(`L'email et le mot de passe sont requis.`); - } - - const url: string = this.constructRequestUrl(`/user/login`); - const headers = this.constructRequestHeaders(); - const body = { email, password }; - - const result: AxiosResponse = await axios.post(url, body, { headers: headers }); - - if (result.status !== 200) { - throw new Error(`La connexion a échoué. Status: ${result.status}`); - } - - this.saveToken(result.data.token); - - return true; - - } catch (error) { - console.log("Error details: ", error); - - console.log("axios.isAxiosError(error): ", axios.isAxiosError(error)); - - if (axios.isAxiosError(error)) { - const err = error as AxiosError; - if (err.status === 401) { - return 'Email ou mot de passe incorrect.'; - } - const data = err.response?.data as { error: string } | undefined; - return data?.error || 'Erreur serveur inconnue lors de la requête.'; - } - - return `Une erreur inattendue s'est produite.` + /** + * @returns true if successful + * @returns An error string if unsuccessful + */ +public async login(email: string, password: string): Promise { + try { + if (!email || !password) { + throw new Error("L'email et le mot de passe sont requis."); } + + const url: string = this.constructRequestUrl(`/auth/simple-auth/login`); + const headers = this.constructRequestHeaders(); + const body = { email, password }; + + const result: AxiosResponse = await axios.post(url, body, { headers: headers }); + + // If login is successful, redirect the user + if (result.status === 200) { + window.location.href = result.request.responseURL; + return true; + } else { + throw new Error(`La connexion a échoué. Statut: ${result.status}`); + } + } catch (error) { + console.log("Error details:", error); + + // Handle Axios-specific errors + if (axios.isAxiosError(error)) { + const err = error as AxiosError; + const responseData = err.response?.data as { message?: string } | undefined; + + // If there is a message field in the response, print it + if (responseData?.message) { + console.log("Backend error message:", responseData.message); + return responseData.message; + } + + // If no message is found, return a fallback message + return "Erreur serveur inconnue lors de la requête."; + } + + // Handle other non-Axios errors + return "Une erreur inattendue s'est produite."; } +} + /** * @returns true if successful @@ -248,7 +262,7 @@ class ApiService { throw new Error(`L'email est requis.`); } - const url: string = this.constructRequestUrl(`/user/reset-password`); + const url: string = this.constructRequestUrl(`/auth/simple-auth/reset-password`); const headers = this.constructRequestHeaders(); const body = { email }; @@ -284,7 +298,7 @@ class ApiService { throw new Error(`L'email, l'ancien et le nouveau mot de passe sont requis.`); } - const url: string = this.constructRequestUrl(`/user/change-password`); + const url: string = this.constructRequestUrl(`/auth/simple-auth/change-password`); const headers = this.constructRequestHeaders(); const body = { email, oldPassword, newPassword }; @@ -452,194 +466,6 @@ class ApiService { } } - //ROOM routes - - public async getUserRooms(): Promise { - try { - const url: string = this.constructRequestUrl(`/room/getUserRooms`); - const headers = this.constructRequestHeaders(); - - const result: AxiosResponse = await axios.get(url, { headers: headers }); - - if (result.status !== 200) { - throw new Error(`L'obtention des salles utilisateur a échoué. Status: ${result.status}`); - } - - return result.data.data.map((room: RoomType) => ({ _id: room._id, title: room.title })); - - } catch (error) { - console.log("Error details: ", error); - - if (axios.isAxiosError(error)) { - const err = error as AxiosError; - const data = err.response?.data as { error: string } | undefined; - const url = err.config?.url || 'URL inconnue'; - return data?.error || `Erreur serveur inconnue lors de la requête (${url}).`; - } - - return `Une erreur inattendue s'est produite.` - } - } - - public async getRoomContent(roomId: string): Promise { - try { - const url = this.constructRequestUrl(`/room/${roomId}`); - const headers = this.constructRequestHeaders(); - - const response = await axios.get<{ data: RoomType }>(url, { headers }); - - if (response.status !== 200) { - throw new Error(`Failed to get room: ${response.status}`); - } - - return response.data.data; - - } catch (error) { - if (axios.isAxiosError(error)) { - const serverError = error.response?.data?.error; - throw new Error(serverError || 'Erreur serveur inconnue'); - } - throw new Error('Erreur réseau'); - } - } - - public async getRoomTitleByUserId(userId: string): Promise { - try { - if (!userId) { - throw new Error(`L'ID utilisateur est requis.`); - } - - const url: string = this.constructRequestUrl(`/room/getRoomTitleByUserId/${userId}`); - const headers = this.constructRequestHeaders(); - - const result: AxiosResponse = await axios.get(url, { headers }); - - if (result.status !== 200) { - throw new Error(`L'obtention des titres des salles a échoué. Status: ${result.status}`); - } - - return result.data.titles; - } catch (error) { - console.log("Error details: ", error); - if (axios.isAxiosError(error)) { - const err = error as AxiosError; - const data = err.response?.data as { error: string } | undefined; - return data?.error || 'Erreur serveur inconnue lors de la requête.'; - } - return `Une erreur inattendue s'est produite.`; - } - } - public async getRoomTitle(roomId: string): Promise { - try { - if (!roomId) { - throw new Error(`L'ID de la salle est requis.`); - } - - const url: string = this.constructRequestUrl(`/room/getRoomTitle/${roomId}`); - const headers = this.constructRequestHeaders(); - - const result: AxiosResponse = await axios.get(url, { headers }); - - if (result.status !== 200) { - throw new Error(`L'obtention du titre de la salle a échoué. Status: ${result.status}`); - } - - return result.data.title; - } catch (error) { - console.log("Error details: ", error); - if (axios.isAxiosError(error)) { - const err = error as AxiosError; - const data = err.response?.data as { error: string } | undefined; - return data?.error || 'Erreur serveur inconnue lors de la requête.'; - } - return `Une erreur inattendue s'est produite.`; - } - } - public async createRoom(title: string): Promise { - try { - if (!title) { - throw new Error("Le titre de la salle est requis."); - } - - const url: string = this.constructRequestUrl(`/room/create`); - const headers = this.constructRequestHeaders(); - const body = { title }; - - const result = await axios.post<{ roomId: string }>(url, body, { headers }); - return `Salle créée avec succès. ID de la salle: ${result.data.roomId}`; - - } catch (error) { - if (axios.isAxiosError(error)) { - const err = error as AxiosError; - - const serverMessage = (err.response?.data as { message?: string })?.message - || (err.response?.data as { error?: string })?.error - || err.message; - - if (err.response?.status === 409) { - throw new Error(serverMessage); - } - - throw new Error(serverMessage || "Erreur serveur inconnue"); - } - throw error; - } - } - - public async deleteRoom(roomId: string): Promise { - try { - if (!roomId) { - throw new Error(`L'ID de la salle est requis.`); - } - - const url: string = this.constructRequestUrl(`/room/delete/${roomId}`); - const headers = this.constructRequestHeaders(); - - const result: AxiosResponse = await axios.delete(url, { headers }); - - if (result.status !== 200) { - throw new Error(`La suppression de la salle a échoué. Status: ${result.status}`); - } - - return `Salle supprimée avec succès.`; - } catch (error) { - console.log("Error details: ", error); - if (axios.isAxiosError(error)) { - const err = error as AxiosError; - const data = err.response?.data as { error: string } | undefined; - return data?.error || 'Erreur serveur inconnue lors de la suppression de la salle.'; - } - return `Une erreur inattendue s'est produite.`; - } - } - - public async renameRoom(roomId: string, newTitle: string): Promise { - try { - if (!roomId || !newTitle) { - throw new Error(`L'ID de la salle et le nouveau titre sont requis.`); - } - - const url: string = this.constructRequestUrl(`/room/rename`); - const headers = this.constructRequestHeaders(); - const body = { roomId, newTitle }; - - const result: AxiosResponse = await axios.put(url, body, { headers }); - - if (result.status !== 200) { - throw new Error(`La mise à jour du titre de la salle a échoué. Status: ${result.status}`); - } - - return `Titre de la salle mis à jour avec succès.`; - } catch (error) { - console.log("Error details: ", error); - if (axios.isAxiosError(error)) { - const err = error as AxiosError; - const data = err.response?.data as { error: string } | undefined; - return data?.error || 'Erreur serveur inconnue lors de la mise à jour du titre.'; - } - return `Une erreur inattendue s'est produite.`; - } - } /** * @returns true if successful * @returns A error string if unsuccessful, @@ -775,7 +601,7 @@ class ApiService { return `Une erreur inattendue s'est produite.` } } - + // Quiz Routes /** @@ -1102,6 +928,195 @@ class ApiService { } } + //ROOM routes + + public async getUserRooms(): Promise { + try { + const url: string = this.constructRequestUrl(`/room/getUserRooms`); + const headers = this.constructRequestHeaders(); + + const result: AxiosResponse = await axios.get(url, { headers: headers }); + + if (result.status !== 200) { + throw new Error(`L'obtention des salles utilisateur a échoué. Status: ${result.status}`); + } + + return result.data.data.map((room: RoomType) => ({ _id: room._id, title: room.title })); + + } catch (error) { + console.log("Error details: ", error); + + if (axios.isAxiosError(error)) { + const err = error as AxiosError; + const data = err.response?.data as { error: string } | undefined; + const url = err.config?.url || 'URL inconnue'; + return data?.error || `Erreur serveur inconnue lors de la requête (${url}).`; + } + + return `Une erreur inattendue s'est produite.` + } + } + + public async getRoomContent(roomId: string): Promise { + try { + const url = this.constructRequestUrl(`/room/${roomId}`); + const headers = this.constructRequestHeaders(); + + const response = await axios.get<{ data: RoomType }>(url, { headers }); + + if (response.status !== 200) { + throw new Error(`Failed to get room: ${response.status}`); + } + + return response.data.data; + + } catch (error) { + if (axios.isAxiosError(error)) { + const serverError = error.response?.data?.error; + throw new Error(serverError || 'Erreur serveur inconnue'); + } + throw new Error('Erreur réseau'); + } + } + + public async getRoomTitleByUserId(userId: string): Promise { + try { + if (!userId) { + throw new Error(`L'ID utilisateur est requis.`); + } + + const url: string = this.constructRequestUrl(`/room/getRoomTitleByUserId/${userId}`); + const headers = this.constructRequestHeaders(); + + const result: AxiosResponse = await axios.get(url, { headers }); + + if (result.status !== 200) { + throw new Error(`L'obtention des titres des salles a échoué. Status: ${result.status}`); + } + + return result.data.titles; + } catch (error) { + console.log("Error details: ", error); + if (axios.isAxiosError(error)) { + const err = error as AxiosError; + const data = err.response?.data as { error: string } | undefined; + return data?.error || 'Erreur serveur inconnue lors de la requête.'; + } + return `Une erreur inattendue s'est produite.`; + } + } + public async getRoomTitle(roomId: string): Promise { + try { + if (!roomId) { + throw new Error(`L'ID de la salle est requis.`); + } + + const url: string = this.constructRequestUrl(`/room/getRoomTitle/${roomId}`); + const headers = this.constructRequestHeaders(); + + const result: AxiosResponse = await axios.get(url, { headers }); + + if (result.status !== 200) { + throw new Error(`L'obtention du titre de la salle a échoué. Status: ${result.status}`); + } + + return result.data.title; + } catch (error) { + console.log("Error details: ", error); + if (axios.isAxiosError(error)) { + const err = error as AxiosError; + const data = err.response?.data as { error: string } | undefined; + return data?.error || 'Erreur serveur inconnue lors de la requête.'; + } + return `Une erreur inattendue s'est produite.`; + } + } + public async createRoom(title: string): Promise { + try { + if (!title) { + throw new Error("Le titre de la salle est requis."); + } + + const url: string = this.constructRequestUrl(`/room/create`); + const headers = this.constructRequestHeaders(); + const body = { title }; + + const result = await axios.post<{ roomId: string }>(url, body, { headers }); + return `Salle créée avec succès. ID de la salle: ${result.data.roomId}`; + + } catch (error) { + if (axios.isAxiosError(error)) { + const err = error as AxiosError; + + const serverMessage = (err.response?.data as { message?: string })?.message + || (err.response?.data as { error?: string })?.error + || err.message; + + if (err.response?.status === 409) { + throw new Error(serverMessage); + } + + throw new Error(serverMessage || "Erreur serveur inconnue"); + } + throw error; + } + } + + public async deleteRoom(roomId: string): Promise { + try { + if (!roomId) { + throw new Error(`L'ID de la salle est requis.`); + } + + const url: string = this.constructRequestUrl(`/room/delete/${roomId}`); + const headers = this.constructRequestHeaders(); + + const result: AxiosResponse = await axios.delete(url, { headers }); + + if (result.status !== 200) { + throw new Error(`La suppression de la salle a échoué. Status: ${result.status}`); + } + + return `Salle supprimée avec succès.`; + } catch (error) { + console.log("Error details: ", error); + if (axios.isAxiosError(error)) { + const err = error as AxiosError; + const data = err.response?.data as { error: string } | undefined; + return data?.error || 'Erreur serveur inconnue lors de la suppression de la salle.'; + } + return `Une erreur inattendue s'est produite.`; + } + } + + public async renameRoom(roomId: string, newTitle: string): Promise { + try { + if (!roomId || !newTitle) { + throw new Error(`L'ID de la salle et le nouveau titre sont requis.`); + } + + const url: string = this.constructRequestUrl(`/room/rename`); + const headers = this.constructRequestHeaders(); + const body = { roomId, newTitle }; + + const result: AxiosResponse = await axios.put(url, body, { headers }); + + if (result.status !== 200) { + throw new Error(`La mise à jour du titre de la salle a échoué. Status: ${result.status}`); + } + + return `Titre de la salle mis à jour avec succès.`; + } catch (error) { + console.log("Error details: ", error); + if (axios.isAxiosError(error)) { + const err = error as AxiosError; + const data = err.response?.data as { error: string } | undefined; + return data?.error || 'Erreur serveur inconnue lors de la mise à jour du titre.'; + } + return `Une erreur inattendue s'est produite.`; + } + } + // Images Route /** @@ -1153,4 +1168,4 @@ class ApiService { } const apiService = new ApiService(); -export default apiService; +export default apiService; \ No newline at end of file