如何在next-auth中解决“getUserByAccount不是函数”?

kzmpq1sx  于 2023-04-11  发布在  其他
关注(0)|答案(3)|浏览(133)

我已经更新了Nextjs到它的最新版本,还更新了文档中指定的next-auth和prisma适配器。
但是,当我尝试在应用程序中使用signIn进行身份验证时,我在最新更新中遇到以下错误:

[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'
}

是我做错了什么,还是我错过了什么不相容的地方?
相关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);
    },
  },
});
niwlg2el

niwlg2el1#

终于解决了这个问题。由于next-auth已经移动到monorepo,更新包是不够的,你需要先卸载它,然后再安装它。
运行:

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

然后:

npm install @next-auth/prisma-adapter

这为我修好了。

7xzttuei

7xzttuei2#

在NextAuth.JS 4.0中,“Prisma模式”略有变化。
从升级指南:

  • created_at/createdAtupdated_at/updatedAt字段从所有模型中删除。
  • user_id/userId一致命名为userId
  • compound_id/compoundId已从帐户中删除。
  • access_token/accessToken已从会话中删除。
  • 用户上的email_verified/emailVerified始终命名为email_verified
  • provider_id/providerId重命名为帐户上的提供商
  • provider_type/providerType重命名为“帐户”类型
  • 帐户上的provider_account_id/providerAccountId始终命名为providerAccountId
  • 帐户重命名为expires_inaccess_token_expires/accessTokenExpires
  • 帐户上的新字段:expires_attoken_typescopeid_tokensession_state
  • verification_requests表已重命名为verification_tokens

在以下位置完成新架构:https://next-auth.js.org/adapters/prisma

yebdmbv4

yebdmbv43#

我的版本:“@next-auth/prisma-adapter”:“^1.0.5”,“next-auth”:“^4.21.1”,
我已经解决了这个错误,卸载@next-auth/prisma-adapter next-auth,然后再次安装@next-auth/prisma-adapter next-auth

相关问题