mimi
mimi

Reputation: 170

How to resolve 'adapter_error_getUserByAccount' in NextAuth with Prisma

I'm trying to implement an auth flow in my project using Next.js + NextAuth.js + Prisma. And I'm using GitHub Provider. It works perfectly fine on my local machine, I can sign in and redirect back to the dashboard. I thought it might be a database problem, so I've tested using my production database URL, but still works fine in the local environment. Only in the production mode error occurs.

Below is the error message I got in Vercel.

[GET] /api/auth/callback/github?code=e19191fb050f6a1708e8&state=lV7j49LEjSWE9GHMBWjQ-6WGr62yqziPqhkWURnGBWQ
15:55:11:41
2022-05-16T06:55:11.782Z    4b29fadc-0c2a-4f7a-9140-fbbb45d159f2    ERROR   [next-auth][error][adapter_error_getUserByAccount] 
https://next-auth.js.org/errors#adapter_error_getuserbyaccount Cannot read property 'findUnique' of undefined {
  message: "Cannot read property 'findUnique' of undefined",
  stack: "TypeError: Cannot read property 'findUnique' of undefined\n" +
    '    at getUserByAccount (/var/task/node_modules/@next-auth/prisma-adapter/dist/index.js:11:45)\n' +
    '    at _callee2$ (/var/task/node_modules/next-auth/core/errors.js:315:29)\n' +
    '    at tryCatch (/var/task/node_modules/regenerator-runtime/runtime.js:63:40)\n' +
    '    at Generator.invoke [as _invoke] (/var/task/node_modules/regenerator-runtime/runtime.js:294:22)\n' +
    '    at Generator.next (/var/task/node_modules/regenerator-runtime/runtime.js:119:21)\n' +
    '    at asyncGeneratorStep (/var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)\n' +
    '    at _next (/var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)\n' +
    '    at /var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7\n' +
    '    at new Promise (<anonymous>)\n' +
    '    at /var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12',
  name: 'TypeError'
}
2022-05-16T06:55:11.783Z    4b29fadc-0c2a-4f7a-9140-fbbb45d159f2    ERROR   [next-auth][error][OAUTH_CALLBACK_HANDLER_ERROR] 
https://next-auth.js.org/errors#oauth_callback_handler_error Cannot read property 'findUnique' of undefined TypeError: Cannot read property 'findUnique' of undefined
    at getUserByAccount (/var/task/node_modules/@next-auth/prisma-adapter/dist/index.js:11:45)
    at _callee2$ (/var/task/node_modules/next-auth/core/errors.js:315:29)
    at tryCatch (/var/task/node_modules/regenerator-runtime/runtime.js:63:40)
    at Generator.invoke [as _invoke] (/var/task/node_modules/regenerator-runtime/runtime.js:294:22)
    at Generator.next (/var/task/node_modules/regenerator-runtime/runtime.js:119:21)
    at asyncGeneratorStep (/var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at /var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
    at new Promise (<anonymous>)
    at /var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12 {
  name: 'GetUserByAccountError',
  code: undefined
}

I've read all related threads on GitHub issues and discussions, but still cannot find any way to resolve this problem.

Upvotes: 7

Views: 8491

Answers (4)

adxxtya
adxxtya

Reputation: 809

I was using the t3 stack and it by default used the prisma declared in the server folder

import { prisma } from "@/server/db";

Everything was working perfectly fine locally, but I was getting the same error in the production.

adapter_error_getUserByAccount

I am not exactly sure why this fixed the issue, but calling a new client within the file and passing it to the adapter seemed to solve the issue

import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

export const authOptions: NextAuthOptions = {
  adapter: PrismaAdapter(prisma),
  callbacks: {
    session: ({ session, user }) => ({
      ...session,
      user: {
        ...session.user,
        id: user.id,
      },
    }),
  },
  providers: [
    GoogleProvider({
      clientId: env.GOOGLE_CLIENT_ID,
      clientSecret: env.GOOGLE_CLIENT_SECRET,
      httpOptions: {
        timeout: 60000,
      },
    }),
  ],
  secret: process.env.NEXTAUTH_SECRET,
};

Upvotes: 1

Mbugua_J
Mbugua_J

Reputation: 75

check if you are importing the prisma client as a named import rather than a default import

Upvotes: 1

CyberMessiah
CyberMessiah

Reputation: 1268

I have experienced a similar error which was due to improper code inside [...nextauth] file. You may double check its contents especially the prisma related portion. In my case I have included

    adapter: PrismaAdapter(PrismaClient)

instead of the right format

const prisma = new PrismaClient()
export default NextAuth({
adapter: PrismaAdapter(prisma),
.........

Upvotes: 1

Maor Kavod
Maor Kavod

Reputation: 149

You need to create a prisma file with the correct fields using "schema.prisma"

https://next-auth.js.org/adapters/prisma

then :

npx prisma db push
npx prisma generate

Upvotes: 5

Related Questions