nextauthjs / next-auth

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

@next-auth/drizzle-adapter is not available on npm #8280

Closed SSardorf closed 1 year ago

SSardorf commented 1 year ago

Question 💬

@next-auth/drizzle-adapter is not available on npm, resulting in us having to use the Authjs adapter.

adapter: DrizzleAdapter(db) Adding the authjs drizzle adapter to a Nextjs project throws the following typescript error:

Types of property 'createUser' are incompatible.
    Type '((user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>) | undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.
      Type 'undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.ts(2322)

How to reproduce ☕️

  1. Import the package import { DrizzleAdapter } from '@auth/drizzle-adapter'

  2. Add the adapter to the authOptions (with type NextAuthOptions)

  3. Error:

    Types of property 'createUser' are incompatible.
    Type '((user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>) | undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.
      Type 'undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.ts(2322)

Contributing 🙌🏽

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

balazsorban44 commented 1 year ago

@auth/drizzle-adapter should work for both libraries. Make sure you are on the latest of next-auth.

The type mismatch was fixed in 4.22.5 https://github.com/nextauthjs/next-auth/commit/3b0128c3cabfaa014531e38e06919ea435df35fd

SSardorf commented 1 year ago

@balazsorban44 I regret to say that there's still an issue here. Even with 4.22.5, I am now getting the following error,

Type 'import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/@auth+core@0.10.1_nodemailer@6.9.4/node_modules/@auth/core/adapters").Adapter' is not assignable to type 'import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/next-auth@4.22.5_next@13.4.13-canary.11_nodemailer@6.9.4_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/adapters").Adapter'.
  Types of property 'linkAccount' are incompatible.
    Type '((account: import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/@auth+core@0.10.1_nodemailer@6.9.4/node_modules/@auth/core/adapters").AdapterAccount) => Promise<void> | import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/@auth+core@0.10.1_nodemailer@6.9.4/node_modules/@auth/core/types").Awaitable<...>) ...' is not assignable to type '((account: import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/next-auth@4.22.5_next@13.4.13-canary.11_nodemailer@6.9.4_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/adapters").AdapterAccount) => Promise<...> | import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/next-auth@4.22.5_next@13.4.13-can...'.
      Type '(account: import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/@auth+core@0.10.1_nodemailer@6.9.4/node_modules/@auth/core/adapters").AdapterAccount) => Promise<void> | import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/@auth+core@0.10.1_nodemailer@6.9.4/node_modules/@auth/core/types").Awaitable<...>' is not assignable to type '(account: import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/next-auth@4.22.5_next@13.4.13-canary.11_nodemailer@6.9.4_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/adapters").AdapterAccount) => Promise<...> | import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/next-auth@4.22.5_next@13.4.13-cana...'.
        Types of parameters 'account' and 'account' are incompatible.
          Type 'import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/next-auth@4.22.5_next@13.4.13-canary.11_nodemailer@6.9.4_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/adapters").AdapterAccount' is not assignable to type 'import("/Users/ssardorf/dev/12s12m/verk/node_modules/.pnpm/@auth+core@0.10.1_nodemailer@6.9.4/node_modules/@auth/core/adapters").AdapterAccount'.
            Types of property 'type' are incompatible.
              Type 'ProviderType' is not assignable to type '"oauth" | "email" | "oidc"'.
                Type '"credentials"' is not assignable to type '"oauth" | "email" | "oidc"'.ts(2322)
types.d.ts(106, 5): The expected type comes from property 'adapter' which is declared here on type 'AuthOptions'

