nextauthjs / next-auth

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

prisma adapter types error #6106

Closed OrJDev closed 1 year ago

OrJDev commented 1 year ago

Adapter type

@next-auth/prisma-adapter

Environment

System: OS: Windows 10 10.0.19045 Memory: 991.97 MB / 5.95 GB Binaries: Node: 16.13.1 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.17 - C:\Program Files\nodejs\yarn.CMD npm: 8.16.0 - C:\Program Files\nodejs\npm.CMD Browsers: Edge: Spartan (44.19041.1266.0), Chromium (108.0.1462.54)
Internet Explorer: 11.0.19041.1566

npmPackages: @next-auth/prisma-adapter: ^1.0.5 => 1.0.5

Reproduction URL

https://github.com/OrJDev/nextauth-error-repro

Describe the issue

Type 'Adapter<boolean>' is not assignable to type 'Adapter<boolean> | undefined'.
  Type 'DefaultAdapter' is not assignable to type 'Adapter<boolean> | undefined'.
    Type 'DefaultAdapter' is not assignable to type 'DefaultAdapter & { createVerificationToken: (verificationToken: VerificationToken) => Awaitable<VerificationToken | null | undefined>; useVerificationToken: (params: { ...; }) => Awaitable<...>; }'.
      Type 'import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/next-auth/adapters").DefaultAdapter' is not assignable to type 'import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/@auth/core/adapters").DefaultAdapter'.
        Types of property 'linkAccount' are incompatible.
          Type '(account: import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/next-auth/adapters").AdapterAccount) => Promise<void> | import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/next-auth/core/types").Awaitable<import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/next-auth/adapters").Adapt...' is not assignable to type '(account: import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/@auth/core/adapters").AdapterAccount) => Promise<void> | import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/@auth/core/lib/types").Awaitable<import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/@auth/core/adapters").Ada...'.
            Types of parameters 'account' and 'account' are incompatible.
              Type 'import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/@auth/core/adapters").AdapterAccount' is not assignable to type 'import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/next-auth/adapters").AdapterAccount'.
                Types of property 'type' are incompatible.
                  Type 'import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/@auth/core/providers/index").ProviderType' is not assignable to type 'import("c:/Users/\u05D0\u05D5\u05E8/Desktop/my-app/node_modules/next-auth/providers/index").ProviderType'.
                    Type '"oidc"' is not assignable to type 'ProviderType'.ts(2322)

How to reproduce

run

npm create jd-app@latest

select prisma and nextauth go to src/routes/api/auth/[...solidauth].ts the error will be there

Expected behavior

shouldn't throw any type errors with a normal prisma client (no extension)

L-Mario564 commented 1 year ago

Had this same issue in a SvelteKit project. Type assertion worked for me and didn't get errors on runtime, should be a good temporary solution.

import SvelteKitAuth from '@auth/sveltekit'; // Using SvelteKit as an example
import type { Adapter } from '@auth/core/adapters';

export const handle = SvelteKitAuth({
  adapter: PrismaAdapter(prismaClient) as Adapter<boolean>,
  providers: [
    // ...
  ]
});
califlower commented 1 year ago

Had this same issue in a SvelteKit project. Type assertion worked for me and didn't get errors on runtime, should be a good temporary solution.

import SvelteKitAuth from '@auth/sveltekit'; // Using SvelteKit as an example
import type { Adapter } from '@auth/core/adapters';

export const handle = SvelteKitAuth({
  adapter: PrismaAdapter(prismaClient) as Adapter<boolean>,
  providers: [
    // ...
  ]
});

this works for now

kalm42 commented 1 year ago

I got the feedback that Type 'Adapter' is not generic.

export const handle = SvelteKitAuth({
    adapter: PrismaAdapter(prisma) as Adapter,
    providers: [
        ...
    ]
});
ryoppippi commented 1 year ago

@kalm42 me too

TurtIeSocks commented 1 year ago

+1

YuraD commented 1 year ago

try this

import type { Adapter } from 'next-auth/adapters';
...
adapter: TypeORMAdapter(connection) as Adapter,

P.S. No matter which adapter you have, it should help with every adapters

ShriPunta commented 1 year ago

Hi @YuraD can you please explain with more context. I am not able to understand what is connection here? Also what do you mean by TypeORMAdapter? where can I import this function?


I am errors in the Prisma adapter index.ts

One of the errors:

Type '(email: string) => Prisma__UserClient<User | null, null>' is not assignable to type '(email: string) => Awaitable<AdapterUser | null>'.
  Type 'Prisma__UserClient<User | null, null>' is not assignable to type 'Awaitable<AdapterUser | null>'.

image

verdverm commented 1 year ago

@ShriPunta this is more about the Adapter and typescript types I think. Adding the Adapter worked for prisma as well

import type { Adapter } from 'next-auth/adapters';

...

