Support Route Handlers in the `app` folder of Nextjs 13
Description 📓
TL;DR
Need support for setting up NextAuth in route handler.
Details
As API routes being replaced by Route handlers (https://beta.nextjs.org/docs/routing/route-handlers) NextAuth need to support running in the route handler.
And documentation need to provide an example how to setup the nextjs authentication with NextAuth that uses route handler instead of the API route pages/api/auth/[...nextauth].ts
How to reproduce ☕️
N/A
Contributing 🙌🏽
No, I am afraid I cannot help regarding this
There is already a PR for it, and you can find an experimental version here https://github.com/nextauthjs/next-auth/pull/6777#issuecomment-1439312304 to try it out!
Note, It's more of an incremental/gradual migration over a longer time period, API Routes are not "replaced".
There is already a PR for it, and you can find an experimental version here #6777 (comment) to try it out!
Note, It's more of an incremental/gradual migration over a longer time period, API Routes are not "replaced".
This is great! Do you know when (approximately) this will make it to the next official release?
I would like to get this released maybe next week, if not sooner.
@balazsorban44 I tried the latest code from https://github.com/nextauthjs/next-auth/pull/6777#issuecomment-1439312304 today. CORS issue is gone but now I am getting this:
[next-auth][error][OAUTH_CALLBACK_ERROR]
https://next-auth.js.org/errors#oauth_callback_error checks.state argument is missing {
error: TypeError: checks.state argument is missing
at Client.callback (webpack-internal:///(sc_server)/./node_modules/openid-client/lib/client.js:321:19)
at oAuthCallback (webpack-internal:///(sc_server)/./node_modules/next-auth/core/lib/oauth/callback.js:95:35)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Object.callback (webpack-internal:///(sc_server)/./node_modules/next-auth/core/routes/callback.js:18:83)
at async AuthHandler (webpack-internal:///(sc_server)/./node_modules/next-auth/core/index.js:198:38)
at async NextAuthRouteHandler (webpack-internal:///(sc_server)/./node_modules/next-auth/next/index.js:51:30)
at async eval (webpack-internal:///(sc_server)/./node_modules/next-auth/next/index.js:85:24)
at async AppRouteRouteHandler.execute (~/nextjs-temp-prep/node_modules/next/dist/server/future/route-handlers/app-route-route-handler.js:214:26)
name: 'OAuthCallbackError',
code: undefined
},
providerId: 'google',
message: 'checks.state argument is missing'
I get the google's consent screen first, and after I give my consent I get redirected to the callback but with the error.
http://localhost:3000/auth/signin?callbackUrl=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fsignin&error=OAuthCallback
Not quite sure what this is.
Same as @benderillo message: 'checks.state argument is missing' using the DiscordProvider also.
at oAuthCallback (webpack-internal:///(sc_server)/./node_modules/next-auth/core/lib/oauth/callback.js:95:35)
Same here using DiscordProvider
checks.state argument is missing
Small blocker I encountered, there seems to be an issue with setting multiple cookies. Currently, I suspect it's in Next.js, investigating. Related: https://github.com/vercel/next.js/issues/38302#issuecomment-1445278435
All of your reports are likely related to that.
@balazsorban44 Thanks for investigating this issue.
Let us know when you have more information.
@balazsorban44 - I'm struggling to get your fork working once deployed on vercel, its not picking up the NEXTAUTH_URL and is instead passing localhost:3000.
Everything is working great if I export the handlers from pages/api/auth/[...nextauth].ts and use next-auth@latest, so I'm wondering if anything else has changed in [email protected] that could break the NEXTAUTH_URL env?
Vercel does not need NEXTAUTH_URL to be set as documented.
I keep getting this everytime I try and use the PR
[next-auth][error][CLIENT_FETCH_ERROR]
https://next-auth.js.org/errors#client_fetch_error Unexpected token < in JSON at position 0 {
error: {
message: 'Unexpected token < in JSON at position 0',
stack: 'SyntaxError: Unexpected token < in JSON at position 0\n' +
' at JSON.parse (<anonymous>)\n' +
' at parseJSONFromBytes (node:internal/deps/undici/undici:6492:19)\n' +
' at successSteps (node:internal/deps/undici/undici:6466:27)\n' +
' at node:internal/deps/undici/undici:1141:60\n' +
' at node:internal/process/task_queues:140:7\n' +
' at AsyncResource.runInAsyncScope (node:async_hooks:204:9)\n' +
' at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)\n' +
' at process.processTicksAndRejections (node:internal/process/task_queues:95:5)',
name: 'SyntaxError'
},
url: 'http://localhost:3000/api/auth/session',
message: 'Unexpected token < in JSON at position 0'
}
@trpipher you have a minimal reproduction?
Will need a reproduction please :pray:. Works for me.
Someone was able to make next-auth work with new Route Handler ?
I tried today with the new Next 13.2.3 version, and [email protected] but I can't get that working without getting 'checks.state argument is missing'.
@balazsorban44 Do I understand correctly that the fix for this ticket depends on https://github.com/vercel/next.js/issues/46355? If so I want it to be here so people can get the current status without posting here that your PR does not work for them yet.
Yes, see: https://github.com/nextauthjs/next-auth/issues/6792#issuecomment-1446204623
That issue you linked is more correct to link to though, so thanks. 👍
to resolve this temporarily, setting checks: ['pkce'] in the config seems to be working with github login.
Hi @balazsorban44, sorry for the late reply. Here is the reproduction repo as you requested in https://github.com/nextauthjs/next-auth/pull/6777#issuecomment-1455228317: https://github.com/EliasGcf/next-auth-with-route-handler
In case it's helpful, I found that using @auth/core with the Solid implementation works almost out of the box with Route Handlers on the Edge runtime. https://github.com/mattddean/t3-app-router-edge/tree/07e322d717a9546a9a1764936b8044e2d6bc724a/src/auth
Hi, please share if there is there any update on this.