-
QuestionI'm getting PrismaClientInitializationErrorwhen trying to initialize prisma clientI'm new to using prisma and wanted to experiment. The connection string looks like: Currently there is nothing but my attempt to make an authentication system with next auth v5 ⨯ Error [PrismaClientInitializationError]: or 2 |
I've tryed to search for similar discussions but the solution was simply a bad url. How to reproduce (optional)No response Expected behavior (optional)Normally it shouldnt give me errors ofc Information about Prisma Schema, Client Queries and Environment (optional)generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
username String
email String @unique
password String
image String?
accounts Account[]
sessions Session[]
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])
}
model Session {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String @db.ObjectId
expiresAt DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
// auth/actions.ts
"use server";
import { FormState } from "./definitions";
import bcrypt from "bcryptjs";
import { signInSchema } from "@/lib/form-schemas";
import prisma from "@/lib/prisma";
import { createSession, deleteSession } from "@/lib/session";
import { redirect } from "next/navigation";
export async function login(
state: FormState,
formData: FormData,
): Promise<FormState> {
const validatedFields = signInSchema.safeParse({
email: formData.get("email"),
password: formData.get("password"),
});
const errorMessage = { message: "Invalid login credentials." };
if (!validatedFields.success) {
return {
errors: validatedFields.error.flatten().fieldErrors,
};
}
const user = await prisma.user.findUnique({
where: { email: validatedFields.data.email },
});
if (!user) {
return errorMessage;
}
const passwordMatch = await bcrypt.compare(
validatedFields.data.password,
user.password,
);
if (!passwordMatch) {
return errorMessage;
}
await createSession(user.id);
redirect("/dashboard");
}
export async function logout() {
await deleteSession();
redirect("/sign-in");
}// lib/prisma.ts
import { PrismaAdapter } from "@auth/prisma-adapter";
import prisma from "@/lib/prisma";
import { Adapter } from "next-auth/adapters";
import NextAuth from "next-auth";
import { authConfig } from "./auth.config";
import Credentials from "next-auth/providers/credentials";
import { signInSchema } from "@/lib/form-schemas";
import { compare } from "bcryptjs";
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma) as Adapter,
...authConfig,
providers: [
Credentials({
async authorize(credentials) {
const validatedFields = signInSchema.safeParse(credentials);
if (!validatedFields.success) return null;
const { email, password } = validatedFields.data;
const user = await prisma.user.findUnique({ where: { email } });
if (!user) return null;
const isPasswordMatch = await compare(password, user.password);
if (!isPasswordMatch) return null;
return user;
},
}),
],
});import { PrismaClient } from "@prisma/client";
const prismaClientSingleton = () => {
return new PrismaClient();
};
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClientSingleton | undefined;
};
const prisma = globalForPrisma.prisma ?? prismaClientSingleton();
if (process.env.NODE_ENV !== "production") {
globalForPrisma.prisma = prisma;
}
export default prisma;
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Hey @Devox628! You need to use Prisma version 6. It's likely that you are using Prisma version 7 which doesn't support MongoDB at the moment. |
Beta Was this translation helpful? Give feedback.
-
|
Hi, As we have not heard back from you, we are closing this discussion to keep our discussions organized. Feel free to start a new discussion if this remains relevant. Thank you for being part of the community! |
Beta Was this translation helpful? Give feedback.
Hey @Devox628!
You need to use Prisma version 6. It's likely that you are using Prisma version 7 which doesn't support MongoDB at the moment.
Could you please try with Prisma v6?