How to resolve 'adapter_error_getUserByAccount' in NextAuth with Prisma
Asked Answered
N

4

6

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.

Nakada answered 16/5, 2022 at 7:17 Comment(1)
Did you ever get to the bottom of this one, im facing the same issue and none of the other answers have affected my outcome.Lunulate
G
4

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
Greig answered 13/7, 2022 at 6:15 Comment(1)
Before I posted this question, I had created the Prisma schema file and ran two commands, but it hadn't resolved the error.Nakada
M
1

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),
.........
Mandibular answered 15/1, 2023 at 22:19 Comment(0)
L
1

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,
};
Larkspur answered 15/8, 2023 at 16:11 Comment(0)
P
0

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

Pilgrim answered 22/2, 2023 at 16:36 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewIrregular

© 2022 - 2024 — McMap. All rights reserved.