Search code examples
mongodbnext.jsprismanext-auth

Prisma Auth Adaptor - TypeError: Cannot read properties of undefined (reading 'create')


When I try to sign in, I get this error:

[auth][error] AdapterError: Read more at https://errors.authjs.dev#adaptererror

[auth][cause]: TypeError: Cannot read properties of undefined (reading 'create')

I am using prisma to connect to mongodb. It was working fine at first, then I deleted all documents in mongodb and tried again and since then I am facing this issue.

I am using:

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

This is my prima scheme:

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 in lib folder

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;

and 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);

this is the error: server log

the login page, I'm directly calling the signin function from button:

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>;

Solution

  • I tried using your auth.ts in one of my projects and yes it was giving the same error.

    Solution: just use the jwt session strategy in auth config and it works fine.

    const config = {
        adapter: PrismaAdapter(db),
        providers: [Google],
        pages: {
          signIn: '/auth/login',
        },
        session: { strategy: "jwt" },
    } satisfies NextAuthConfig;