due to an incongruency between next-auth/src/adapter.ts (https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/adapters.ts#L9-L11) and core/src/adapter.ts (https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/adapters.ts#L142-L154)

williamlmao commented 1 year ago

@balazsorban44 I am also having the same issue

SnekCode commented 1 year ago

I've gone down this rabbit hole today trying to implement my own custom Adapter for next auth. Ran into this exact issue and @balazsorban44's fix then his issue again. Same error messages and all. What timing!

I was able to side step the type error by defining the type of Adapter as type Adapter = NextAuthOptions['adapter']. This works because I'm not using the Adapter type from @auth/core but rather getting that type directly from next-auth.

I hope this helps someone!

doneumark commented 11 months ago

having the same issue :(

dmythro commented 10 months ago

How was it closed? I have the same issue here:

{
  "@auth/drizzle-adapter": "0.3.12",
  "next-auth": "4.24.5",
}

With "@auth/drizzle-adapter": "0.3.9" is was working, but I'm not sure if there are important fixes.

mikelong10 commented 7 months ago

Hi @balazsorban44 👋 ,

As others have echoed here, I am still unfortunately running into this issue. I'm trying to transition from a Prisma + PlanetScale db setup to a Drizzle + Turso db setup. My next-auth was working great with the Prisma adapter, but I'm having type errors with the Drizzle adapter, as pictured in the screenshot below.

FYI, I'm on the latest versions as of right now I believe: next-auth@4.24.7 and @auth/drizzle-adapter@0.8.1.

image

adriangalilea commented 7 months ago

Hi @balazsorban44 👋 ,

As others have echoed here, I am still unfortunately running into this issue. I'm trying to transition from a Prisma + PlanetScale db setup to a Drizzle + Turso db setup. My next-auth was working great with the Prisma adapter, but I'm having type errors with the Drizzle adapter, as pictured in the screenshot below.

FYI, I'm on the latest versions as of right now I believe: next-auth@4.24.7 and @auth/drizzle-adapter@0.8.1.

image

Having the exact same issue and the exact same transition, from prisma + planetscale to drizzle + turso

MazenSenih commented 6 months ago

@adriangalilea I'm having the same issue as well..

adriangalilea commented 6 months ago

@adriangalilea I'm having the same issue as well..

I fixed it by making my own custom adapter if I remember correctly.

You can take a look at my repo adriangalilea/e-id

It's not exactly this project that I was using in fact is much more simple, I think I only needed to alter the create user function, take a look if something helps.

mikelong10 commented 6 months ago

@adriangalilea @MazenSenih

so, i actually ended up figuring this out but forgot to post back here, sorry y'all!

the fix i found was actually to use the 5.0.0 beta version of "next-auth" along with the new core and drizzle-adapter packages from "@auth".

from my package.json:

(i believe next-auth 5.00-beta.17 is out now, but it wasn't back when i was working on this. i'd assume 17 would work as well, but up to your discretion obviously. same goes for the other 2)

...and this is my auth.ts file where i initialize my NextAuth for reference, and the type error is gone!:

import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { db } from "@db";
import NextAuth from "next-auth";
import FacebookProvider from "next-auth/providers/facebook";
import GoogleProvider from "next-auth/providers/google";
import EmailProvider from "next-auth/providers/nodemailer";

export const {
  handlers: { GET, POST },
  auth,
} = NextAuth({
  adapter: DrizzleAdapter(db),
  pages: {
    signIn: "/login",
    error: "/login/error",
    verifyRequest: "/login/verify",
    newUser: "/profile",
  },
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID!,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
    }),
    FacebookProvider({
      clientId: process.env.FACEBOOK_CLIENT_ID!,
      clientSecret: process.env.FACEBOOK_CLIENT_SECRET!,
    }),
    EmailProvider({
      server: {
        host: process.env.EMAIL_SERVER_HOST,
        port: Number(process.env.EMAIL_SERVER_PORT),
        auth: {
          user: process.env.EMAIL_SERVER_USER,
          pass: process.env.EMAIL_SERVER_PASSWORD,
        },
      },
      from: process.env.EMAIL_LOGIN_FROM,
    }),
  ],
});
pkarw commented 3 months ago

Thanks @mikelong10 you just saved me few hours of precious life fixing this issue ;)