From d2cafe30c333b355adcdf68835340e53f0345ae6 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:16:48 +1100 Subject: [PATCH 01/17] =?UTF-8?q?=F0=9F=90=B3=20add=20dockerfile,=20remove?= =?UTF-8?q?=20duplicate=20npm=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/Dockerfile | 25 +++++++++++++++++++++++++ backend/package.json | 3 +-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 backend/Dockerfile diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..620bff8 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,25 @@ +FROM node:22.10.0-alpine AS base +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +RUN corepack enable + +COPY . /yellowshirt-backend +WORKDIR /yellowshirt-backend + +FROM base AS prod-deps +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile + +ENV NODE_ENV=production +ENV FRONTEND_LOCAL=https://yellowshirt.xyz +#ENV SESSION_SECRET=... + +FROM base AS build +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile +RUN pnpm run build + +FROM base +COPY --from=prod-deps /yellowshirt-backend/node_modules /yellowshirt-backend/node_modules +COPY --from=build /yellowshirt-backend/dist /yellowshirt-backend/dist + +EXPOSE 3000 +CMD ["pnpm", "start"] diff --git a/backend/package.json b/backend/package.json index 0ce85a5..b229198 100644 --- a/backend/package.json +++ b/backend/package.json @@ -27,8 +27,7 @@ "dev": "tsx watch ./src/index.ts", "build": "tsc --build", "start_env": "node --env-file=.env dist/src/index.js", - "start": "node dist/src/index.js", - "production": "node dist/src/index.js" + "start": "node dist/src/index.js" }, "prettier": { "singleQuote": false, From 9a69358ecf96edd275824a89ab5be6dc452f3ed6 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:34:51 +1100 Subject: [PATCH 02/17] =?UTF-8?q?=F0=9F=90=B3=20add=20relevant=20tsconfig?= =?UTF-8?q?=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/Dockerfile | 3 +++ backend/tsconfig.app.json | 27 +++++++++++++++++++++++++++ backend/tsconfig.json | 11 +++++++++++ backend/tsconfig.node.json | 13 +++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 backend/tsconfig.app.json create mode 100644 backend/tsconfig.json create mode 100644 backend/tsconfig.node.json diff --git a/backend/Dockerfile b/backend/Dockerfile index 620bff8..78fe69f 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -15,6 +15,9 @@ ENV FRONTEND_LOCAL=https://yellowshirt.xyz FROM base AS build RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile + +RUN ls -l + RUN pnpm run build FROM base diff --git a/backend/tsconfig.app.json b/backend/tsconfig.app.json new file mode 100644 index 0000000..d739292 --- /dev/null +++ b/backend/tsconfig.app.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "composite": true, + "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", + "target": "ES2020", + "useDefineForClassFields": true, + "lib": ["ES2020", "DOM", "DOM.Iterable"], + "module": "ESNext", + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "resolveJsonModule": true, + "isolatedModules": true, + "moduleDetection": "force", + "noEmit": true, + "jsx": "react-jsx", + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true + }, + "include": ["src"] +} diff --git a/backend/tsconfig.json b/backend/tsconfig.json new file mode 100644 index 0000000..ea9d0cd --- /dev/null +++ b/backend/tsconfig.json @@ -0,0 +1,11 @@ +{ + "files": [], + "references": [ + { + "path": "./tsconfig.app.json" + }, + { + "path": "./tsconfig.node.json" + } + ] +} diff --git a/backend/tsconfig.node.json b/backend/tsconfig.node.json new file mode 100644 index 0000000..3afdd6e --- /dev/null +++ b/backend/tsconfig.node.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "composite": true, + "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo", + "skipLibCheck": true, + "module": "ESNext", + "moduleResolution": "bundler", + "allowSyntheticDefaultImports": true, + "strict": true, + "noEmit": true + }, + "include": ["vite.config.ts"] +} From 68f901eb0ebe6f60ad473164277a333b6e794d7d Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:41:01 +1100 Subject: [PATCH 03/17] =?UTF-8?q?=F0=9F=AB=82=20add=20relevant=20type=20de?= =?UTF-8?q?pendencies,=20clarify=20some=20backend=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 + backend/pnpm-lock.yaml | 20 ++++++ backend/src/index.ts | 120 +++++++++++++++++++++--------------- backend/src/requestTypes.ts | 2 - 4 files changed, 93 insertions(+), 51 deletions(-) diff --git a/backend/package.json b/backend/package.json index b229198..aed59af 100644 --- a/backend/package.json +++ b/backend/package.json @@ -14,6 +14,8 @@ "fs": "^0.0.1-security" }, "devDependencies": { + "@types/bcrypt": "^5.0.2", + "@types/cors": "^2.8.17", "@types/express": "^4.17.21", "@types/express-session": "^1.18.0", "@types/node": "^20.14.9", diff --git a/backend/pnpm-lock.yaml b/backend/pnpm-lock.yaml index 6123144..277857d 100644 --- a/backend/pnpm-lock.yaml +++ b/backend/pnpm-lock.yaml @@ -27,6 +27,12 @@ importers: specifier: ^0.0.1-security version: 0.0.1-security devDependencies: + '@types/bcrypt': + specifier: ^5.0.2 + version: 5.0.2 + '@types/cors': + specifier: ^2.8.17 + version: 2.8.17 '@types/express': specifier: ^4.17.21 version: 4.17.21 @@ -448,12 +454,18 @@ packages: '@tsconfig/node16@1.0.4': resolution: {integrity: sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==} + '@types/bcrypt@5.0.2': + resolution: {integrity: sha512-6atioO8Y75fNcbmj0G7UjI9lXN2pQ/IGJ2FWT4a/btd0Lk9lQalHLKhkgKVZ3r+spnmWUKfbMi1GEe9wyHQfNQ==} + '@types/body-parser@1.19.5': resolution: {integrity: sha512-fB3Zu92ucau0iQ0JMCFQE7b/dv8Ot07NI3KaZIkIUNXq82k4eBAqUaneXfleGY9JWskeS9y+u0nXMyspcuQrCg==} '@types/connect@3.4.38': resolution: {integrity: sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==} + '@types/cors@2.8.17': + resolution: {integrity: sha512-8CGDvrBj1zgo2qE+oS3pOCyYNqCPryMWY2bGfwA0dcfopWGgxs+78df0Rs3rc9THP4JkOhLsAa+15VdpAqkcUA==} + '@types/express-serve-static-core@4.19.5': resolution: {integrity: sha512-y6W03tvrACO72aijJ5uF02FRq5cgDR9lUxddQ8vyF+GvmjJQqbzDcJngEjURc+ZsG31VI3hODNZJ2URj86pzmg==} @@ -1578,6 +1590,10 @@ snapshots: '@tsconfig/node16@1.0.4': {} + '@types/bcrypt@5.0.2': + dependencies: + '@types/node': 20.14.9 + '@types/body-parser@1.19.5': dependencies: '@types/connect': 3.4.38 @@ -1587,6 +1603,10 @@ snapshots: dependencies: '@types/node': 20.14.9 + '@types/cors@2.8.17': + dependencies: + '@types/node': 20.14.9 + '@types/express-serve-static-core@4.19.5': dependencies: '@types/node': 20.14.9 diff --git a/backend/src/index.ts b/backend/src/index.ts index fe1d9b1..66096d6 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -6,7 +6,15 @@ import { LoginBody, LeaderboardQuery, } from "./requestTypes"; -import { SessionStorage, User, LoginErrors, Level, Gamemode, Game, Hotspot } from "./interfaces"; +import { + SessionStorage, + User, + LoginErrors, + Level, + Gamemode, + Game, + Hotspot, +} from "./interfaces"; import bcrypt from "bcrypt"; import { collection, @@ -194,7 +202,7 @@ app.post("/login", async (req: TypedRequest, res: Response) => { bcrypt.compare( saltedPassword, details.docs[0].data().password, - async (err: Error | null, result: boolean) => { + async (err: Error | undefined, result: boolean) => { if (err) { return res.status(500).send("Error processing password"); } @@ -234,7 +242,7 @@ app.get( ) => { const getDoc = await getDocs(collection(db, "levels")); - const { roundCount, gameMode } = req.query; + const { roundCount } = req.query; // array of level IDs const docIds = getDoc.docs.map((doc) => doc.id); @@ -252,8 +260,12 @@ app.get( app.post( "/endGame", async ( - req: TypedRequest<{ gameMode: Gamemode, levels: Level["id"][], score: number }>, - res: Response + req: TypedRequest<{ + gameMode: Gamemode; + levels: Level["id"][]; + score: number; + }>, + res: Response, ) => { const { gameMode, levels, score } = req.body; @@ -268,64 +280,70 @@ app.post( gamemode: gameMode, levels: levels, score: score, - userid: userId + userid: userId, }; addDoc(collection(db, "games"), game); - res.status(200).send("Game Ended Successfully"); - } -) - -app.get("/level", async (req: TypedRequestQuery<{ levelId: string }>, res: Response) => { - const levelId = req.query.levelId; - const docRef = doc(db, "levels", levelId); - const docSnap = await getDoc(docRef); + }, +); - if (!docSnap.exists()) { - res.status(404).json({ error: "Level not found" }); - return; - } +app.get( + "/level", + async (req: TypedRequestQuery<{ levelId: string }>, res: Response) => { + const levelId = req.query.levelId; + const docRef = doc(db, "levels", levelId); + const docSnap = await getDoc(docRef); + + if (!docSnap.exists()) { + res.status(404).json({ error: "Level not found" }); + return; + } - const levelData = docSnap.data(); - - const floorMap = { - LG: 0, - G: 1, - L1: 2, - L2: 3, - L3: 4, - L4: 5, - L5: 6, - L6: 7, - }; + const levelData = docSnap.data(); + + const floorMap = { + LG: 0, + G: 1, + L1: 2, + L2: 3, + L3: 4, + L4: 5, + L5: 6, + L6: 7, + }; - const hotspots: Hotspot[] = [] - levelData.hotspots.forEach((h) => { - hotspots.push( - { + const hotspots: Hotspot[] = []; + levelData.hotspots.forEach((h: Hotspot) => { + hotspots.push({ levelId: h.levelId, pitch: h.pitch, yaw: h.yaw, targetPitch: h.targetPitch, targetYaw: h.targetYaw, - } - ) - }) + }); + }); + const level: Level = { + photoLink: levelData.panorama, + locationName: levelData.title, + latitude: levelData.latitude, + longitude: levelData.longitude, + zPosition: undefined, + hotspots: hotspots, + }; - const level: Level = { - photoLink: levelData.panorama, - locationName: levelData.title, - latitude: levelData.latitude, - longitude: levelData.longitude, - zPosition: floorMap[levelData.floor] ?? 1, // if floor is undefined, then location must be G (eg. a lawn) - hotspots: hotspots, - } + // if floor is undefined, then location must be G (eg. a lawn) + if (levelData.floor in floorMap) { + level.zPosition = levelData.floor; + } else { + level.zPosition = 1; + } - res.status(200).json(level); -}); + res.status(200).json(level); + }, +); app.get( "/leaderboard/data", @@ -354,7 +372,7 @@ app.get( } }); - const ids = Object.values(highestScores).map(user => user.id); + const ids = Object.values(highestScores).map((user) => user.id); if (ids.length == 0) { return res.status(400).send("error"); @@ -376,7 +394,11 @@ app.get( for (let i = start; i < end; i++) { // if username is undefined, don't add to leaderboard let username: string; - if (!(username = await getUsername(queryScoreSnapshot.docs[i].data().userid))) { + if ( + !(username = await getUsername( + queryScoreSnapshot.docs[i].data().userid, + )) + ) { return res.status(500).send("invalid userId in game database"); } const dataEntry: ScoreEntry = { diff --git a/backend/src/requestTypes.ts b/backend/src/requestTypes.ts index aef3cd7..44bac4d 100644 --- a/backend/src/requestTypes.ts +++ b/backend/src/requestTypes.ts @@ -1,5 +1,3 @@ -import { Gamemode } from "./interfaces"; - export interface TypedRequest extends Express.Request { body: T; } From a68063bcef58bdd648be03ffc27521d28beb78b2 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:44:06 +1100 Subject: [PATCH 04/17] =?UTF-8?q?=F0=9F=93=83=20fix=20tsconfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/tsconfig.app.json | 27 --------------------------- backend/tsconfig.json | 34 +++++++++++++++++++++++++--------- backend/tsconfig.node.json | 13 ------------- 3 files changed, 25 insertions(+), 49 deletions(-) delete mode 100644 backend/tsconfig.app.json delete mode 100644 backend/tsconfig.node.json diff --git a/backend/tsconfig.app.json b/backend/tsconfig.app.json deleted file mode 100644 index d739292..0000000 --- a/backend/tsconfig.app.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "compilerOptions": { - "composite": true, - "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", - "target": "ES2020", - "useDefineForClassFields": true, - "lib": ["ES2020", "DOM", "DOM.Iterable"], - "module": "ESNext", - "skipLibCheck": true, - - /* Bundler mode */ - "moduleResolution": "bundler", - "allowImportingTsExtensions": true, - "resolveJsonModule": true, - "isolatedModules": true, - "moduleDetection": "force", - "noEmit": true, - "jsx": "react-jsx", - - /* Linting */ - "strict": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noFallthroughCasesInSwitch": true - }, - "include": ["src"] -} diff --git a/backend/tsconfig.json b/backend/tsconfig.json index ea9d0cd..d739292 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -1,11 +1,27 @@ { - "files": [], - "references": [ - { - "path": "./tsconfig.app.json" - }, - { - "path": "./tsconfig.node.json" - } - ] + "compilerOptions": { + "composite": true, + "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", + "target": "ES2020", + "useDefineForClassFields": true, + "lib": ["ES2020", "DOM", "DOM.Iterable"], + "module": "ESNext", + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "resolveJsonModule": true, + "isolatedModules": true, + "moduleDetection": "force", + "noEmit": true, + "jsx": "react-jsx", + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true + }, + "include": ["src"] } diff --git a/backend/tsconfig.node.json b/backend/tsconfig.node.json deleted file mode 100644 index 3afdd6e..0000000 --- a/backend/tsconfig.node.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "compilerOptions": { - "composite": true, - "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo", - "skipLibCheck": true, - "module": "ESNext", - "moduleResolution": "bundler", - "allowSyntheticDefaultImports": true, - "strict": true, - "noEmit": true - }, - "include": ["vite.config.ts"] -} From 377f0ea204067ac8c565abd51c305d2cda234682 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:52:40 +1100 Subject: [PATCH 05/17] =?UTF-8?q?=F0=9F=93=83=20fix=20tsconfig=20(again..)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/tsconfig.json | 130 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 23 deletions(-) diff --git a/backend/tsconfig.json b/backend/tsconfig.json index d739292..4a01831 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -1,27 +1,111 @@ { "compilerOptions": { - "composite": true, - "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", - "target": "ES2020", - "useDefineForClassFields": true, - "lib": ["ES2020", "DOM", "DOM.Iterable"], - "module": "ESNext", - "skipLibCheck": true, - - /* Bundler mode */ - "moduleResolution": "bundler", - "allowImportingTsExtensions": true, - "resolveJsonModule": true, - "isolatedModules": true, - "moduleDetection": "force", - "noEmit": true, - "jsx": "react-jsx", - - /* Linting */ - "strict": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noFallthroughCasesInSwitch": true + /* Visit https://aka.ms/tsconfig to read more about this file */ + + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "esnext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + + /* Modules */ + "module": "commonjs" /* Specify what module code is generated. */, + // "rootDir": "./", /* Specify the root folder within your source files. */ + "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */, + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ + // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ + // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ + // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./dist" /* Specify an output folder for all emitted files. */, + // "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ + // "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, + + /* Type Checking */ + "strict": true /* Enable all strict type-checking options. */, + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ }, - "include": ["src"] + "exclude": ["node_modules", "dist"], + "include": ["src/**/*.ts", "tests/**/*.spec.ts"], + "files": ["src/index.ts"] } From f6a007a2570c515d00d079790f5bc52ee5feaf09 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:06:11 +1100 Subject: [PATCH 06/17] =?UTF-8?q?=F0=9F=AA=B0=20deploy=20to=20fly.io,=20ad?= =?UTF-8?q?d=20ping=20test=20route?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/fly.toml | 22 ++++++++++++++++++++++ backend/src/index.ts | 4 ++++ 2 files changed, 26 insertions(+) create mode 100644 backend/fly.toml diff --git a/backend/fly.toml b/backend/fly.toml new file mode 100644 index 0000000..473226a --- /dev/null +++ b/backend/fly.toml @@ -0,0 +1,22 @@ +# fly.toml app configuration file generated for yellowshirt-backend on 2024-10-18T15:00:04+11:00 +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. +# + +app = 'yellowshirt-backend' +primary_region = 'syd' + +[build] + +[http_service] + internal_port = 3000 + force_https = true + auto_stop_machines = 'stop' + auto_start_machines = true + min_machines_running = 0 + processes = ['app'] + +[[vm]] + memory = '1gb' + cpu_kind = 'shared' + cpus = 1 diff --git a/backend/src/index.ts b/backend/src/index.ts index 66096d6..2df5bf6 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -289,6 +289,10 @@ app.post( }, ); +app.get("/ping", (req: Request, res: Response) => { + res.status(200).send("pong"); +}); + app.get( "/level", async (req: TypedRequestQuery<{ levelId: string }>, res: Response) => { From a657cfa75da93a64abd1460e81d565b4f5ccf827 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:27:50 +1100 Subject: [PATCH 07/17] =?UTF-8?q?=F0=9F=8E=AF=20update=20/api=20proxy=20ta?= =?UTF-8?q?rget=20->=20fly=20endpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/vite.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index c005b5d..3de7792 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -7,7 +7,7 @@ export default defineConfig({ server: { proxy: { "/api": { - target: "http://localhost:3000", + target: "https://yellowshirt-backend.fly.dev", changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ""), }, From e6c316264789a9a8eaec14934b61d4c90bf41e3d Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:33:51 +1100 Subject: [PATCH 08/17] =?UTF-8?q?=F0=9F=9B=AB=20fix=20npm=20start=20script?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/package.json b/backend/package.json index aed59af..5fadeec 100644 --- a/backend/package.json +++ b/backend/package.json @@ -28,8 +28,8 @@ "dev_env": "tsx watch --env-file=.env ./src/index.ts", "dev": "tsx watch ./src/index.ts", "build": "tsc --build", - "start_env": "node --env-file=.env dist/src/index.js", - "start": "node dist/src/index.js" + "start_env": "node --env-file=.env dist/index.js", + "start": "node dist/index.js" }, "prettier": { "singleQuote": false, From 33695378c09f0ed58f8329a0d4c4cc4041d41285 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:21:56 +1100 Subject: [PATCH 09/17] =?UTF-8?q?=F0=9F=94=90=20update=20cors,=20permit=20?= =?UTF-8?q?multiple=20origins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/Dockerfile | 2 +- backend/src/index.ts | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/backend/Dockerfile b/backend/Dockerfile index 78fe69f..54c26b4 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -10,7 +10,7 @@ FROM base AS prod-deps RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile ENV NODE_ENV=production -ENV FRONTEND_LOCAL=https://yellowshirt.xyz +ENV ALLOWED_ORIGINS=/yellowshirt\.xyz$/,/trainee-woodelf-24t2\.pages\.dev$/ #ENV SESSION_SECRET=... FROM base AS build diff --git a/backend/src/index.ts b/backend/src/index.ts index 2df5bf6..b4a853f 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -92,11 +92,24 @@ const sessionIdToUserId = async ( }; const app = express(); +let allowedOrigins: RegExp[]; +if (process.env.ALLOWED_ORIGINS) { + allowedOrigins = process.env.ALLOWED_ORIGINS.split(",").map( + (origin) => new RegExp(origin), + ); +} else { + allowedOrigins = []; +} app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.use( cors({ - origin: process.env.FRONTEND_LOCAL as string, + origin: (origin, callback) => { + if (!origin) return callback(null, true); + allowedOrigins.some((r) => r.test(origin as string)) + ? callback(null, true) + : callback(new Error(`Origin ${origin} not in ALLOWED_ORIGINS`)); + }, credentials: true, optionsSuccessStatus: 200, }), From 263ec1ef4a8792417a1d65c24a51fc6eb5b3aec8 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:27:20 +1100 Subject: [PATCH 10/17] =?UTF-8?q?=F0=9F=9A=81=20update=20vite=20proxy=20co?= =?UTF-8?q?nfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/vite.config.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 3de7792..17719b4 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -9,6 +9,7 @@ export default defineConfig({ "/api": { target: "https://yellowshirt-backend.fly.dev", changeOrigin: true, + secure: false, rewrite: (path) => path.replace(/^\/api/, ""), }, }, From bb2c3656e387a6072ab5177095b11757f4951d60 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:24:55 +1100 Subject: [PATCH 11/17] =?UTF-8?q?=F0=9F=94=A5=20change=20how=20allowed=5Fo?= =?UTF-8?q?rigins=20listed,=20update=20readme,=20many=20things?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 +++++--- backend/Dockerfile | 9 ++++- backend/fly.toml | 3 ++ backend/src/index.ts | 78 +++++++++++++++++++++++------------------ frontend/vite.config.ts | 4 +-- 5 files changed, 66 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 25fdd39..c0c8fc3 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,15 @@ by alyssa, ben, chris, lachlan, and osvaldo 1. `cd backend && pnpm i` 2. set the following value in `backend/.env`: + ```bash SESSION_SECRET= NODE_ENV= +ALLOWED_ORIGINS=/commaseparated/,/regexes/ ``` -NODE_ENV is usually either 'development' or 'production' -3. `cd ../frontend && pnpm i` -4. set the following values in `frontend/.env`: + +NODE_ENV is usually either 'development' or 'production' 3. `cd ../frontend && pnpm i` 4. set the following values in `frontend/.env`: + ```bash VITE_FIREBASE_API_KEY= VITE_FIREBASE_AUTH_DOMAIN= @@ -23,12 +25,15 @@ VITE_FIREBASE_STORAGE_BUCKET= VITE_FIREBASE_MESSAGING_SENDER_ID= VITE_FIREBASE_APP_ID= ``` -5. *optionally*, create a .env file at the root of the repository which contains a cloudflare deploy book endpoint. + +5. _optionally_, create a .env file at the root of the repository which contains a cloudflare deploy book endpoint. + ``` echo "https://api.cloudflare.com/client/v4/pages/webhooks/deploy_hooks/ENDPOINT_GOES_HERE" > .env ``` + you can now use the `rebuild` script in the root of the repository to initiate manual deployments without having to push changes to `main`. ## level editor -you can create your own yellowshirt levels (or, more precisely, *maps* composed of *levels*) using the [panoproc](https://github.com/lachlanshoesmith/panoproc) level editor. installation and usage instructions can be found in that repository's readme. +you can create your own yellowshirt levels (or, more precisely, _maps_ composed of _levels_) using the [panoproc](https://github.com/lachlanshoesmith/panoproc) level editor. installation and usage instructions can be found in that repository's readme. diff --git a/backend/Dockerfile b/backend/Dockerfile index 54c26b4..a5c7b56 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -10,7 +10,14 @@ FROM base AS prod-deps RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile ENV NODE_ENV=production -ENV ALLOWED_ORIGINS=/yellowshirt\.xyz$/,/trainee-woodelf-24t2\.pages\.dev$/ + +# envs to set for runtime +# if using fly, env vars can be specified in fly.toml + +#ENV ALLOWED_ORIGINS=/yellowshirt\.xyz$/,/trainee-woodelf-24t2\.pages\.dev$/,/yellowshirt-backend\.fly\.dev$/ + +# ⚠️ SESSION_SECRET should be, well, a secret! +# if using fly, use fly secrets #ENV SESSION_SECRET=... FROM base AS build diff --git a/backend/fly.toml b/backend/fly.toml index 473226a..5056cf0 100644 --- a/backend/fly.toml +++ b/backend/fly.toml @@ -20,3 +20,6 @@ primary_region = 'syd' memory = '1gb' cpu_kind = 'shared' cpus = 1 + +[env] + ALLOWED_ORIGINS = 'yellowshirt\.xyz$,trainee-woodelf-24t2\.pages\.dev$,yellowshirt-backend\.fly\.dev$' diff --git a/backend/src/index.ts b/backend/src/index.ts index b4a853f..d623fd2 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -1,4 +1,4 @@ -import express, { Response, Request } from "express"; +import express, { Response, Request, Router } from "express"; import session from "express-session"; import { TypedRequest, @@ -37,6 +37,7 @@ const EXPRESS_PORT = 3000; const games = collection(db, "games"); const sessions = collection(db, "sessions"); const users = collection(db, "users"); +const router = express.Router(); const session_auth = async (sessionId: string) => { const sessionData = query(sessions, where("sessionId", "==", sessionId)); @@ -128,8 +129,9 @@ app.use( resave: false, }), ); +app.use("/api", router); -app.get("/user", async (req: Request, res: Response) => { +router.get("/user", async (req: Request, res: Response) => { // if sessionId is in the DB and not expired --> session is a user. // if not --> session is a guest. const sessionId = req.sessionID; @@ -161,41 +163,47 @@ app.listen(EXPRESS_PORT, () => { ); }); -app.post("/register", async (req: TypedRequest, res: Response) => { - const { username, password } = req.body; - const querySnapshot = await getDocs(users); +router.post( + "/register", + async (req: TypedRequest, res: Response) => { + const { username, password } = req.body; + const querySnapshot = await getDocs(users); - const errorCheck: LoginErrors = { - usernameNotFound: true, - }; + const errorCheck: LoginErrors = { + usernameNotFound: true, + }; - if (querySnapshot.docs.some((doc) => doc.data().username === username)) { - errorCheck.usernameNotFound = false; - return res.status(400).json(errorCheck); - } + if (querySnapshot.docs.some((doc) => doc.data().username === username)) { + errorCheck.usernameNotFound = false; + return res.status(400).json(errorCheck); + } - const salt: string = crypto.randomBytes(128).toString("base64"); - const saltedPassword: string = password.concat(salt); + const salt: string = crypto.randomBytes(128).toString("base64"); + const saltedPassword: string = password.concat(salt); - const saltRounds: number = 10; - const hashedPassword: string = await bcrypt.hash(saltedPassword, saltRounds); + const saltRounds: number = 10; + const hashedPassword: string = await bcrypt.hash( + saltedPassword, + saltRounds, + ); - const newUser: User = { - username, - password: hashedPassword, - salt: salt, - dateJoined: new Date(), - highScore: 0, - cumulativeScore: 0, - shirts: 0, - }; + const newUser: User = { + username, + password: hashedPassword, + salt: salt, + dateJoined: new Date(), + highScore: 0, + cumulativeScore: 0, + shirts: 0, + }; - await addDoc(users, newUser); + await addDoc(users, newUser); - return res.status(201).send("User Successfully Registered"); -}); + return res.status(201).send("User Successfully Registered"); + }, +); -app.post("/login", async (req: TypedRequest, res: Response) => { +router.post("/login", async (req: TypedRequest, res: Response) => { const { username, password } = req.body; const loginDetails = query(users, where("username", "==", username)); const details = await getDocs(loginDetails); @@ -247,7 +255,7 @@ app.post("/login", async (req: TypedRequest, res: Response) => { }); // curl -H 'Content-Type: application/json' -d '{ "email": "ben", "color": "pink"}' -X POST http://localhost:3000/subscribe -app.get( +router.get( "/startGame", async ( req: TypedRequestQuery<{ roundCount: number; gameMode: Gamemode }>, @@ -270,7 +278,7 @@ app.get( }, ); -app.post( +router.post( "/endGame", async ( req: TypedRequest<{ @@ -302,11 +310,11 @@ app.post( }, ); -app.get("/ping", (req: Request, res: Response) => { +router.get("/ping", (req: Request, res: Response) => { res.status(200).send("pong"); }); -app.get( +router.get( "/level", async (req: TypedRequestQuery<{ levelId: string }>, res: Response) => { const levelId = req.query.levelId; @@ -362,7 +370,7 @@ app.get( }, ); -app.get( +router.get( "/leaderboard/data", async (req: TypedRequestQuery, res: Response) => { // const { pagenum, gamemode, increments } = req.query; @@ -433,7 +441,7 @@ app.get( }, ); -app.post("/logout", async (req: Request, res: Response) => { +router.post("/logout", async (req: Request, res: Response) => { const sessionId = req.sessionID; if (!(await session_remove(sessionId))) { console.log("Not logged in"); diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 17719b4..367118c 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -7,9 +7,9 @@ export default defineConfig({ server: { proxy: { "/api": { - target: "https://yellowshirt-backend.fly.dev", + target: "https://yellowshirt-backend.fly.dev/api", changeOrigin: true, - secure: false, + secure: false, rewrite: (path) => path.replace(/^\/api/, ""), }, }, From 1693773e6e6b20ef14d03c703c34a6da189b6b67 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:32:05 +1100 Subject: [PATCH 12/17] =?UTF-8?q?=F0=9F=8C=A9=20add=20cloudflare=20redirec?= =?UTF-8?q?ts=20file=20for=20backend?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/_redirects | 1 + 1 file changed, 1 insertion(+) create mode 100644 frontend/_redirects diff --git a/frontend/_redirects b/frontend/_redirects new file mode 100644 index 0000000..0819415 --- /dev/null +++ b/frontend/_redirects @@ -0,0 +1 @@ +/api/* https://yellowshirt-backend.fly.dev/:splat From 13c65f8b94eba940fb9a5286756e13ff17b2de48 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:34:44 +1100 Subject: [PATCH 13/17] =?UTF-8?q?=F0=9F=8C=A9=20move=20cloudflare=20redire?= =?UTF-8?q?cts=20file=20..=20mayb=20this=20works?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/{ => public}/_redirects | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename frontend/{ => public}/_redirects (100%) diff --git a/frontend/_redirects b/frontend/public/_redirects similarity index 100% rename from frontend/_redirects rename to frontend/public/_redirects From 5c4b511d1ad5912abc4ac4568d4e6aa85c8766a1 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Sat, 19 Oct 2024 00:58:50 +1100 Subject: [PATCH 14/17] =?UTF-8?q?=F0=9F=A4=AF=20remove=20/api=20express=20?= =?UTF-8?q?routing,=20remove=20console=20logs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/index.ts | 78 +++++++++---------- frontend/src/components/Forms/Login/Login.tsx | 4 +- .../components/Forms/Register/Register.tsx | 4 +- .../components/Leaderboard/Leaderboard.tsx | 6 +- frontend/src/components/Navbar/Navbar.tsx | 23 +++--- .../ProfileDropdown/ProfileDropdown.tsx | 16 ++-- .../src/pages/ProfilePage/ProfilePage.tsx | 2 - 7 files changed, 62 insertions(+), 71 deletions(-) diff --git a/backend/src/index.ts b/backend/src/index.ts index d623fd2..b4a853f 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -1,4 +1,4 @@ -import express, { Response, Request, Router } from "express"; +import express, { Response, Request } from "express"; import session from "express-session"; import { TypedRequest, @@ -37,7 +37,6 @@ const EXPRESS_PORT = 3000; const games = collection(db, "games"); const sessions = collection(db, "sessions"); const users = collection(db, "users"); -const router = express.Router(); const session_auth = async (sessionId: string) => { const sessionData = query(sessions, where("sessionId", "==", sessionId)); @@ -129,9 +128,8 @@ app.use( resave: false, }), ); -app.use("/api", router); -router.get("/user", async (req: Request, res: Response) => { +app.get("/user", async (req: Request, res: Response) => { // if sessionId is in the DB and not expired --> session is a user. // if not --> session is a guest. const sessionId = req.sessionID; @@ -163,47 +161,41 @@ app.listen(EXPRESS_PORT, () => { ); }); -router.post( - "/register", - async (req: TypedRequest, res: Response) => { - const { username, password } = req.body; - const querySnapshot = await getDocs(users); +app.post("/register", async (req: TypedRequest, res: Response) => { + const { username, password } = req.body; + const querySnapshot = await getDocs(users); - const errorCheck: LoginErrors = { - usernameNotFound: true, - }; + const errorCheck: LoginErrors = { + usernameNotFound: true, + }; - if (querySnapshot.docs.some((doc) => doc.data().username === username)) { - errorCheck.usernameNotFound = false; - return res.status(400).json(errorCheck); - } + if (querySnapshot.docs.some((doc) => doc.data().username === username)) { + errorCheck.usernameNotFound = false; + return res.status(400).json(errorCheck); + } - const salt: string = crypto.randomBytes(128).toString("base64"); - const saltedPassword: string = password.concat(salt); + const salt: string = crypto.randomBytes(128).toString("base64"); + const saltedPassword: string = password.concat(salt); - const saltRounds: number = 10; - const hashedPassword: string = await bcrypt.hash( - saltedPassword, - saltRounds, - ); + const saltRounds: number = 10; + const hashedPassword: string = await bcrypt.hash(saltedPassword, saltRounds); - const newUser: User = { - username, - password: hashedPassword, - salt: salt, - dateJoined: new Date(), - highScore: 0, - cumulativeScore: 0, - shirts: 0, - }; + const newUser: User = { + username, + password: hashedPassword, + salt: salt, + dateJoined: new Date(), + highScore: 0, + cumulativeScore: 0, + shirts: 0, + }; - await addDoc(users, newUser); + await addDoc(users, newUser); - return res.status(201).send("User Successfully Registered"); - }, -); + return res.status(201).send("User Successfully Registered"); +}); -router.post("/login", async (req: TypedRequest, res: Response) => { +app.post("/login", async (req: TypedRequest, res: Response) => { const { username, password } = req.body; const loginDetails = query(users, where("username", "==", username)); const details = await getDocs(loginDetails); @@ -255,7 +247,7 @@ router.post("/login", async (req: TypedRequest, res: Response) => { }); // curl -H 'Content-Type: application/json' -d '{ "email": "ben", "color": "pink"}' -X POST http://localhost:3000/subscribe -router.get( +app.get( "/startGame", async ( req: TypedRequestQuery<{ roundCount: number; gameMode: Gamemode }>, @@ -278,7 +270,7 @@ router.get( }, ); -router.post( +app.post( "/endGame", async ( req: TypedRequest<{ @@ -310,11 +302,11 @@ router.post( }, ); -router.get("/ping", (req: Request, res: Response) => { +app.get("/ping", (req: Request, res: Response) => { res.status(200).send("pong"); }); -router.get( +app.get( "/level", async (req: TypedRequestQuery<{ levelId: string }>, res: Response) => { const levelId = req.query.levelId; @@ -370,7 +362,7 @@ router.get( }, ); -router.get( +app.get( "/leaderboard/data", async (req: TypedRequestQuery, res: Response) => { // const { pagenum, gamemode, increments } = req.query; @@ -441,7 +433,7 @@ router.get( }, ); -router.post("/logout", async (req: Request, res: Response) => { +app.post("/logout", async (req: Request, res: Response) => { const sessionId = req.sessionID; if (!(await session_remove(sessionId))) { console.log("Not logged in"); diff --git a/frontend/src/components/Forms/Login/Login.tsx b/frontend/src/components/Forms/Login/Login.tsx index 0347bbe..d0b753a 100644 --- a/frontend/src/components/Forms/Login/Login.tsx +++ b/frontend/src/components/Forms/Login/Login.tsx @@ -77,8 +77,8 @@ function LoginPage() { return setPasswordMatch(false); } } - } catch (err) { - console.log("Error: ", err); + } catch (e) { + console.error(e); } finally { setIsProcessing(false); } diff --git a/frontend/src/components/Forms/Register/Register.tsx b/frontend/src/components/Forms/Register/Register.tsx index 2ec784b..0d6e77b 100644 --- a/frontend/src/components/Forms/Register/Register.tsx +++ b/frontend/src/components/Forms/Register/Register.tsx @@ -108,8 +108,8 @@ function Register() { const errorCheck = await resp.json(); if (!errorCheck.usernameNotFound) setUsernameAvailable(false); } - } catch (err) { - console.log("Error: ", err); + } catch (e) { + console.error(e); } finally { setIsProcessing(false); } diff --git a/frontend/src/components/Leaderboard/Leaderboard.tsx b/frontend/src/components/Leaderboard/Leaderboard.tsx index 64f1106..0f4ea1e 100644 --- a/frontend/src/components/Leaderboard/Leaderboard.tsx +++ b/frontend/src/components/Leaderboard/Leaderboard.tsx @@ -60,10 +60,10 @@ function Leaderboard() { setLeaderboardPage(1); setPageCount(1); } else { - console.error("Error: ", resp.status); + console.error(resp.status); } - } catch (err) { - console.log("error ", err); + } catch (e) { + console.error(e); } }; diff --git a/frontend/src/components/Navbar/Navbar.tsx b/frontend/src/components/Navbar/Navbar.tsx index fcf083e..c5b7670 100644 --- a/frontend/src/components/Navbar/Navbar.tsx +++ b/frontend/src/components/Navbar/Navbar.tsx @@ -5,7 +5,7 @@ import ProfileIcon from "../ProfileIcon/ProfileIcon"; import ProfileDropdown from "../ProfileDropdown/ProfileDropdown"; import { useEffect, useState } from "react"; import Credits from "../Credits/Credits"; -import Help from "../Help/Help" +import Help from "../Help/Help"; import { useNavigate } from "react-router-dom"; import { Menu, X } from "lucide-react"; import Sheet from "../Sheet/Sheet"; @@ -16,7 +16,7 @@ function Navbar() { const [showHelp, setShowHelp] = useState(false); const [showDropDown, setShowDropDown] = useState(false); const [showProfileDropDown, setShowProfileDropDown] = useState(false); - const [username, setUsername] = useState('') + const [username, setUsername] = useState(""); const navigate = useNavigate(); const toggleCredits = () => { @@ -54,17 +54,18 @@ function Navbar() { }); if (resp.ok) { - return resp.json().then((r) => {setUsername(r.username)}) + return resp.json().then((r) => { + setUsername(r.username); + }); } else { - console.log(resp); + console.error(resp); } } catch (e) { - console.log("Error: ", e); + console.error(e); } - } - getUsername() - .catch(console.error) - }, []) + }; + getUsername().catch(console.error); + }, []); return (
@@ -129,9 +130,7 @@ function Navbar() { [classes.slide_out]: !showDropDown, })} > - + diff --git a/frontend/src/components/ProfileDropdown/ProfileDropdown.tsx b/frontend/src/components/ProfileDropdown/ProfileDropdown.tsx index 9aba2fe..d52b52f 100644 --- a/frontend/src/components/ProfileDropdown/ProfileDropdown.tsx +++ b/frontend/src/components/ProfileDropdown/ProfileDropdown.tsx @@ -6,7 +6,6 @@ import { useNavigate } from "react-router-dom"; function ProfileDropdown(props: { username: string }) { const navigate = useNavigate(); - const handleClick = async () => { try { const resp = await fetch("/api/logout", { @@ -18,14 +17,12 @@ function ProfileDropdown(props: { username: string }) { }); if (resp.ok) { - // here for testing change later - console.log("Log out success"); navigate("/login", { replace: true }); } else { - console.log(resp); + console.error(resp); } } catch (e) { - console.log("Error: ", e); + console.error(e); } }; @@ -33,8 +30,13 @@ function ProfileDropdown(props: { username: string }) {

{props.username}

- {navigate('/profile')}}> - {/* {}}> */} + { + navigate("/profile"); + }} + > + {/* {}}> */} diff --git a/frontend/src/pages/ProfilePage/ProfilePage.tsx b/frontend/src/pages/ProfilePage/ProfilePage.tsx index a6843be..e650a12 100644 --- a/frontend/src/pages/ProfilePage/ProfilePage.tsx +++ b/frontend/src/pages/ProfilePage/ProfilePage.tsx @@ -14,7 +14,6 @@ const ProfilePage = () => { async function getData() { let dataPromise = await fetch("/api/user", { method: "GET" }); let dataJson = await dataPromise.json(); - console.log(dataJson); setUsername(dataJson.username); setHighScore(dataJson.highScore); setCumulativeScore(dataJson.cumulativeScore); @@ -31,7 +30,6 @@ const ProfilePage = () => { let date2 = new Date(); let msDay = 1000 * 3600 * 24; // milliseconds per day let days = Math.round((date2.getTime() - date1.getTime()) / msDay); - console.log(days); setActiveDays(days); } From ddc6fbf49c9d2e9a665d89cb1a1e38e59a026589 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Sat, 19 Oct 2024 01:10:34 +1100 Subject: [PATCH 15/17] =?UTF-8?q?=E2=9D=8C=20remove=20some=20unnecessary?= =?UTF-8?q?=20error=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/Forms/Login/Login.tsx | 39 ++++++++--------- .../components/Forms/Register/Register.tsx | 31 ++++++-------- .../components/Leaderboard/Leaderboard.tsx | 42 ++++++++----------- frontend/src/components/Navbar/Navbar.tsx | 30 ++++++------- .../ProfileDropdown/ProfileDropdown.tsx | 24 ++++------- frontend/vite.config.ts | 3 +- 6 files changed, 70 insertions(+), 99 deletions(-) diff --git a/frontend/src/components/Forms/Login/Login.tsx b/frontend/src/components/Forms/Login/Login.tsx index d0b753a..6ce5abb 100644 --- a/frontend/src/components/Forms/Login/Login.tsx +++ b/frontend/src/components/Forms/Login/Login.tsx @@ -57,31 +57,26 @@ function LoginPage() { setIsProcessing(true); // formData.username = formData.username.trim(); - try { - const resp = await fetch("/api/login", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(formData), - credentials: "include", - }); + const resp = await fetch("/api/login", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(formData), + credentials: "include", + }); - if (resp.ok) { - navigate("/gamemodes"); - } else { - const errorCheck = await resp.json(); - if (errorCheck.usernameNotFound) { - return setUsernameFound(false); - } else if (errorCheck.passwordInvalid) { - return setPasswordMatch(false); - } + if (resp.ok) { + navigate("/gamemodes"); + } else { + const errorCheck = await resp.json(); + if (errorCheck.usernameNotFound) { + return setUsernameFound(false); + } else if (errorCheck.passwordInvalid) { + return setPasswordMatch(false); } - } catch (e) { - console.error(e); - } finally { - setIsProcessing(false); } + setIsProcessing(false); }; return ( diff --git a/frontend/src/components/Forms/Register/Register.tsx b/frontend/src/components/Forms/Register/Register.tsx index 0d6e77b..297966a 100644 --- a/frontend/src/components/Forms/Register/Register.tsx +++ b/frontend/src/components/Forms/Register/Register.tsx @@ -93,26 +93,21 @@ function Register() { if (isProcessing) return; setIsProcessing(true); - try { - const resp = await fetch("/api/register", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(formData), - }); + const resp = await fetch("/api/register", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(formData), + }); - if (resp.ok) { - navigate("/login", { replace: true }); - } else { - const errorCheck = await resp.json(); - if (!errorCheck.usernameNotFound) setUsernameAvailable(false); - } - } catch (e) { - console.error(e); - } finally { - setIsProcessing(false); + if (resp.ok) { + navigate("/login", { replace: true }); + } else { + const errorCheck = await resp.json(); + if (!errorCheck.usernameNotFound) setUsernameAvailable(false); } + setIsProcessing(false); }; return ( diff --git a/frontend/src/components/Leaderboard/Leaderboard.tsx b/frontend/src/components/Leaderboard/Leaderboard.tsx index 0f4ea1e..6e34e4b 100644 --- a/frontend/src/components/Leaderboard/Leaderboard.tsx +++ b/frontend/src/components/Leaderboard/Leaderboard.tsx @@ -37,33 +37,27 @@ function Leaderboard() { if (leaderboardPage != 0 && (pageNum <= 0 || pageNum > pageCount)) { return; } - try { - const resp = await fetch( - `/api/leaderboard/data?pagenum=${pageNum}&gamemode=${leaderboardType}&increments=6`, - { - method: "GET", - headers: { - "Content-Type": "application/json", - }, + const resp = await fetch( + `/api/leaderboard/data?pagenum=${pageNum}&gamemode=${leaderboardType}&increments=6`, + { + method: "GET", + headers: { + "Content-Type": "application/json", }, - ); + }, + ); - if (resp.status === 200) { - const data = (await resp.json()) as fetchedData; - setUsers(data.leaderboardData); - if (data.pageCount != pageCount) { - setPageCount(data.pageCount); - } - setLeaderboardPage(pageNum); - } else if (resp.status === 204) { - setUsers([]); - setLeaderboardPage(1); - setPageCount(1); - } else { - console.error(resp.status); + if (resp.status === 200) { + const data = (await resp.json()) as fetchedData; + setUsers(data.leaderboardData); + if (data.pageCount != pageCount) { + setPageCount(data.pageCount); } - } catch (e) { - console.error(e); + setLeaderboardPage(pageNum); + } else if (resp.status === 204) { + setUsers([]); + setLeaderboardPage(1); + setPageCount(1); } }; diff --git a/frontend/src/components/Navbar/Navbar.tsx b/frontend/src/components/Navbar/Navbar.tsx index c5b7670..98933f1 100644 --- a/frontend/src/components/Navbar/Navbar.tsx +++ b/frontend/src/components/Navbar/Navbar.tsx @@ -44,27 +44,21 @@ function Navbar() { useEffect(() => { const getUsername = async () => { - try { - const resp = await fetch("/api/user", { - method: "GET", - headers: { - "Content-Type": "application/json", - }, - credentials: "include", - }); + const resp = await fetch("/api/user", { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + credentials: "include", + }); - if (resp.ok) { - return resp.json().then((r) => { - setUsername(r.username); - }); - } else { - console.error(resp); - } - } catch (e) { - console.error(e); + if (resp.ok) { + return resp.json().then((r) => { + setUsername(r.username); + }); } }; - getUsername().catch(console.error); + getUsername(); }, []); return ( diff --git a/frontend/src/components/ProfileDropdown/ProfileDropdown.tsx b/frontend/src/components/ProfileDropdown/ProfileDropdown.tsx index d52b52f..1493edd 100644 --- a/frontend/src/components/ProfileDropdown/ProfileDropdown.tsx +++ b/frontend/src/components/ProfileDropdown/ProfileDropdown.tsx @@ -7,22 +7,16 @@ function ProfileDropdown(props: { username: string }) { const navigate = useNavigate(); const handleClick = async () => { - try { - const resp = await fetch("/api/logout", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - credentials: "include", - }); + const resp = await fetch("/api/logout", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + credentials: "include", + }); - if (resp.ok) { - navigate("/login", { replace: true }); - } else { - console.error(resp); - } - } catch (e) { - console.error(e); + if (resp.ok) { + navigate("/login", { replace: true }); } }; diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 367118c..3de7792 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -7,9 +7,8 @@ export default defineConfig({ server: { proxy: { "/api": { - target: "https://yellowshirt-backend.fly.dev/api", + target: "https://yellowshirt-backend.fly.dev", changeOrigin: true, - secure: false, rewrite: (path) => path.replace(/^\/api/, ""), }, }, From d31d55f8713996f09fe6e97ce23b1edb86700e15 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Sat, 19 Oct 2024 01:19:13 +1100 Subject: [PATCH 16/17] =?UTF-8?q?=E2=9C=8D=20fix=20isprocessing=20not=20ch?= =?UTF-8?q?anging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/Forms/Login/Login.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frontend/src/components/Forms/Login/Login.tsx b/frontend/src/components/Forms/Login/Login.tsx index 6ce5abb..43e04c8 100644 --- a/frontend/src/components/Forms/Login/Login.tsx +++ b/frontend/src/components/Forms/Login/Login.tsx @@ -66,6 +66,8 @@ function LoginPage() { credentials: "include", }); + setIsProcessing(false); + if (resp.ok) { navigate("/gamemodes"); } else { @@ -76,7 +78,6 @@ function LoginPage() { return setPasswordMatch(false); } } - setIsProcessing(false); }; return ( From efba86bb1174e5598909c6b207697a7f8efff066 Mon Sep 17 00:00:00 2001 From: lachlanshoesmith <12870244+lachlanshoesmith@users.noreply.github.com> Date: Sat, 19 Oct 2024 02:41:00 +1100 Subject: [PATCH 17/17] =?UTF-8?q?=F0=9F=94=92=20fix=20bcrypt=20crashing=20?= =?UTF-8?q?on=20alpine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/.dockerignore | 2 ++ backend/Dockerfile | 10 +++++----- backend/src/index.ts | 8 ++++++-- frontend/vite.config.ts | 1 + 4 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 backend/.dockerignore diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..210c47a --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,2 @@ +node_modules +Dockerfile diff --git a/backend/Dockerfile b/backend/Dockerfile index a5c7b56..3b91797 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -3,11 +3,14 @@ ENV PNPM_HOME="/pnpm" ENV PATH="$PNPM_HOME:$PATH" RUN corepack enable +# install dependencies for compiling bcrypt +RUN apk --no-cache add --virtual builds-deps build-base python3 + COPY . /yellowshirt-backend WORKDIR /yellowshirt-backend FROM base AS prod-deps -RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod ENV NODE_ENV=production @@ -21,10 +24,7 @@ ENV NODE_ENV=production #ENV SESSION_SECRET=... FROM base AS build -RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile - -RUN ls -l - +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install && pnpm rebuild bcrypt RUN pnpm run build FROM base diff --git a/backend/src/index.ts b/backend/src/index.ts index b4a853f..2bb040f 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -100,6 +100,7 @@ if (process.env.ALLOWED_ORIGINS) { } else { allowedOrigins = []; } + app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.use( @@ -217,13 +218,16 @@ app.post("/login", async (req: TypedRequest, res: Response) => { details.docs[0].data().password, async (err: Error | undefined, result: boolean) => { if (err) { - return res.status(500).send("Error processing password"); + errorCheck.passwordInvalid = true; + return res.status(401).json(errorCheck); } if (result) { req.session.regenerate(async (err: Error | null) => { if (err) { - return res.status(500).send("Error regenerating session."); + return res.status(500).json({ + error: "Error regenerating session", + }); } const expiryTime: Date = new Date(); expiryTime.setDate(expiryTime.getDate() + 7); diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 3de7792..4c2397d 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -8,6 +8,7 @@ export default defineConfig({ proxy: { "/api": { target: "https://yellowshirt-backend.fly.dev", + // target: "http://localhost:3000", changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ""), },