next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

Kysely auth adapter

Open Adventune opened this issue 3 years ago • 8 comments

Adapter type

@auth/kysely-adapter

Environment

System: OS: Linux 6.2 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish) CPU: (4) x64 11th Gen Intel(R) Core(TM) i3-1115G4 @ 3.00GHz Memory: 579.13 MB / 7.50 GB Container: Yes Shell: 5.1.16 - /bin/bash Binaries: Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node Yarn: 1.22.19 - ~/.nvm/versions/node/v18.17.1/bin/yarn npm: 10.0.0 - ~/.nvm/versions/node/v18.17.1/bin/npm Browsers: Chromium: 116.0.5845.187

Reproduction URL

https://github.com/nextauthjs/next-auth-example/tree/main

Describe the issue

Using the documented code for kysely adapter causes errors with typing.

1. TS2345: Argument of type  KyselyAuth<Database, Database>  is not assignable to parameter of type  Kysely<Database> The types of  fn.agg  are incompatible between these types.

2. TS2322: Type import('.../project/node_modules/@auth/core/adapters').Adapter is not assignable to type import('.../project/node_modules/next-auth/adapters').Adapter

( 3*. TS2345: Argument of type import('.../project/node_modules/kysely/dist/esm/kysely').Kysely<import('.../project/packages/web/src/types/db').Database> is not assignable to parameter of type import('.../project/node_modules/kysely/dist/esm/kysely').Kysely<import('.../project/node_modules/@auth/kysely-adapter/index').Database> )

There seems to be some problems with mixing authjs and next-auth libraries.

*3rd error is when db = new KyselyAuth<Database>() in db.ts is swapped with db = new Kysely<Database>()

How to reproduce

  1. Init a new nextjs project
  2. Install next-auth
  3. Install dependencies from kysely adapter documentation
  4. Create app/api/auth/[...nextauth]/route.ts & types/db.d.ts and paste contents from adapter documentation (with slightly varying paths)
  5. Look at red squiggly lines

Expected behavior

No type errors

Adventune avatar Sep 19 '23 19:09 Adventune

I can confirm I am experiencing this as well.

robbyyt avatar Oct 10 '23 20:10 robbyyt

having the same error here

JannVazTor avatar Oct 12 '23 04:10 JannVazTor

I was experiencing the same issue and found the problem after digging into the source code of the adapter. There is a mismatch between the Database type in the docs and what the adapter accepts.

// adapter-kysely/index.ts export interface Database { User: { id: GeneratedAlways<string> name: string | null email: string emailVerified: Date | string | null image: string | null } Account: { id: GeneratedAlways<string> userId: string type: string provider: string providerAccountId: string refresh_token: string | null access_token: string | null expires_at: number | null token_type: string | null scope: string | null id_token: string | null session_state: string | null } Session: { id: GeneratedAlways<string> userId: string sessionToken: string expires: Date | string } VerificationToken: { identifier: string token: string expires: Date | string } }

Docs: interface Database { User: { id: GeneratedAlways<string> name: string | null email: string emailVerified: Date | null image: string | null } Account: { id: GeneratedAlways<string> userId: string type: string provider: string providerAccountId: string refresh_token: string | null access_token: string | null expires_at: number | null token_type: string | null scope: string | null id_token: string | null session_state: string | null } Session: { id: GeneratedAlways<string> userId: string sessionToken: string expires: Date } VerificationToken: { identifier: string token: string expires: Date } } see e. g. the expires columns. This would be an easy fix, but: the KyselyAdapter function doesn't accept any superset of this type, so if I want to add my own data, it throws an error, because it is accepting only Kysely<Database>. This is really annoying, and makes the adapter unusable, which is really depressing, because Kysely is awesome. I tried to fix this using various TypeScript hacks, like reusing the KyselyAuth class there, but none of them worked (they killed Kysely type checking). If anyone has more experience in TypeScript, I would appreciate their help and open a pull request for this. For now, I will go back to Drizzle...

bartaldavid avatar Oct 20 '23 11:10 bartaldavid

Having this issue as well.. Renders the Kysely adapter completely useless, I reverted back to utilizing prisma to handle this..

samuelho-dev avatar Nov 19 '23 21:11 samuelho-dev

I just typecasted the input as any for now, and kysely seems to work fine for me.

Multiply avatar Nov 20 '23 08:11 Multiply

I just typecasted the input as any for now, and kysely seems to work fine for me.

Yeah I added ts-ignore for it and that does work but it is unfortunate that it doesn't work with typing

Adventune avatar Nov 20 '23 16:11 Adventune

The actual package is a single index.ts file.

It's fairly easy to just copy that file from @auth/kysely-adapter/src/index.ts into project.

I found this great since I can customize the adapter function. It's a great starting template to get a really customized adapter.

kevin-griley avatar Nov 21 '23 20:11 kevin-griley

I'm going to close this as nobody in the issue thread added an actual reproduction. Please open one with a code example we can investigate, thanks!

balazsorban44 avatar Dec 10 '23 03:12 balazsorban44