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

Types of property 'createUser' are incompatible

Open Master-Guy opened this issue 2 years ago • 21 comments

Environment

System: OS: Windows 11 10.0.22000 CPU: (32) x64 AMD Ryzen 9 3950X 16-Core Processor Memory: 88.88 GB / 127.94 GB Binaries: Node: 20.6.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.19 - C:\Program Files\nodejs\yarn.CMD npm: 10.2.5 - C:\Program Files\nodejs\npm.CMD pnpm: 8.13.1 - C:\Program Files\nodejs\pnpm.CMD Browsers: Edge: Spartan (44.22000.120.0), Chromium (120.0.2210.91) Internet Explorer: 11.0.22000.120 npmPackages: @auth/core: ^0.19.0 => 0.19.0 @auth/sequelize-adapter: ^1.0.12 => 1.0.12 next: ^14.0.4 => 14.0.4 next-auth: ^4.24.5 => 4.24.5 react: ^18.2.0 => 18.2.0

Reproduction URL

https://github.com/Master-Guy/next-auth-issue

Describe the issue

After upgrading to the latest version of @auth-sequelize TypeScript is complaining about the Adapter not being correct anymore. The code has not been changed at all. Even though TypeScript shows an error, node dev runs without issues and I don't (yet) experience any problems on my project.

[{
	"resource": "/f:/Programming/Github/Navis/src/pages/api/auth/[...nextauth].tsx",
	"owner": "typescript",
	"code": "2322",
	"severity": 8,
	"message": "Type 'import(\"f:/Programming/Github/Navis/node_modules/@auth/core/adapters\").Adapter' is not assignable to type 'import(\"f:/Programming/Github/Navis/node_modules/next-auth/adapters\").Adapter'.\n  Types of property 'createUser' are incompatible.\n    Type '((user: AdapterUser) => Awaitable<AdapterUser>) | undefined' is not assignable to type '((user: Omit<AdapterUser, \"id\">) => Awaitable<AdapterUser>) | undefined'.\n      Type '(user: AdapterUser) => Awaitable<AdapterUser>' is not assignable to type '(user: Omit<AdapterUser, \"id\">) => Awaitable<AdapterUser>'.\n        Types of parameters 'user' and 'user' are incompatible.\n          Property 'id' is missing in type 'Omit<AdapterUser, \"id\">' but required in type 'AdapterUser'.",
	"source": "ts",
	"startLineNumber": 93,
	"startColumn": 2,
	"endLineNumber": 93,
	"endColumn": 9,
	"relatedInformation": [
		{
			"startLineNumber": 174,
			"startColumn": 5,
			"endLineNumber": 174,
			"endColumn": 7,
			"message": "'id' is declared here.",
			"resource": "/f:/Programming/Github/Navis/node_modules/@auth/core/adapters.d.ts"
		},
		{
			"startLineNumber": 106,
			"startColumn": 5,
			"endLineNumber": 106,
			"endColumn": 12,
			"message": "The expected type comes from property 'adapter' which is declared here on type 'AuthOptions'",
			"resource": "/f:/Programming/Github/Navis/node_modules/next-auth/core/types.d.ts"
		}
	]
}]

How to reproduce

See repo

Expected behavior

No TypeScript errors

Master-Guy avatar Dec 28 '23 13:12 Master-Guy

Same thing happening with @auth/[email protected] and [email protected].

PabloRamirezDev avatar Dec 28 '23 20:12 PabloRamirezDev

In the meanwhile you can infer the Adapter type from next-auth/adapters

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

[...]
adapter: PrismaAdapter(prisma) as Adapter

bayasdev avatar Dec 28 '23 22:12 bayasdev

The type of Adapter seems to be different for [email protected] and @auth/[email protected].

[email protected]:

