IdoPesok / zsa

https://zsa.vercel.app
MIT License
846 stars 26 forks source link

Redirect fails when using next-auth sign in redirect #188

Closed julianklumpers closed 2 months ago

julianklumpers commented 4 months ago

Hi,

i'm using next-auth (v5) which support the app router and server actions. When creating a createServerAction in the handler, I am using the signIn function with the redirectTo prop. When the sign in is successful, I get a redirect error. When using a "normal" server action it works fine.

here is my code

// login.tsx
<form action={authenticate} className="space-y-4">
   <PinForm />
</form>
// actions.ts
export const authenticate = createServerAction()
  .input(pinSchema, { type: 'formData' })
  .handler(async ({ input }) => {
    await signIn('credentials', {
      ...input,
      redirectTo: '/dashboard',
    });
});

The error in the console

Error: NEXT_REDIRECT
    at getRedirectError (webpack-internal:///(rsc)/../../node_modules/next/dist/client/components/redirect.js:49:19)
    at redirect (webpack-internal:///(rsc)/../../node_modules/next/dist/client/components/redirect.js:60:11)
    at signIn (webpack-internal:///(rsc)/../../node_modules/next-auth/lib/actions.js:59:76)
    at async $$ACTION_1 (webpack-internal:///(rsc)/./src/app/login/actions.ts:67:21)
    at async wrapper (webpack-internal:///(rsc)/../../node_modules/zsa/dist/index.mjs:730:22)
    at async /path/to/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:39:418
    at async rw (/path/to/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:38:7978)
    at async r6 (/path/to/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:41:1256)
    at async doRender (/path/to/node_modules/next/dist/server/base-server.js:1438:30)
    at async cacheEntry.responseCache.get.routeKind (/path/to/node_modules/next/dist/server/base-server.js:1599:28)
    at async DevServer.renderToResponseWithComponentsImpl (/path/to/node_modules/next/dist/server/base-server.js:1507:28)
    at async DevServer.renderPageComponent (/path/to/node_modules/next/dist/server/base-server.js:1931:24)
    at async DevServer.renderToResponseImpl (/path/to/node_modules/next/dist/server/base-server.js:1969:32)
    at async DevServer.pipeImpl (/path/to/node_modules/next/dist/server/base-server.js:920:25)
    at async NextNodeServer.handleCatchallRenderRequest (/path/to/node_modules/next/dist/server/next-server.js:272:17)
    at async DevServer.handleRequestImpl (/path/to/node_modules/next/dist/server/base-server.js:816:17)
    at async /path/to/node_modules/next/dist/server/dev/next-dev-server.js:339:20
    at async Span.traceAsyncFn (/path/to/node_modules/next/dist/trace/trace.js:154:20)
    at async DevServer.handleRequest (/path/to/node_modules/next/dist/server/dev/next-dev-server.js:336:24)
    at async invokeRender (/path/to/node_modules/next/dist/server/lib/router-server.js:174:21)
    at async handleRequest (/path/to/node_modules/next/dist/server/lib/router-server.js:353:24)
    at async requestHandlerImpl (/path/to/node_modules/next/dist/server/lib/router-server.js:377:13)
    at async Server.requestListener (/path/to/node_modules/next/dist/server/lib/start-server.js:141:13) {
  digest: 'NEXT_REDIRECT;replace;https://localhost:3002/dashboard;303;',
  mutableCookies: p {
    _parsed: Map(3) {
      '__Host-authjs.csrf-token' => [Object],
      '__Secure-authjs.callback-url' => [Object],
      '__Secure-authjs.session-token' => [Object]
    },
    _headers: HeadersList {
      cookies: [Array],
      [Symbol(headers map)]: [Map],
      [Symbol(headers map sorted)]: null
    }
  }
}
IdoPesok commented 4 months ago

Hi, it should be able to handle this error correctly. Do you have "use server" at the top of the actions file? If so, can you possibly share a repro link.

IdoPesok commented 2 months ago

Closing due to inactivity. Happy to reopen if it is still an issue.