You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A prominent documented example implies that the auth member returned by NextAuth has compatible signature with NextMiddleware
This seems to exploit lack of adequate typing in NextJS paradigm
Actually attempting to use auth in a type-safe way reveals it has no compatible signature
The example code below is offered right on the frontpage as documentation for Auth.js but seems not to be type-safe. It exploits the fact that NextJS is poorly typed through automatic loading of named files with conventional exports having no typing.
This is concerning because it does not ensure that the code paths in auth can actually process the arguments passed from middleware, and even if they handle them by good luck now, this is not being guaranteed by typescript for the future.
At the time of writing the frontpage shows this...
If you transform the middleware implementation to its type-safe equivalent, the code no longer compiles, because the auth member returned by NextAuth doesn't have a compatible signature with NextMiddleware ...
The error reported against the invocation of auth(...params) is...
Argument of type 'NextRequest' is not assignable to parameter of type 'NextApiRequest'.
Type 'NextRequest' is missing the following properties from type 'NextApiRequest': query, env, aborted, httpVersion, and 67 more.ts(2345)
This makes me concerned that the auth signature cannot be reliably used without error in app routes, only API routes. Is that true?
For context below you can see the interactive screenshot from my own codebase ( note: in my codebase the auth.ts file is under /lib ).
Non-Goals
I am not proposing a change to behaviour, only to typings for code quality, developer experience and future type-safety.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
auth
member returned byNextAuth
has compatible signature withNextMiddleware
auth
in a type-safe way reveals it has no compatible signatureThe example code below is offered right on the frontpage as documentation for Auth.js but seems not to be type-safe. It exploits the fact that NextJS is poorly typed through automatic loading of named files with conventional exports having no typing.
This is concerning because it does not ensure that the code paths in
auth
can actually process the arguments passed from middleware, and even if they handle them by good luck now, this is not being guaranteed by typescript for the future.At the time of writing the frontpage shows this...
If you transform the middleware implementation to its type-safe equivalent, the code no longer compiles, because the
auth
member returned byNextAuth
doesn't have a compatible signature withNextMiddleware
...The error reported against the invocation of
auth(...params)
is...This makes me concerned that the auth signature cannot be reliably used without error in app routes, only API routes. Is that true?
For context below you can see the interactive screenshot from my own codebase ( note: in my codebase the
auth.ts
file is under/lib
).Non-Goals
Background
NextMiddleware is presented as the correct typing for NextJS middleware
Proposal
An overload for the
auth
member should be added, aligned withNextMiddleware
Beta Was this translation helpful? Give feedback.
All reactions