Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Consolidate the db-service repo into this one #467

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ AUTH0_WEBSERVER_CLIENT_ID=
AUTH0_WEBSERVER_CLIENT_SECRET=
AUTH0_AUDIENCE=

API_KEY=

DATABASE_URL="postgres://jimmyli:@localhost:5432/monks-and-mages-local"
PORT="5001"

AUTH0_AUDIENCE=
AUTH0_ISSUER_BASE_URL=

AUTH0_WEBSERVER_CLIENT_ID=
AUTH0_WEBSERVER_CLIENT_SECRET=

API_KEY=
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,43 @@
"private": true,
"scripts": {
"build": "npx webpack",
"postinstall": "npx prisma generate",
"migrate:dev": "npx prisma migrate dev",
"db:deploy": "npx prisma migrate deploy",
"scrapeImages": "npx esrun src/scripts/scrapeImages.ts",
"compressImages": "npx esrun src/scripts/compressImages.ts",
"images": "yarn scrapeImages && yarn compressImages",
"ci": "yarn lint && jest --maxWorkers=2",
"dev": "npx webpack --watch & nodemon .",
"heroku-cleanup": "yarn install --dev",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx --fix && yarn tsc --skipLibCheck --noEmit",
"start": "npx webpack & cp src/server/homepage.html dist/homepage.html & node .",
"start": "cp src/server/homepage.html dist/homepage.html & node .",
"test": "yarn lint && jest --watchAll"
},
"dependencies": {
"@auth0/auth0-react": "^1.10.1",
"@prisma/client": "^3.14.0",
"@reduxjs/toolkit": "^1.7.2",
"@socket.io/admin-ui": "^0.2.0",
"@types/uuid": "^8.3.4",
"auth0": "^2.40.0",
"axios": "^0.27.2",
"dotenv": "^16.0.1",
"express": "^4.17.2",
"express-jwt": "^8.4.1",
"express-oauth2-jwt-bearer": "^1.3.0",
"framer-motion": "^10.0.1",
"history": "^5.3.0",
"jsonwebtoken": "^8.5.1",
"jwks-rsa": "^3.1.0",
"lodash": "^4.17.21",
"lodash.clonedeep": "^4.5.0",
"lodash.difference": "^4.5.0",
"lodash.isequal": "^4.5.0",
"lodash.samplesize": "^4.2.0",
"lodash.shuffle": "^4.2.0",
"lodash.uniqby": "^4.7.0",
"prisma": "^3.14.0",
"react": "^18.2.0",
"react-cookie": "^4.1.1",
"react-dom": "^18.2.0",
Expand Down Expand Up @@ -104,4 +112,4 @@
"node": "16.x"
},
"license": "MIT"
}
}
67 changes: 67 additions & 0 deletions prisma/migrations/20220531021102_db_setup/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- CreateTable
CREATE TABLE "User" (
"uid" VARCHAR(255) NOT NULL,
"username" VARCHAR(255) NOT NULL,

CONSTRAINT "User_pkey" PRIMARY KEY ("uid")
);

-- CreateTable
CREATE TABLE "SavedDeck" (
"id" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"name" TEXT NOT NULL,
"userUid" VARCHAR(255) NOT NULL,

CONSTRAINT "SavedDeck_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "GameResult" (
"id" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"guests" TEXT[],
"winningGuests" TEXT[],

CONSTRAINT "GameResult_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "_PlayedGames" (
"A" TEXT NOT NULL,
"B" VARCHAR(255) NOT NULL
);

