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

6

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);
    },
  },
});
Katharyn answered 9/11, 2021 at 13:52 Comment(0)
W
14

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.

Whitehall answered 12/2, 2022 at 5:28 Comment(2)
Now it's npm install @auth/prisma-adapterAlexiaalexin
so, no need to install next-auth? what about the handler? how do you export it?Kirbee
F
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

Fleshings answered 19/1, 2022 at 15:38 Comment(0)
R
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

Rotifer answered 5/4, 2023 at 1:35 Comment(1)
Yes, you need to make sure you get matching versions of these packages or issues will arise.Pozzy
J
0

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

Jataka answered 2/9, 2023 at 14:31 Comment(0)
C
0

I solved this problem using the following:

$ npm uninstall next-auth @next-auth/prisma-adapter
$ npm install @next-auth/prisma-adapter next-auth
Cunha answered 22/1 at 20:2 Comment(0)
N
0

I had similar problem. I still don't understand how, but dropping Account model from MongoDB, then executing this command npx prisma db push resolved my issue.

Neume answered 10/4 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.