Search code examples
next.jsprismanext-auth

How to resolve 'getUserByAccount is not a function' in next-auth?


I've updated Nextjs to it's newest version and also updated next-auth and the prisma adapter as specified by the docs.

However, when I try to authenticate in the app with signIn I get the following error with the latest updates:

[next-auth][error][OAUTH_CALLBACK_HANDLER_ERROR] 
https://next-auth.js.org/errors#oauth_callback_handler_error getUserByAccount is not a function {
  message: 'getUserByAccount is not a function',
  stack: 'TypeError: getUserByAccount is not a function\n' +
    '    at Object.callback (/home/.../node_modules/next-auth/core/routes/callback.js:81:39)\n' +
    '    at runMicrotasks (<anonymous>)\n' +
    '    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n' +
    '    at async NextAuthHandler (/home/.../node_modules/next-auth/core/index.js:103:28)\n' +
    '    at async NextAuthNextHandler (/home/.../node_modules/next-auth/next/index.js:40:7)\n' +
    '    at async [...]/node_modules/next-auth/next/index.js:80:32\n' +
    '    at async Object.apiResolver (/home/.../node_modules/next/dist/server/api-utils.js:102:9)\n' +
    '    at async DevServer.handleApiRequest (/home/.../node_modules/next/dist/server/next-server.js:1014:9)\n' +
    '    at async Object.fn (/home/.../node_modules/next/dist/server/next-server.js:901:37)\n' +
    '    at async Router.execute (/home/.../node_modules/next/dist/server/router.js:210:32)',
  name: 'TypeError'
}

Is there something I'm doing wrong, or is there an incompatibility I'm missing?

Relevant package.json:

...
    "@next-auth/prisma-adapter": "^0.5.2-next.19",
    "next": "^12.0.3",
    "next-auth": "4.0.0-beta.6",
    "prisma": "^3.4.1",
...

[...nextauth].ts:

import NextAuth from 'next-auth';
import CognitoProvider from 'next-auth/providers/cognito';
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

export default NextAuth({
  adapter: PrismaAdapter(prisma),
  providers: [
    CognitoProvider({
      clientId: process.env.COGNITO_CLIENT_ID,
      clientSecret: process.env.COGNITO_CLIENT_SECRET,
      issuer: process.env.COGNITO_ISSUER,
    }),
  ],

  callbacks: {
    async session({ session, user }) {
      session.userId = user.id;
      session.role = user.role;
      return Promise.resolve(session);
    },
  },
});

Solution

  • Finally resolved the problem. Since next-auth has moved to monorepo, updating package was not enough, you need to uninstall it first then install it again.

    Run:

    npm uninstall next-auth @next-auth/prisma-adapter
    

    then:

    npm install @next-auth/prisma-adapter
    

    This fixed it for me.