You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prisma Auth适配器报错:TypeError: 无法读取undefined的'create'属性

登录时出现AdapterError及TypeError问题排查与解决

我使用Prisma连接MongoDB,原本运行正常,删除MongoDB中所有文档后,登录时出现以下错误:

[auth][error] AdapterError: 详情见https://errors.authjs.dev#adaptererror
[auth][cause]: TypeError: Cannot read properties of undefined (reading 'create')


使用的依赖版本

"next": "^14.1.4",
"next-auth": "^5.0.0-beta.17",
"@auth/prisma-adapter": "^2.0.0",
"@prisma/client": "^5.13.0",

Prisma Schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}
 
model User {
  id            String          @id @default(auto()) @map("_id") @db.ObjectId
  name          String?
  email         String?         @unique
  emailVerified DateTime?
  image         String?
  hashedPassword String?
  accounts      Account[]
  
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
 
model Account {
  id                String  @id @default(auto()) @map("_id") @db.ObjectId
  userId            String  @db.ObjectId
  type              String
  provider          String
  providerAccountId String
  refresh_token     String? @db.String
  access_token      String? @db.String
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String? @db.String
  session_state     String?
 
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
 
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)
 
  @@unique([provider, providerAccountId])
}

Prisma Client 配置(lib/prismadb.ts)

import { PrismaClient } from '@prisma/client';

const globalForPrisma = global as unknown as { prisma: PrismaClient };

export const prisma = globalForPrisma.prisma || new PrismaClient();

if (process.env.NODE_ENV === 'production') globalForPrisma.prisma = prisma;

NextAuth 配置(auth.ts)

import type { NextAuthConfig } from 'next-auth';
import NextAuth from 'next-auth';
import Google from 'next-auth/providers/google';
import { PrismaAdapter } from '@auth/prisma-adapter';
import { prisma } from './lib/prismadb';

const config = {
  adapter: PrismaAdapter(prisma),
  providers: [Google],
  pages: {
    signIn: '/login',
  },
} satisfies NextAuthConfig;

export const { handlers, auth, signIn, signOut } = NextAuth(config);

登录页面按钮代码

import { signIn } from 'next-auth/react';

<Button
  variant="outline"
  onClick={() => signIn('google')}
  className="w-full mt-5 border-indigo-600"
>
  <Icons.google className="mr-2 h-4 w-4" />
  Google
</Button>;

问题原因及解决方法

这个错误的核心原因是NextAuth v5的Prisma适配器要求Schema中必须包含Session和VerificationToken模型。删除所有文档后首次登录时,适配器尝试创建关联记录,但由于Schema缺失这两个模型,Prisma Client没有对应的create方法,从而触发TypeError。

解决步骤:

  1. 更新Prisma Schema,添加以下两个模型:
model Session {
  id           String   @id @default(auto()) @map("_id") @db.ObjectId
  sessionToken String   @unique
  userId       String   @db.ObjectId
  expires      DateTime
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime
  createdAt  DateTime @default(now())
  updatedAt  DateTime @updatedAt

  @@unique([identifier, token])
}
  1. 重新生成Prisma Client:
    在终端运行命令:
npx prisma generate
  1. 重启服务或重新部署,确保新的Prisma Client生效。

额外检查点:

  • 确认MongoDB连接正常,数据库拥有足够的读写权限
  • 检查环境变量DATABASE_URL配置是否正确
  • 当前@auth/prisma-adapter@2.0.0与next-auth@5.0.0-beta.17版本兼容,无需调整依赖版本

内容的提问来源于stack exchange,提问作者yanger rai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 05:55:55