zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

[BUG] @@auth doesn't work if a User model is defined

Open onimitch opened this issue 1 year ago • 2 comments

Description and expected behavior If you specify @@auth on a model but also have a User model defined, Zenstack uses the User model for the AuthUser type.

model AuthUser {
    @@auth
    id String @id
}

model User {
    id String @id @default(cuid())
}

With the models above, enhance expects a type matching User despite me marking AuthUser as @@auth.

Currently my workaround is to not use @@auth, and name my AuthUser model User, but that's forced me to name my "real" User model to something else, which I don't want to do.

model User {
    id String @id
}

model RealUser {
    id String @id @default(cuid())
}

Expected behaviour

I can use @@auth on a Model and still define a User model myself.

Environment (please complete the following information)

  • ZenStack version: 2.5.1
  • Prisma version: 5.18.0
  • Database type: Postgresql

onimitch avatar Sep 11 '24 11:09 onimitch

I've also just noticed that with this set up:

model User {
    @@ignore
    id String @id
}

model RealUser {
    id String @id @default(cuid())
}

The type of user in enhance call is simply Record<string, unknown>. It would be nice if Zenstack could still generate the correct type based on the Model fields.

onimitch avatar Sep 11 '24 11:09 onimitch

OK going to edit the bug. It looks like @@ignore is a red herring. @@auth simply doesn't work if you have a model called User.

onimitch avatar Sep 11 '24 11:09 onimitch

model AuthUser { @@auth id String @id }

model User { id String @id @default(cuid()) }

Hi @onimitch , thanks for filing this. I tried with the latest release but couldn't reproduce the issue with this model:

model AuthUser {
    @@auth
    id String @id
}

model User {
    id String @id @default(cuid())
}

The typing of the generated enhance function looks correct. Do you have a project that can expose this? Thanks!

ymc9 avatar Nov 14 '24 22:11 ymc9

Btw, v2.9 will allow to use a "type" as "@@auth" so you don't need to use the "@@ignore" hack anymore.

ymc9 avatar Nov 14 '24 22:11 ymc9

Closing for now. Please feel free to reactivate if it's still an issue.

ymc9 avatar Nov 22 '24 20:11 ymc9