nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.09k stars 3.33k forks source link

TS: PrismaAdapter does not accept prisma client that has client extensions #6078

Closed kennykingdev closed 1 year ago

kennykingdev commented 1 year ago

Question 💬

When using the new prisma client extensions that they introduced in 4.7.0, the type of the prisma client changes with whatever extensions you apply.

If you try to pass your extended prisma client into PrismaAdapter, TS complains about the types not matching what it expects.

For now I have both a standard prisma client and an extended client. I pass the standard client into PrismaAdapter and use the extended client where needed in callbacks and events.

I would prefer that PrismaAdapter accept an extended client so that I don't have import and use both clients in [...nextAuth].ts

How to reproduce ☕️

const prisma = new PrismaClient().$extends({
  name: 'fullName',
  result: {
    user: {
      fullName: {
        needs: { firstName: true, lastName: true },
        compute(user) {
          return `${user.firstName} ${user.lastName}`;
        },
      },
    },
  },
});

export const authOptions: NextAuthOptions = {
  adapter: PrismaAdapter(prisma),
  providers: [
    GoogleProvider({
      clientId: 'GOOGLE_CLIENT_ID',
      clientSecret: 'GOOGLE_CLIENT_SECRET',
    }),
  ],
};

Contributing 🙌🏽

Yes, I am willing to help answer this question in a PR

balazsorban44 commented 1 year ago

Technically you should not be using v4 yet: https://github.com/nextauthjs/next-auth/blob/1e6daa8304aa98858f8e27eef092fbeb2f34f436/packages/adapter-prisma/package.json#L40

Happy to add support though, so is this just a TS issue? :thinking:

kennykingdev commented 1 year ago

Yes.

stale[bot] commented 1 year ago

It looks like this issue did not receive any activity for 60 days. It will be closed in 7 days if no further activity occurs. If you think your issue is still relevant, commenting will keep it open. Thanks!

tacomanator commented 1 year ago

Temporary work around until supported:

import { PrismaClient } from "@prisma/client";
export const authOptions: NextAuthOptions = {
  adapter: PrismaAdapter(prisma as unknown as PrismaClient),
  ...
}

If necessary could replace PrismaClient with a type extracted from your extended client, as described in this article.

stale[bot] commented 1 year ago

It looks like this issue did not receive any activity for 60 days. It will be closed in 7 days if no further activity occurs. If you think your issue is still relevant, commenting will keep it open. Thanks!

stale[bot] commented 1 year ago

To keep things tidy, we are closing this issue for now. If you think your issue is still relevant, leave a comment and we might reopen it. Thanks!

benjamin-guibert commented 1 year ago

Any update on this?

alvesvaren commented 10 months ago

This is still an issue I think in the latest version of the adapter

mamlzy commented 7 months ago

still facing an error using prisma.$extends. can you help why this is happen? @balazsorban44 , thank you.

image

here is my code:

image

all of this working fine, if i'm not using prisma extension.

zhanlarr-ww commented 6 months ago

Fixed by https://github.com/nextauthjs/next-auth/pull/9798 it seems, for others who arrive here