9

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);
    },
  },
});
N1K
  • 128
  • 1
  • 6

4 Answers4

10

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.

Goran Jakovljevic
  • 2,714
  • 1
  • 31
  • 27
3

In the NextAuth.JS 4.0 the "Prisma schema" have slightly changed.

From the upgrade guide:

  • created_at/createdAt and updated_at/updatedAt fields are removed from all Models.
  • user_id/userId consistently named userId.
  • compound_id/compoundId is removed from Account.
  • access_token/accessToken is removed from Session.
  • email_verified/emailVerified on User is consistently named email_verified.
  • provider_id/providerId renamed to provider on Account
  • provider_type/providerType renamed to type on Account
  • provider_account_id/providerAccountId on Account is consistently named providerAccountId
  • access_token_expires/accessTokenExpires on Account renamed to expires_in
  • New fields on Account: expires_at, token_type, scope, id_token, session_state
  • verification_requests table has been renamed to verification_tokens

Complete new schema in: https://next-auth.js.org/adapters/prisma

juliomalves
  • 42,130
  • 20
  • 150
  • 146
2

my version: "@next-auth/prisma-adapter": "^1.0.5", "next-auth": "^4.21.1",

i have resolved the error, uninstall @next-auth/prisma-adapter next-auth, and install @next-auth/prisma-adapter next-auth again

Lutvee
  • 21
  • 1
0

I used "debug:process.env.NODE_ENV ==='development'", and worked just fine