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

Support Route Handlers in the `app` folder of Nextjs 13

Open benderillo opened this issue 3 years ago • 33 comments

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

benderillo avatar Feb 23 '23 05:02 benderillo

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".

balazsorban44 avatar Feb 23 '23 12:02 balazsorban44

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?

benderillo avatar Feb 24 '23 00:02 benderillo

I would like to get this released maybe next week, if not sooner.

balazsorban44 avatar Feb 24 '23 10:02 balazsorban44

@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.

benderillo avatar Feb 24 '23 15:02 benderillo

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)

liamrohan avatar Feb 24 '23 18:02 liamrohan

Same here using DiscordProvider

checks.state argument is missing

tom-brulin avatar Feb 25 '23 11:02 tom-brulin

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 avatar Feb 27 '23 12:02 balazsorban44

@balazsorban44 Thanks for investigating this issue.

Let us know when you have more information.

tom-brulin avatar Feb 27 '23 14:02 tom-brulin

@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?

grezle avatar Feb 27 '23 19:02 grezle

Vercel does not need NEXTAUTH_URL to be set as documented.

balazsorban44 avatar Feb 27 '23 23:02 balazsorban44

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 avatar Feb 27 '23 23:02 trpipher

@trpipher you have a minimal reproduction?

balazsorban44 avatar Feb 28 '23 00:02 balazsorban44

Will need a reproduction please :pray:. Works for me.

balazsorban44 avatar Feb 28 '23 00:02 balazsorban44

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'.

tom-brulin avatar Mar 01 '23 12:03 tom-brulin

@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.

benderillo avatar Mar 01 '23 23:03 benderillo

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. 👍

balazsorban44 avatar Mar 02 '23 02:03 balazsorban44

to resolve this temporarily, setting checks: ['pkce'] in the config seems to be working with github login.

NiranjithCU avatar Mar 05 '23 15:03 NiranjithCU

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

EliasGcf avatar Mar 10 '23 23:03 EliasGcf

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

mattddean avatar Mar 16 '23 13:03 mattddean

Hi, please share if there is there any update on this.

codinesh avatar Mar 21 '23 23:03 codinesh