export const authConfig: NextAuthOptions = {
    adapter: PrismaAdapter(db) as Adapter,

...

TypeORMAdapter is a sibling or alternative to PrismaAdapter, both are one of the many adapters for next-auth

bottercode commented 1 year ago

image got no issues in this case

MaKTaiL commented 1 year ago

Which Schema should I use with "@next-auth/prisma-adapter"? The one shown in the v3 version or the one shown for "@auth/prisma-adapter"?

next-auth v3: https://next-auth.js.org/v3/adapters/prisma auth: https://authjs.dev/reference/adapter/prisma

LeonardoTrapani commented 1 year ago

Looks like the module @next-auth/adapters is no longer existing, so the only solution is using @next-auth/prisma-adapter. I would suggest fixing the docs, or fixing the package

OrJDev commented 1 year ago

use @auth/prisma-adapter to solve this issue

Puetz commented 1 year ago

Why was this issue closed? I just set up my Next JS project with

"@auth/prisma-adapter": "^1.0.1"
"next-auth": "^4.22.5"

and I'm having the exact same problems as everyone mentioned.
So I installed "@next-auth/prisma-adapter": "^1.0.7" which works for me.

Please fix either your documentation about the prisma adapter or fix the type error for the newest versions of next-auth and @auth/prisma-adapter.

nickjanssen commented 1 year ago

@Puetz as @next-auth is becoming @auth that's probably not the right way.

@OrJDev please re-open as the problem still persists, even with latest @auth/prisma-adapter.

adammetal commented 1 year ago

Why was this issue closed? I just set up my Next JS project with

"@auth/prisma-adapter": "^1.0.1"
"next-auth": "^4.22.5"

and I'm having the exact same problems as everyone mentioned. So I installed "@next-auth/prisma-adapter": "^1.0.7" which works for me.

Please fix either your documentation about the prisma adapter or fix the type error for the newest versions of next-auth and @auth/prisma-adapter.

This fixed my problem also. I think they should update a documentation a bit. Maybe we can help them? Open a pr? Because right now i feel a bit lost.

maltesa commented 1 year ago

I can confirm that I'm still having type errors with the latest versions:

Using this Schema.

    "@auth/prisma-adapter": "^1.0.1",
    "next-auth": "^4.23.1",
export const authOptions: NextAuthOptions = {
  adapter: PrismaAdapter(db),
// ^~~~~   TypeError           
  providers: [getEmailProvider()],
//...
}

Error:

ype 'import("/workspace/node_modules/.pnpm/@auth+core@0.9.0/node_modules/@auth/core/adapters").Adapter' is not assignable to type 'import("/workspace/node_modules/.pnpm/next-auth@4.23.1_next@13.4.12_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/adapters").Adapter'.
  Types of property 'linkAccount' are incompatible.
    Type '((account: import("/workspace/node_modules/.pnpm/@auth+core@0.9.0/node_modules/@auth/core/adapters").AdapterAccount) => Promise<void> | import("/workspace/node_modules/.pnpm/@auth+core@0.9.0/node_modules/@auth/core/types").Awaitable<import("/workspace/node_modules/.pnpm/@auth+core@0.9.0/node_modules/@auth/core/adapt...' is not assignable to type '((account: import("/workspace/node_modules/.pnpm/next-auth@4.23.1_next@13.4.12_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/adapters").AdapterAccount) => Promise<...> | import("/workspace/node_modules/.pnpm/next-auth@4.23.1_next@13.4.12_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/core/types").Awaita...'.
      Type '(account: import("/workspace/node_modules/.pnpm/@auth+core@0.9.0/node_modules/@auth/core/adapters").AdapterAccount) => Promise<void> | import("/workspace/node_modules/.pnpm/@auth+core@0.9.0/node_modules/@auth/core/types").Awaitable<import("/workspace/node_modules/.pnpm/@auth+core@0.9.0/node_modules/@auth/core/adapte...' is not assignable to type '(account: import("/workspace/node_modules/.pnpm/next-auth@4.23.1_next@13.4.12_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/adapters").AdapterAccount) => Promise<...> | import("/workspace/node_modules/.pnpm/next-auth@4.23.1_next@13.4.12_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/core/types").Awaitab...'.
        Types of parameters 'account' and 'account' are incompatible.
          Type 'import("/workspace/node_modules/.pnpm/next-auth@4.23.1_next@13.4.12_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/adapters").AdapterAccount' is not assignable to type 'import("/workspace/node_modules/.pnpm/@auth+core@0.9.0/node_modules/@auth/core/adapters").AdapterAccount'.
            Types of property 'type' are incompatible.
              Type 'ProviderType' is not assignable to type '"email" | "oidc" | "oauth"'.
                Type '"credentials"' is not assignable to type '"email" | "oidc" | "oauth"'.ts(2322)
types.d.ts(106, 5): The expected type comes from property 'adapter' which is declared here on type 'AuthOptions'

No errors when using latest @next-auth/prisma-adapter

FabioCingottini commented 9 months ago

Not sure if it can help someone, but I solved it by using:

import { Adapter } from 'next-auth/adapters';
import { PrismaAdapter } from '@auth/prisma-adapter';

const nextAuthOptions = {
  ...,
  adapter: <Adapter>PrismaAdapter(prismaClient),
  ...,

With versions:

...
"next-auth": "^4.24.5",
"@auth/prisma-adapter": "^1.3.3",
...
AlfredMadere commented 3 months ago

casting the adapter to Adapter is still necessary to avoid type errors with

    "@auth/prisma-adapter": "^2.4.2",
    "next-auth": "5.0.0-beta.4",
baitangxiaoyu commented 3 months ago
"@auth/prisma-adapter": "^2.4.2",
"next-auth": "5.0.0-beta.4",

image If I adopt this approach, there will be no problem.

pantharshit007 commented 1 month ago
image

getting this error. The only workaround I can find right now is adding type: module

EDIT: I am using turbo repo so it was a bit hard but for me, the culprit was tsconfig.json

added this:

  "extends": "@repo/typescript-config/react-library.json",
  "compilerOptions": {
    "outDir": "dist",
    "module": "ESNext",           // Added to support ES Modules
    "moduleResolution": "Node",   // Added to resolve imports correctly
    "esModuleInterop": true      // Added to enable ES Module interop
  },
  "include": ["src"],
  "exclude": ["node_modules", "dist"]
}