nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.96k stars 3.52k forks source link

Unknown argument `provider_providerAccountId`. Did you mean `providerId_providerAccountId`? Available options are marked with ?. #11204

Closed mashwishi closed 5 months ago

mashwishi commented 5 months ago

Adapter type

@auth/prisma-adapter

Environment

  System:
    OS: Windows 11 10.0.22631
    CPU: (12) x64 Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
    Memory: 17.58 GB / 31.94 GB
  Binaries:
    Node: 18.18.2 - c:\program files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 9.8.1 - c:\program files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (122.0.2365.66)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @auth/prisma-adapter: ^1.3.3 => 1.6.0 
    next: 14.1.0 => 14.1.0 
    next-auth: 5.0.0-beta.5 => 5.0.0-beta.5 
    react: ^18 => 18.3.1 

Reproduction URL

https://github.com/nextauthjs/next-auth-example/blob/main/auth.ts

Describe the issue

I'm encountering an issue with NextAuth's Prisma adapter. When attempting to use prisma.account.findUnique() with a specific where clause, I'm getting an error indicating an invalid argument provider_providerAccountId. The error message suggests using providerId_providerAccountId instead.

Here's the error message I'm receiving:

[auth][error] AdapterError: Read more at https://errors.authjs.dev#adaptererror
[auth][cause]: PrismaClientValidationError:
Invalid `prisma.account.findUnique()` invocation:

{
  where: {
    provider_providerAccountId: {
      providerAccountId: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      provider: "google"
    },
    ...
  },
  select: {
    user: true
  }
}

Unknown argument `provider_providerAccountId`. Did you mean `providerId_providerAccountId`? Available options are marked with ?.

And here's my Prisma schema:

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

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model account {
  id                 Int       @id @default(autoincrement())
  userId             Int
  providerType       String    @db.VarChar(255)
  providerId         String    @db.VarChar(255)
  providerAccountId  String    @db.VarChar(255)
  refreshToken       String?   @db.VarChar(255)
  accessToken        String?   @db.VarChar(255)
  accessTokenExpires DateTime? @db.DateTime(0)
  createdAt          DateTime  @default(now()) @db.DateTime(0)
  updatedAt          DateTime  @default(now()) @db.DateTime(0)
  user               user      @relation(fields: [userId], references: [id], onUpdate: Restrict, map: "account_ibfk_1")

  @@unique([providerId, providerAccountId], map: "provider_providerAccountId")
  @@index([userId], map: "userId")
}

model schoolaccess {
  id        Int  @id @default(autoincrement())
  user_id   Int
  school_id Int?
  user      user @relation(fields: [user_id], references: [id], onUpdate: Restrict, map: "schoolaccess_ibfk_1")

  @@index([user_id], map: "user_id")
}

model session {
  id           Int      @id @default(autoincrement())
  userId       Int
  expires      DateTime @db.DateTime(0)
  sessionToken String   @unique(map: "sessionToken") @db.VarChar(255)
  accessToken  String   @unique(map: "accessToken") @db.VarChar(255)
  createdAt    DateTime @default(now()) @db.DateTime(0)
  updatedAt    DateTime @default(now()) @db.DateTime(0)
  user         user     @relation(fields: [userId], references: [id], onUpdate: Restrict, map: "session_ibfk_1")

  @@index([userId], map: "userId")
}

model user {
  id            Int            @id @default(autoincrement())
  name          String?        @db.VarChar(255)
  email         String?        @unique(map: "email") @db.VarChar(255)
  password      String?        @db.VarChar(255)
  emailVerified DateTime?      @db.DateTime(0)
  image         String?        @db.VarChar(255)
  role          String         @db.Text
  createdAt     DateTime       @default(now()) @db.DateTime(0)
  updatedAt     DateTime       @default(now()) @db.DateTime(0)
  account       account[]
  schoolaccess  schoolaccess[]
  session       session[]
}

model verificationrequest {
  id         Int      @id @default(autoincrement())
  identifier String   @db.VarChar(255)
  token      String   @unique(map: "token") @db.VarChar(255)
  expires    DateTime @db.DateTime(0)
  createdAt  DateTime @default(now()) @db.DateTime(0)
  updatedAt  DateTime @default(now()) @db.DateTime(0)

  @@unique([identifier, token], map: "identifier")
}

I haven't modified the Prisma schema generated by npx prisma db pull, so I believe this might be an issue related to the adapter or how NextAuth interfaces with Prisma.

Thank you for your help in resolving this issue! Let me know if you need any more information.

How to reproduce

Attempt to use google signin, Here's my auth implementation:

export const { handlers, auth, signIn, signOut } = NextAuth({
  session: { strategy: 'jwt' },
  adapter: PrismaAdapter(prisma) as Adapter,
  pages: {
    signIn: '/login',
  },
  providers: [
    google,
  ],
  callbacks: {
    authorized({ auth, request: { nextUrl } }) {
      //....
    },
    jwt: ({ token, user }) => {
      //....
    },
    session(params) {
      //....
    },
  },
});

Expected behavior

The expected behavior would be for the Prisma adapter to recognize and accept the provider_providerAccountId argument in the where clause of the prisma.account.findUnique() invocation without throwing an error. This means that the Prisma adapter should handle queries with this argument properly and not suggest a different argument (providerId_providerAccountId) in the error message.

github-actions[bot] commented 5 months ago

We could not detect a valid reproduction link. Make sure to follow the bug report template carefully.

Why was this issue closed?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We need a link to a public GitHub repository. Example: (NextAuth.js example repository).

The bug template that you filled out has a section called "Reproduction URL", which is where you should provide the link to the reproduction.

What should I do?

Depending on the reason the issue was closed, you can do the following:

In general, assume that we should not go through a lengthy onboarding process at your company code only to be able to verify an issue.

My repository is private and cannot make it public

In most cases, a private repo will not be a sufficient minimal reproduction, as this codebase might contain a lot of unrelated parts that would make our investigation take longer. Please do not make it public. Instead, create a new repository using the templates above, adding the relevant code to reproduce the issue. Common things to look out for:

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps by opening a new issue.

I think my reproduction is good enough, why aren't you looking into it quickly?

We look into every issue and monitor open issues for new comments.

However, sometimes we might miss a few due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources