From 0e016745527860ecc26a16fc551da489996f9f5b Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 7 Dec 2024 15:44:53 +0100 Subject: [PATCH] Feature/set hashedKey of ApiKey to unique (#4103) * Set hashedKey to unique --- apps/api/src/services/api-key/api-key.service.ts | 2 +- .../migration.sql | 5 +++++ prisma/schema.prisma | 3 +-- 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 prisma/migrations/20241207142023_set_hashed_key_of_api_key_to_unique/migration.sql diff --git a/apps/api/src/services/api-key/api-key.service.ts b/apps/api/src/services/api-key/api-key.service.ts index 2a1f14d03..f70e5330c 100644 --- a/apps/api/src/services/api-key/api-key.service.ts +++ b/apps/api/src/services/api-key/api-key.service.ts @@ -32,7 +32,7 @@ export class ApiKeyService { public async getUserByApiKey(apiKey: string) { const hashedKey = this.hashApiKey(apiKey); - const { user } = await this.prismaService.apiKey.findFirst({ + const { user } = await this.prismaService.apiKey.findUnique({ include: { user: true }, where: { hashedKey } }); diff --git a/prisma/migrations/20241207142023_set_hashed_key_of_api_key_to_unique/migration.sql b/prisma/migrations/20241207142023_set_hashed_key_of_api_key_to_unique/migration.sql new file mode 100644 index 000000000..f9a6eecbb --- /dev/null +++ b/prisma/migrations/20241207142023_set_hashed_key_of_api_key_to_unique/migration.sql @@ -0,0 +1,5 @@ +-- DropIndex +DROP INDEX "ApiKey_hashedKey_idx"; + +-- CreateIndex +CREATE UNIQUE INDEX "ApiKey_hashedKey_key" ON "ApiKey"("hashedKey"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e2587acf7..7df28d694 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -79,13 +79,12 @@ model Analytics { model ApiKey { createdAt DateTime @default(now()) - hashedKey String + hashedKey String @unique id String @id @default(uuid()) updatedAt DateTime @updatedAt userId String user User @relation(fields: [userId], onDelete: Cascade, references: [id]) - @@index([hashedKey]) @@index([userId]) }