0

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>;
2
  • Seems like you are using a custom signIn page here. Could you please provide the code for it and tag me then maybe I can help. Since everything looks fine in the code you provided. Commented Jun 15, 2024 at 10:55
  • Thanks for looking into it, @JayendraAwasthi . Added the signin from the login page Commented Jun 15, 2024 at 12:27

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, it worked. Would you happen to know why session is required whereas in the documentation I could not find any such mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.