-- CreateTable
CREATE TABLE "_WonGames" (
"A" TEXT NOT NULL,
"B" VARCHAR(255) NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "_PlayedGames_AB_unique" ON "_PlayedGames"("A", "B");

-- CreateIndex
CREATE INDEX "_PlayedGames_B_index" ON "_PlayedGames"("B");

-- CreateIndex
CREATE UNIQUE INDEX "_WonGames_AB_unique" ON "_WonGames"("A", "B");

-- CreateIndex
CREATE INDEX "_WonGames_B_index" ON "_WonGames"("B");

-- AddForeignKey
ALTER TABLE "SavedDeck" ADD CONSTRAINT "SavedDeck_userUid_fkey" FOREIGN KEY ("userUid") REFERENCES "User"("uid") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_PlayedGames" ADD CONSTRAINT "_PlayedGames_A_fkey" FOREIGN KEY ("A") REFERENCES "GameResult"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_PlayedGames" ADD CONSTRAINT "_PlayedGames_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("uid") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_WonGames" ADD CONSTRAINT "_WonGames_A_fkey" FOREIGN KEY ("A") REFERENCES "GameResult"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_WonGames" ADD CONSTRAINT "_WonGames_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("uid") ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:

- Added the required column `skeleton` to the `SavedDeck` table without a default value. This is not possible if the table is not empty.

*/
-- AlterTable
ALTER TABLE "SavedDeck" ADD COLUMN "skeleton" JSONB NOT NULL;
3 changes: 3 additions & 0 deletions prisma/migrations/20230224131229_add_xp_curve/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "exp" INTEGER NOT NULL DEFAULT 0,
ADD COLUMN "numberOfGamesWon" INTEGER NOT NULL DEFAULT 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:

- A unique constraint covering the columns `[username]` on the table `User` will be added. If there are existing duplicate values, this will fail.

*/
-- CreateIndex
CREATE UNIQUE INDEX "User_username_key" ON "User"("username");
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "avatarUrl" TEXT NOT NULL DEFAULT E'';
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
39 changes: 39 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
datasource db {
url = env("DATABASE_URL")
provider = "postgresql"
}

generator client {
provider = "prisma-client-js"
}

model User {
uid String @id @db.VarChar(255)
username String @unique @db.VarChar(255) // auth0-provided username
createdAt DateTime @default(now())
SavedDeck SavedDeck[]
playedGames GameResult[] @relation("PlayedGames")
wonGames GameResult[] @relation("WonGames")
numberOfGamesWon Int @default(0)
exp Int @default(0)
avatarUrl String @default("")
}

model SavedDeck {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
user User @relation(fields: [userUid], references: [uid])
userUid String @db.VarChar(255)
skeleton Json
}

model GameResult {
id String @id @default(uuid())
createdAt DateTime @default(now())
userPlayers User[] @relation("PlayedGames")
guests String[]
winningUsers User[] @relation("WonGames")
winningGuests String[]
}
13 changes: 13 additions & 0 deletions src/server/authz/auth0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ManagementClient } from 'auth0';

/**
* This is the management client for verifying on the server side and making calls,
* e.g. the getUser call to get more information about a user
*/
export const auth0 = new ManagementClient({
domain: process.env.AUTH0_ISSUER_BASE_URL as string,
clientId: process.env.AUTH0_WEBSERVER_CLIENT_ID,
clientSecret: process.env.AUTH0_WEBSERVER_CLIENT_SECRET,
});

auth0.getAccessToken();
6 changes: 6 additions & 0 deletions src/server/authz/checkJWT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { auth } from 'express-oauth2-jwt-bearer';

export const checkJwt = auth({
issuerBaseURL: `https://${process.env.AUTH0_ISSUER_BASE_URL}`,
audience: process.env.AUTH0_AUDIENCE,
});
19 changes: 19 additions & 0 deletions src/server/authz/getUserFromJWT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { auth0 } from './auth0';

// parses the JWT token and uses auth0's node module to determine the user
export const getUserFromJWT = async (token = '.') => {
try {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const buff = Buffer.from(base64, 'base64');
const payloadinit = buff.toString('ascii');
const payload = JSON.parse(payloadinit);

const user = await auth0.getUser({
id: payload.sub,
});
return user;
} catch {
return null;
}
};
3 changes: 3 additions & 0 deletions src/server/authz/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './auth0';
export * from './checkJWT';
export * from './getUserFromJWT';
Loading
Loading