From e42a938b13ab3de4ec90704fb8e9fb17c3a52f62 Mon Sep 17 00:00:00 2001 From: Emma Date: Tue, 20 Aug 2024 09:55:49 -0600 Subject: [PATCH] move to postgres, adds user checks to content creation --- actions/GameSystems/create.ts | 8 +- actions/Schemas/create.ts | 8 +- lib/secret/index.ts | 38 ++-- middleware.ts => notmiddleware.ts | 0 .../20240317161445_init/migration.sql | 46 ----- .../20240317164602_update/migration.sql | 8 - .../20240319184644_adds_types/migration.sql | 8 - .../20240815100640_retagging/migration.sql | 83 -------- .../20240818144724_auth/migration.sql | 71 ------- .../20240818160027_pw/migration.sql | 2 - .../20240820145917_init/migration.sql | 177 ++++++++++++++++++ .../migration.sql | 2 + prisma/migrations/migration_lock.toml | 2 +- prisma/schema.prisma | 4 +- util/isEmailVerified.ts | 54 ++++++ 15 files changed, 266 insertions(+), 245 deletions(-) rename middleware.ts => notmiddleware.ts (100%) delete mode 100644 prisma/migrations/20240317161445_init/migration.sql delete mode 100644 prisma/migrations/20240317164602_update/migration.sql delete mode 100644 prisma/migrations/20240319184644_adds_types/migration.sql delete mode 100644 prisma/migrations/20240815100640_retagging/migration.sql delete mode 100644 prisma/migrations/20240818144724_auth/migration.sql delete mode 100644 prisma/migrations/20240818160027_pw/migration.sql create mode 100644 prisma/migrations/20240820145917_init/migration.sql create mode 100644 prisma/migrations/20240820155443_optional_original_id/migration.sql create mode 100644 util/isEmailVerified.ts diff --git a/actions/GameSystems/create.ts b/actions/GameSystems/create.ts index ba845a6..72235c9 100644 --- a/actions/GameSystems/create.ts +++ b/actions/GameSystems/create.ts @@ -2,17 +2,13 @@ import { auth } from "@/auth"; import { prisma } from "@/prisma/prismaClient"; +import { isEmailVerified } from "@/util/isEmailVerified"; export const createGameSystem = async (name: string) => { const session = await auth(); if (!session?.user?.id) return null; - const user = await prisma.user.findFirst({ - where: { id: session.user.id }, - select: { emailVerified: true }, - }); - - if (!user?.emailVerified) return null; + if (!isEmailVerified(session.user.id)) return null; const { id } = await prisma.gameSystem.create({ data: { diff --git a/actions/Schemas/create.ts b/actions/Schemas/create.ts index 5222488..33be2e8 100644 --- a/actions/Schemas/create.ts +++ b/actions/Schemas/create.ts @@ -1,13 +1,18 @@ "use server"; +import { auth } from "@/auth"; import { prisma } from "@/prisma/prismaClient"; +import { isEmailVerified } from "@/util/isEmailVerified"; import { redirect } from "next/navigation"; export const createSchema = async (form: FormData) => { const name = form.get("name")?.toString(); const gsId = form.get("gsId")?.toString(); - if (!name || !gsId) return; + const session = await auth(); + + if (!name || !gsId || !session?.user?.id || !isEmailVerified(session.user.id)) + return; const { id } = await prisma.schema.create({ data: { @@ -16,6 +21,7 @@ export const createSchema = async (form: FormData) => { types: "{}", version: 0, gameSystemId: gsId, + authorId: session.user.id, }, select: { id: true }, }); diff --git a/lib/secret/index.ts b/lib/secret/index.ts index 085c71d..ce1633d 100644 --- a/lib/secret/index.ts +++ b/lib/secret/index.ts @@ -1,5 +1,7 @@ // import { mkdirSync, readFileSync, writeFileSync } from "fs"; -// import { writeFile } from "fs/promises"; +import { writeFile } from "fs/promises"; + +import { mkdirSync, readFileSync } from "fs"; export class DHSecretClient { private token!: Promise; //Set by init @@ -13,14 +15,10 @@ export class DHSecretClient { * @param dhBaseUri uri for hosted Dragon's Hoard instance * @param cacheDir path to cache dir */ - constructor( - private dhBaseUri: string, - private cacheDir: string, - ) { + constructor(private dhBaseUri: string, private cacheDir: string) { this.cacheLocation = this.cacheDir.trim().replace(/\/^/, "") + "/.dh_cache"; - // mkdirSync(this.cacheDir, { recursive: true }); - // writeFileSync(this.cacheLocation, "{}", { encoding: "utf-8", flag: "wx" }); - // this.readDiskCache(); + mkdirSync(this.cacheDir, { recursive: true }); + this.readDiskCache(); this.token = this.fetchToken(); } @@ -43,19 +41,22 @@ export class DHSecretClient { return token; } - // private readDiskCache() { - // const cache = readFileSync(this.cacheLocation, "utf-8"); + private readDiskCache() { + const cache = readFileSync(this.cacheLocation, "utf-8"); - // this.cache = JSON.parse(cache || "{}"); - // } - // private async writeDiskCache() { - // await writeFile(this.cacheLocation, JSON.stringify(this.cache), "utf-8"); - // } + if (!cache) { + this.cache = {}; + this.writeDiskCache(); + } else this.cache = JSON.parse(cache || "{}"); + } + private async writeDiskCache() { + await writeFile(this.cacheLocation, JSON.stringify(this.cache), "utf-8"); + } private writeCache(key: string, value: string, expires?: number) { this.cache[key] = { value, expires }; - // this.writeDiskCache(); + this.writeDiskCache(); } private readCache(key: string) { @@ -73,7 +74,10 @@ export class DHSecretClient { } async fetchSecret(secret_name: string, environment?: string) { - const uri = this.dhBaseUri + "/api/keys/" + secret_name + + const uri = + this.dhBaseUri + + "/api/keys/" + + secret_name + (environment ? "?env=" + environment : ""); const cached = this.readCache(secret_name); diff --git a/middleware.ts b/notmiddleware.ts similarity index 100% rename from middleware.ts rename to notmiddleware.ts diff --git a/prisma/migrations/20240317161445_init/migration.sql b/prisma/migrations/20240317161445_init/migration.sql deleted file mode 100644 index 3ea44f2..0000000 --- a/prisma/migrations/20240317161445_init/migration.sql +++ /dev/null @@ -1,46 +0,0 @@ --- CreateTable -CREATE TABLE `GameSystem` ( - `id` VARCHAR(191) NOT NULL, - `name` VARCHAR(191) NOT NULL, - `created` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- CreateTable -CREATE TABLE `Schema` ( - `id` VARCHAR(191) NOT NULL, - `gameSystemId` VARCHAR(191) NOT NULL, - `name` VARCHAR(191) NOT NULL, - `schema` JSON NOT NULL, - `version` INTEGER NOT NULL, - - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- CreateTable -CREATE TABLE `Publication` ( - `id` VARCHAR(191) NOT NULL, - `schemaId` VARCHAR(191) NOT NULL, - `name` VARCHAR(191) NOT NULL, - `data` JSON NOT NULL, - - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- CreateTable -CREATE TABLE `Tag` ( - `id` VARCHAR(191) NOT NULL, - `publicationId` VARCHAR(191) NOT NULL, - - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- AddForeignKey -ALTER TABLE `Schema` ADD CONSTRAINT `Schema_gameSystemId_fkey` FOREIGN KEY (`gameSystemId`) REFERENCES `GameSystem`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `Publication` ADD CONSTRAINT `Publication_schemaId_fkey` FOREIGN KEY (`schemaId`) REFERENCES `Schema`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `Tag` ADD CONSTRAINT `Tag_publicationId_fkey` FOREIGN KEY (`publicationId`) REFERENCES `Publication`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20240317164602_update/migration.sql b/prisma/migrations/20240317164602_update/migration.sql deleted file mode 100644 index 0a42173..0000000 --- a/prisma/migrations/20240317164602_update/migration.sql +++ /dev/null @@ -1,8 +0,0 @@ -/* - Warnings: - - - A unique constraint covering the columns `[name]` on the table `GameSystem` will be added. If there are existing duplicate values, this will fail. - -*/ --- CreateIndex -CREATE UNIQUE INDEX `GameSystem_name_key` ON `GameSystem`(`name`); diff --git a/prisma/migrations/20240319184644_adds_types/migration.sql b/prisma/migrations/20240319184644_adds_types/migration.sql deleted file mode 100644 index e01d09f..0000000 --- a/prisma/migrations/20240319184644_adds_types/migration.sql +++ /dev/null @@ -1,8 +0,0 @@ -/* - Warnings: - - - Added the required column `types` to the `Schema` table without a default value. This is not possible if the table is not empty. - -*/ --- AlterTable -ALTER TABLE `Schema` ADD COLUMN `types` JSON NOT NULL; diff --git a/prisma/migrations/20240815100640_retagging/migration.sql b/prisma/migrations/20240815100640_retagging/migration.sql deleted file mode 100644 index 74fb996..0000000 --- a/prisma/migrations/20240815100640_retagging/migration.sql +++ /dev/null @@ -1,83 +0,0 @@ -/* - Warnings: - - - Added the required column `authorId` to the `GameSystem` table without a default value. This is not possible if the table is not empty. - - Added the required column `authorId` to the `Publication` table without a default value. This is not possible if the table is not empty. - - Added the required column `authorId` to the `Schema` table without a default value. This is not possible if the table is not empty. - - Added the required column `originalId` to the `Schema` table without a default value. This is not possible if the table is not empty. - - Added the required column `name` to the `Tag` table without a default value. This is not possible if the table is not empty. - -*/ --- DropForeignKey -ALTER TABLE `Schema` DROP FOREIGN KEY `Schema_gameSystemId_fkey`; - --- DropForeignKey -ALTER TABLE `Tag` DROP FOREIGN KEY `Tag_publicationId_fkey`; - --- AlterTable -ALTER TABLE `GameSystem` ADD COLUMN `authorId` VARCHAR(191) NOT NULL; - --- AlterTable -ALTER TABLE `Publication` ADD COLUMN `authorId` VARCHAR(191) NOT NULL; - --- AlterTable -ALTER TABLE `Schema` ADD COLUMN `authorId` VARCHAR(191) NOT NULL, - ADD COLUMN `originalId` VARCHAR(191) NOT NULL, - MODIFY `gameSystemId` VARCHAR(191) NULL; - --- AlterTable -ALTER TABLE `Tag` ADD COLUMN `name` VARCHAR(191) NOT NULL, - MODIFY `publicationId` VARCHAR(191) NULL; - --- CreateTable -CREATE TABLE `TagsOnPublications` ( - `publicationId` VARCHAR(191) NOT NULL, - `tagId` VARCHAR(191) NOT NULL, - - PRIMARY KEY (`publicationId`, `tagId`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- CreateTable -CREATE TABLE `TagsOnTags` ( - `parentTagId` VARCHAR(191) NOT NULL, - `childTagId` VARCHAR(191) NOT NULL, - - PRIMARY KEY (`parentTagId`, `childTagId`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- CreateTable -CREATE TABLE `User` ( - `id` VARCHAR(191) NOT NULL, - `username` VARCHAR(191) NOT NULL, - `email` VARCHAR(191) NOT NULL, - - UNIQUE INDEX `User_email_key`(`email`), - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- AddForeignKey -ALTER TABLE `GameSystem` ADD CONSTRAINT `GameSystem_authorId_fkey` FOREIGN KEY (`authorId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `Schema` ADD CONSTRAINT `Schema_gameSystemId_fkey` FOREIGN KEY (`gameSystemId`) REFERENCES `GameSystem`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `Schema` ADD CONSTRAINT `Schema_authorId_fkey` FOREIGN KEY (`authorId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `Publication` ADD CONSTRAINT `Publication_authorId_fkey` FOREIGN KEY (`authorId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `TagsOnPublications` ADD CONSTRAINT `TagsOnPublications_publicationId_fkey` FOREIGN KEY (`publicationId`) REFERENCES `Publication`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `TagsOnPublications` ADD CONSTRAINT `TagsOnPublications_tagId_fkey` FOREIGN KEY (`tagId`) REFERENCES `Tag`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `Tag` ADD CONSTRAINT `Tag_publicationId_fkey` FOREIGN KEY (`publicationId`) REFERENCES `Publication`(`id`) ON DELETE SET NULL ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `TagsOnTags` ADD CONSTRAINT `TagsOnTags_parentTagId_fkey` FOREIGN KEY (`parentTagId`) REFERENCES `Tag`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `TagsOnTags` ADD CONSTRAINT `TagsOnTags_childTagId_fkey` FOREIGN KEY (`childTagId`) REFERENCES `Tag`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20240818144724_auth/migration.sql b/prisma/migrations/20240818144724_auth/migration.sql deleted file mode 100644 index 10f709e..0000000 --- a/prisma/migrations/20240818144724_auth/migration.sql +++ /dev/null @@ -1,71 +0,0 @@ -/* - Warnings: - - - A unique constraint covering the columns `[username]` on the table `User` will be added. If there are existing duplicate values, this will fail. - - Added the required column `updatedAt` to the `User` table without a default value. This is not possible if the table is not empty. - -*/ --- AlterTable -ALTER TABLE `User` ADD COLUMN `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - ADD COLUMN `emailVerified` DATETIME(3) NULL, - ADD COLUMN `image` VARCHAR(191) NULL, - ADD COLUMN `name` VARCHAR(191) NULL, - ADD COLUMN `updatedAt` DATETIME(3) NOT NULL, - MODIFY `username` VARCHAR(191) NULL, - MODIFY `email` VARCHAR(191) NULL; - --- CreateTable -CREATE TABLE `Account` ( - `id` VARCHAR(191) NOT NULL, - `userId` VARCHAR(191) NOT NULL, - `type` VARCHAR(191) NOT NULL, - `provider` VARCHAR(191) NOT NULL, - `providerAccountId` VARCHAR(191) NOT NULL, - `refresh_token` TEXT NULL, - `access_token` TEXT NULL, - `expires_at` INTEGER NULL, - `token_type` VARCHAR(191) NULL, - `scope` VARCHAR(191) NULL, - `id_token` TEXT NULL, - `session_state` VARCHAR(191) NULL, - `refresh_token_expires_in` INTEGER NULL, - `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - `updatedAt` DATETIME(3) NOT NULL, - - UNIQUE INDEX `Account_userId_key`(`userId`), - INDEX `Account_userId_idx`(`userId`), - UNIQUE INDEX `Account_provider_providerAccountId_key`(`provider`, `providerAccountId`), - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- CreateTable -CREATE TABLE `Session` ( - `id` VARCHAR(191) NOT NULL, - `sessionToken` VARCHAR(191) NOT NULL, - `userId` VARCHAR(191) NOT NULL, - `expires` DATETIME(3) NOT NULL, - `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - `updatedAt` DATETIME(3) NOT NULL, - - UNIQUE INDEX `Session_sessionToken_key`(`sessionToken`), - INDEX `Session_userId_idx`(`userId`), - PRIMARY KEY (`id`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- CreateTable -CREATE TABLE `VerificationToken` ( - `identifier` VARCHAR(191) NOT NULL, - `token` VARCHAR(191) NOT NULL, - `expires` DATETIME(3) NOT NULL, - - UNIQUE INDEX `VerificationToken_identifier_token_key`(`identifier`, `token`) -) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- CreateIndex -CREATE UNIQUE INDEX `User_username_key` ON `User`(`username`); - --- AddForeignKey -ALTER TABLE `Account` ADD CONSTRAINT `Account_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE `Session` ADD CONSTRAINT `Session_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20240818160027_pw/migration.sql b/prisma/migrations/20240818160027_pw/migration.sql deleted file mode 100644 index 82202fe..0000000 --- a/prisma/migrations/20240818160027_pw/migration.sql +++ /dev/null @@ -1,2 +0,0 @@ --- AlterTable -ALTER TABLE `User` ADD COLUMN `passwordHash` VARCHAR(191) NULL; diff --git a/prisma/migrations/20240820145917_init/migration.sql b/prisma/migrations/20240820145917_init/migration.sql new file mode 100644 index 0000000..c6ae696 --- /dev/null +++ b/prisma/migrations/20240820145917_init/migration.sql @@ -0,0 +1,177 @@ +-- CreateTable +CREATE TABLE "GameSystem" ( + "id" TEXT NOT NULL, + "authorId" TEXT NOT NULL, + "name" TEXT NOT NULL, + "created" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "GameSystem_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Schema" ( + "id" TEXT NOT NULL, + "gameSystemId" TEXT, + "authorId" TEXT NOT NULL, + "originalId" TEXT NOT NULL, + "name" TEXT NOT NULL, + "schema" JSONB NOT NULL, + "types" JSONB NOT NULL, + "version" INTEGER NOT NULL, + + CONSTRAINT "Schema_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Publication" ( + "id" TEXT NOT NULL, + "schemaId" TEXT NOT NULL, + "authorId" TEXT NOT NULL, + "name" TEXT NOT NULL, + "data" JSONB NOT NULL, + + CONSTRAINT "Publication_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "TagsOnPublications" ( + "publicationId" TEXT NOT NULL, + "tagId" TEXT NOT NULL, + + CONSTRAINT "TagsOnPublications_pkey" PRIMARY KEY ("publicationId","tagId") +); + +-- CreateTable +CREATE TABLE "Tag" ( + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "publicationId" TEXT, + + CONSTRAINT "Tag_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "TagsOnTags" ( + "parentTagId" TEXT NOT NULL, + "childTagId" TEXT NOT NULL, + + CONSTRAINT "TagsOnTags_pkey" PRIMARY KEY ("parentTagId","childTagId") +); + +-- CreateTable +CREATE TABLE "User" ( + "id" TEXT NOT NULL, + "name" TEXT, + "username" TEXT, + "email" TEXT, + "emailVerified" TIMESTAMP(3), + "passwordHash" TEXT, + "image" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "User_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Account" ( + "id" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "type" TEXT NOT NULL, + "provider" TEXT NOT NULL, + "providerAccountId" TEXT NOT NULL, + "refresh_token" TEXT, + "access_token" TEXT, + "expires_at" INTEGER, + "token_type" TEXT, + "scope" TEXT, + "id_token" TEXT, + "session_state" TEXT, + "refresh_token_expires_in" INTEGER, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "Account_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Session" ( + "id" TEXT NOT NULL, + "sessionToken" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "expires" TIMESTAMP(3) NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "Session_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "VerificationToken" ( + "identifier" TEXT NOT NULL, + "token" TEXT NOT NULL, + "expires" TIMESTAMP(3) NOT NULL +); + +-- CreateIndex +CREATE UNIQUE INDEX "GameSystem_name_key" ON "GameSystem"("name"); + +-- CreateIndex +CREATE UNIQUE INDEX "User_username_key" ON "User"("username"); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); + +-- CreateIndex +CREATE UNIQUE INDEX "Account_userId_key" ON "Account"("userId"); + +-- CreateIndex +CREATE INDEX "Account_userId_idx" ON "Account"("userId"); + +-- CreateIndex +CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId"); + +-- CreateIndex +CREATE UNIQUE INDEX "Session_sessionToken_key" ON "Session"("sessionToken"); + +-- CreateIndex +CREATE INDEX "Session_userId_idx" ON "Session"("userId"); + +-- CreateIndex +CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token"); + +-- AddForeignKey +ALTER TABLE "GameSystem" ADD CONSTRAINT "GameSystem_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Schema" ADD CONSTRAINT "Schema_gameSystemId_fkey" FOREIGN KEY ("gameSystemId") REFERENCES "GameSystem"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Schema" ADD CONSTRAINT "Schema_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Publication" ADD CONSTRAINT "Publication_schemaId_fkey" FOREIGN KEY ("schemaId") REFERENCES "Schema"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Publication" ADD CONSTRAINT "Publication_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "TagsOnPublications" ADD CONSTRAINT "TagsOnPublications_publicationId_fkey" FOREIGN KEY ("publicationId") REFERENCES "Publication"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "TagsOnPublications" ADD CONSTRAINT "TagsOnPublications_tagId_fkey" FOREIGN KEY ("tagId") REFERENCES "Tag"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Tag" ADD CONSTRAINT "Tag_publicationId_fkey" FOREIGN KEY ("publicationId") REFERENCES "Publication"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "TagsOnTags" ADD CONSTRAINT "TagsOnTags_parentTagId_fkey" FOREIGN KEY ("parentTagId") REFERENCES "Tag"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "TagsOnTags" ADD CONSTRAINT "TagsOnTags_childTagId_fkey" FOREIGN KEY ("childTagId") REFERENCES "Tag"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Account" ADD CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Session" ADD CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20240820155443_optional_original_id/migration.sql b/prisma/migrations/20240820155443_optional_original_id/migration.sql new file mode 100644 index 0000000..e3b080b --- /dev/null +++ b/prisma/migrations/20240820155443_optional_original_id/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Schema" ALTER COLUMN "originalId" DROP NOT NULL; diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml index e5a788a..fbffa92 100644 --- a/prisma/migrations/migration_lock.toml +++ b/prisma/migrations/migration_lock.toml @@ -1,3 +1,3 @@ # Please do not edit this file manually # It should be added in your version-control system (i.e. Git) -provider = "mysql" \ No newline at end of file +provider = "postgresql" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a0f30b8..76b69eb 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -9,7 +9,7 @@ generator client { } datasource db { - provider = "mysql" + provider = "postgresql" url = env("DATABASE_URL") } @@ -31,7 +31,7 @@ model Schema { author User @relation(fields: [authorId], references: [id]) authorId String - originalId String + originalId String? name String schema Json types Json diff --git a/util/isEmailVerified.ts b/util/isEmailVerified.ts new file mode 100644 index 0000000..2dbe8a2 --- /dev/null +++ b/util/isEmailVerified.ts @@ -0,0 +1,54 @@ +import { prisma } from "@/prisma/prismaClient"; + +export async function isEmailVerified(id: string) { + const user = await prisma.user.findUnique({ + where: { id }, + select: { + emailVerified: true, + email: true, + accounts: { select: { provider: true, access_token: true } }, + }, + }); + if (!user) return false; + + if (user?.emailVerified) return true; + + const discordAccount = user.accounts.find((a) => a.provider === "discord"); + if (user && discordAccount?.access_token) { + const dcUser = await getDiscordUserInfo(discordAccount.access_token); + if (!dcUser.verified) return false; + prisma.user.update({ where: { id }, data: { emailVerified: new Date() } }); + return true; + } +} + +async function getDiscordUserInfo(accessToken: string): Promise<{ + id: string; + username: string; + discriminator: string; + avatar: string; + verified: boolean; + email: string; + flags: number; + banner: string; + accent_color: number; + premium_type: number; + public_flags: number; + avatar_decoration_data: { + sku_id: string; + asset: string; + }; +}> { + try { + const response = await fetch("https://discord.com/api/users/@me", { + headers: { + Authorization: `Bearer ${accessToken}`, + }, + }); + + return await response.json(); + } catch (error) { + console.error("Error fetching user info from Discord:", error); + throw error; + } +}