createUser?: (user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>

@auth/[email protected]:

createUser?(user: AdapterUser): Awaitable<AdapterUser>

User ID is generated in the adapter, so I think it is appropriate to omit it from the argument type.

nbifrye avatar Dec 30 '23 18:12 nbifrye

👀 https://github.com/nextauthjs/next-auth/pull/9380 and https://github.com/nextauthjs/next-auth/pull/9381

nbifrye avatar Dec 30 '23 18:12 nbifrye

In the meanwhile you can infer the Adapter type from next-auth/adapters

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

[...]
adapter: PrismaAdapter(prisma) as Adapter

This works by declaring the types on the Mongodb Adapter. However, agree with @nbifrye that id should be omitted for MongoDB specifically

tyeetale avatar Dec 30 '23 18:12 tyeetale

Same with the drizzle adapters. Had to cast the DrizzleAdapter from "@auth/drizzle-adapter" as an Adapter from "next-auth/adapters";

AlexanderHott avatar Dec 31 '23 23:12 AlexanderHott

Looks like v2.0.10 ~ v2.0.12 have this issue.

RaenonX avatar Jan 07 '24 08:01 RaenonX

Same thing happening with PrismaAdapter.

Screenshot from 2024-01-08 01-02-35

import type { Adapter } from "next-auth/adapters"; Fixes the issue for now.

In the meanwhile you can infer the Adapter type from next-auth/adapters

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

[...]
adapter: PrismaAdapter(prisma) as Adapter

somu-code avatar Jan 08 '24 06:01 somu-code

Same thing happening with PrismaAdapter.

Screenshot from 2024-01-08 01-02-35

import type { Adapter } from "next-auth/adapters"; Fixes the issue for now.

In the meanwhile you can infer the Adapter type from next-auth/adapters

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

[...]
adapter: PrismaAdapter(prisma) as Adapter

I used this but Now I'm facing an error of session: [next-auth][error][adapter_error_getSessionAndUser] https://next-auth.js.org/errors#adapter_error_getsessionanduser Invalid prisma.session.findUnique() invocation:

Error occurred during query execution: ConnectorError(ConnectorError { user_facing_error: None, kind: AuthenticationFailed { user: "SCRAM failure: bad auth : authentication failed" }, transient: false }) { message: '\n' + 'Invalid prisma.session.findUnique() invocation:\n' + '\n' + '\n' + 'Error occurred during query execution:\n' +
'ConnectorError(ConnectorError { user_facing_error: None, kind: AuthenticationFailed { user: "SCRAM failure: bad auth : authentication failed" }, transient: false })', stack: 'PrismaClientUnknownRequestError: \n' +
'Invalid prisma.session.findUnique() invocation:\n' + '\n' + '\n' + 'Error occurred during query execution:\n' +
'ConnectorError(ConnectorError { user_facing_error: None, kind: AuthenticationFailed { user: "SCRAM failure: bad auth : authentication failed" }, transient: false })\n' + ' at si.handleRequestError (C:\Users\pedro\.vscode\learning projects\projects\technews\technews\node_modules\@prisma\client\runtime\library.js:125:7007)\n' + ' at si.handleAndLogRequestError (C:\Users\pedro\.vscode\learning projects\projects\technews\technews\node_modules\@prisma\client\runtime\library.js:125:6151)\n' + ' at si.request (C:\Users\pedro\.vscode\learning projects\projects\technews\technews\node_modules\@prisma\client\runtime\library.js:125:5859)\n' + ' at async l (C:\Users\pedro\.vscode\learning projects\projects\technews\technews\node_modules\@prisma\client\runtime\library.js:130:9805)\n' + ' at async getSessionAndUser (webpack-internal:///(rsc)/./node_modules/@auth/prisma-adapter/index.js:250:36)', name: 'PrismaClientUnknownRequestError' } [next-auth][error][SESSION_ERROR] https://next-auth.js.org/errors#session_error
Invalid prisma.session.findUnique() invocation:

Error occurred during query execution: ConnectorError(ConnectorError { user_facing_error: None, kind: AuthenticationFailed { user: "SCRAM failure: bad auth : authentication failed" }, transient: false }) PrismaClientUnknownRequestError: Invalid prisma.session.findUnique() invocation:

Error occurred during query execution: ConnectorError(ConnectorError { user_facing_error: None, kind: AuthenticationFailed { user: "SCRAM failure: bad auth : authentication failed" }, transient: false }) at si.handleRequestError (C:\Users\pedro.vscode\learning projects\projects\technews\technews\node_modules@prisma\client\runtime\library.js:125:7007)
at si.handleAndLogRequestError (C:\Users\pedro.vscode\learning projects\projects\technews\technews\node_modules@prisma\client\runtime\library.js:125:6151) at si.request (C:\Users\pedro.vscode\learning projects\projects\technews\technews\node_modules@prisma\client\runtime\library.js:125:5859) time\library.js:130:9805) time\library.js:130:9805)
at async getSessionAndUser (webpack-internal:///(rsc)/./node_modules/@auth/prisma-adapter/index.js:250:36) { name: 'GetSessionAndUserError', code: undefined }

Did anyone have the same problem?

PedroJacobCard avatar Jan 17 '24 20:01 PedroJacobCard

Hi everyone, just wanted to let you know, this is known.

It's related to #9380, after which the types in @auth/core/adapters are slightly different than what next-auth v4 expects. Since @auth/*-adapters are designed in a way to keep working with next-auth v4, you are seeing this error now (type only!). However the actual implementation of the adapters have not changed yet (#9381), it is technically safe to ignore the type error by either of these:

  1. // @ts-expect-error for now
  2. downgrade your adapter to a lower version, until this error goes away

[!NOTE] Nothing has changed in the adapter implementation and stayed the same for years, and #9380 is actually an attempt to make it even smoother to create new adapters, by delegating the repetitive task of creating the user id to the core library instead (see #9381)

The real solution to this issue would be to backport #9380 and #9793 to v4 https://github.com/nextauthjs/next-auth/tree/v4/packages/next-auth, so the old Adapter type

https://github.com/nextauthjs/next-auth/blob/9296222ffdf56d0f04d08593161d3fe26b0f308b/packages/next-auth/src/adapters.ts#L63

can match up with the new one:

https://github.com/nextauthjs/next-auth/blob/f5fb578f0b324af9cb392e7aa8e8bfbf0d90e0ce/packages/core/src/adapters.ts#L271

Until that is done, #9381 is pending and won't make changes to any of the adapters to keep things run smoothly.

If anyone is up for making a PR to v4, I'm happy to review!

balazsorban44 avatar Jan 27 '24 22:01 balazsorban44

This issue was marked with the good first issue label by a maintainer.

This means that it is a good candidate for someone interested in contributing to the project, but does not know where to start.

Have a look at the Contributing Guide first.

This will help you set up your development environment to get started. When you are ready, open a PR, and link back to this issue in the form of adding Fixes #1234 to the PR description, where 1234 is the issue number. This will auto-close the issue when the PR gets merged, making it easier for us to keep track of what has been fixed.

Please make sure that - if applicable - you add tests for the changes you make.

If you have any questions, feel free to ask in the comments below or the PR. Generally, you don't need to @mention anyone directly, as we will get notified anyway and will respond as soon as we can)

[!NOTE]
There is no need to ask for permission "can I work on this?" Please, go ahead if there is no linked PR :slightly_smiling_face:

github-actions[bot] avatar Jan 27 '24 22:01 github-actions[bot]

Same with Drizzle Adapter Seems like all adapter types are somehow broken. Is there a reason to omit the ids that I am not able to understand?

spiritanand avatar Feb 20 '24 18:02 spiritanand

this appears to still be an issue on 4.24.7, are there any PR's in the pipeline to address the issue or are you accepting new contributions?

ProfXponent avatar Mar 22 '24 11:03 ProfXponent

In the meanwhile you can infer the Adapter type from next-auth/adapters

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

[...]
adapter: PrismaAdapter(prisma) as Adapter

Thanks! this fixed the issue, I hope something is done about it soon.

okoraretega avatar Apr 04 '24 14:04 okoraretega

This results in very bad user experience for a new developer, since they literally copy and paste the code from the documentation and it results in an obscure type error. This should get fixed ASAP.

bkeroack avatar Apr 04 '24 17:04 bkeroack

This results in very bad user experience for a new developer, since they literally copy and paste the code from the documentation and it results in an obscure type error. This should get fixed ASAP.

agreed, type incompatibilities between internal libraries is not a great look while evaluating an authentication provider

ProfXponent avatar Apr 05 '24 08:04 ProfXponent

In the meanwhile you can infer the Adapter type from next-auth/adapters

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

[...]
adapter: PrismaAdapter(prisma) as Adapter

While the code itself technically works, I am getting this type error:

image

Deliaz avatar Jul 24 '24 10:07 Deliaz

Hi folks! A long overdue update. I've been able to get around looking more into this, and I created a fix, that should solve the issue. If anyone could test out

pnpm add [email protected] (or your package manager of liking), that would be very helpful. Once confirmed, I can release a fix to next-auth@latest as well!

Note, in VSCode, yiu might need to restart the TypeScript server for this to take affect.

balazsorban44 avatar Aug 10 '24 16:08 balazsorban44

Hi folks! A long overdue update. I've been able to get around looking more into this, and I created a fix, that should solve the issue. If anyone could test out

pnpm add [email protected] (or your package manager of liking), that would be very helpful. Once confirmed, I can release a fix to next-auth@latest as well!

Note, in VSCode, yiu might need to restart the TypeScript server for this to take affect.

is this already on the current release or should we test against this PR?

ProfXponent avatar Aug 11 '24 06:08 ProfXponent

It's pnpm add [email protected]

balazsorban44 avatar Aug 11 '24 08:08 balazsorban44

It's pnpm add [email protected]

I should probably learn how to read at some point...

This resolved the type errors for my use case (hasura adapter) and seems to be working as intended.

Thanks for putting in the time to get this one out.

ProfXponent avatar Aug 11 '24 11:08 ProfXponent

This should be out on 4.24.8, please upgrade

balazsorban44 avatar Sep 25 '24 15:09 balazsorban44

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

This worked for me, thanks! Still on latest version this happens

samuelpalacioss avatar Feb 16 '25 02:02 samuelpalacioss

Still happens on "next-auth": "5.0.0-beta.20" Use the same fix as above

Edit by maintainer bot: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

SamHendry avatar Apr 08 '25 17:04 SamHendry