From f4aacfedae8c1cf97b8c6d72ad9eb2437e4233ca Mon Sep 17 00:00:00 2001 From: augustin Date: Tue, 20 Feb 2024 12:56:48 +0100 Subject: [PATCH 1/3] fix: withDefaultAuth with anonymous user --- packages/runtime/src/enhancements/default-auth.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/runtime/src/enhancements/default-auth.ts b/packages/runtime/src/enhancements/default-auth.ts index cce9af782..c37b51b38 100644 --- a/packages/runtime/src/enhancements/default-auth.ts +++ b/packages/runtime/src/enhancements/default-auth.ts @@ -15,7 +15,7 @@ import { DefaultPrismaProxyHandler, PrismaProxyActions, makeProxy } from './prox export function withDefaultAuth( prisma: DbClient, options: InternalEnhancementOptions, - context?: EnhancementContext + context: EnhancementContext = {} ): DbClient { return makeProxy( prisma, @@ -32,14 +32,10 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler { prisma: DbClientContract, model: string, options: InternalEnhancementOptions, - private readonly context?: EnhancementContext + private readonly context: EnhancementContext ) { super(prisma, model, options); - if (!this.context?.user) { - throw new Error(`Using \`auth()\` in \`@default\` requires a user context`); - } - this.userContext = this.context.user; } @@ -95,6 +91,9 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler { } private getDefaultValueFromAuth(fieldInfo: FieldInfo) { + if (!this.userContext) { + throw new Error(`Using \`auth()\` in \`@default\` requires a user context`); + } return fieldInfo.defaultValueProvider?.(this.userContext); } } From 7eb9670fe5f80f61a0752150419510b893665475 Mon Sep 17 00:00:00 2001 From: augustin Date: Tue, 20 Feb 2024 13:28:05 +0100 Subject: [PATCH 2/3] add test --- .../enhancements/with-policy/auth.test.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/integration/tests/enhancements/with-policy/auth.test.ts b/tests/integration/tests/enhancements/with-policy/auth.test.ts index f5b4e2f4f..04a88408c 100644 --- a/tests/integration/tests/enhancements/with-policy/auth.test.ts +++ b/tests/integration/tests/enhancements/with-policy/auth.test.ts @@ -505,4 +505,33 @@ describe('With Policy: auth() test', () => { ]) ); }); + + it('Default auth() without user context', async () => { + const { enhance } = await loadSchema( + ` + model User { + id String @id + posts Post[] + + @@allow('all', true) + } + + model Post { + id String @id @default(uuid()) + title String + author User @relation(fields: [authorId], references: [id]) + authorId String @default(auth().id) + + @@allow('all', auth().id != null) + } + ` + ); + + const db = enhance(); + await expect(db.user.create({ data: { id: 'userId-1' } })).toResolveTruthy(); + await expect(db.post.create({ data: { title: 'title' } })).rejects.toThrow( + 'Using `auth()` in `@default` requires a user context' + ); + await expect(db.post.findMany({})).toResolveTruthy(); + }); }); From 32414f4f5f37267b987a0f58998b732d105c8de6 Mon Sep 17 00:00:00 2001 From: augustin Date: Tue, 20 Feb 2024 15:44:42 +0100 Subject: [PATCH 3/3] feat: improve error message --- packages/runtime/src/enhancements/default-auth.ts | 2 +- tests/integration/tests/enhancements/with-policy/auth.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/runtime/src/enhancements/default-auth.ts b/packages/runtime/src/enhancements/default-auth.ts index c37b51b38..bbbd35861 100644 --- a/packages/runtime/src/enhancements/default-auth.ts +++ b/packages/runtime/src/enhancements/default-auth.ts @@ -92,7 +92,7 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler { private getDefaultValueFromAuth(fieldInfo: FieldInfo) { if (!this.userContext) { - throw new Error(`Using \`auth()\` in \`@default\` requires a user context`); + throw new Error(`Evaluating default value of field \`${fieldInfo.name}\` requires a user context`); } return fieldInfo.defaultValueProvider?.(this.userContext); } diff --git a/tests/integration/tests/enhancements/with-policy/auth.test.ts b/tests/integration/tests/enhancements/with-policy/auth.test.ts index 04a88408c..e1fff4f73 100644 --- a/tests/integration/tests/enhancements/with-policy/auth.test.ts +++ b/tests/integration/tests/enhancements/with-policy/auth.test.ts @@ -522,7 +522,7 @@ describe('With Policy: auth() test', () => { author User @relation(fields: [authorId], references: [id]) authorId String @default(auth().id) - @@allow('all', auth().id != null) + @@allow('all', true) } ` ); @@ -530,7 +530,7 @@ describe('With Policy: auth() test', () => { const db = enhance(); await expect(db.user.create({ data: { id: 'userId-1' } })).toResolveTruthy(); await expect(db.post.create({ data: { title: 'title' } })).rejects.toThrow( - 'Using `auth()` in `@default` requires a user context' + 'Evaluating default value of field `authorId` requires a user context' ); await expect(db.post.findMany({})).toResolveTruthy(); });