kinde-oss / kinde-auth-nextjs

Kinde NextJS SDK - authentication for server rendered apps
https://kinde.com/docs/developer-tools/nextjs-sdk/
MIT License
132 stars 17 forks source link

Bug: Error 500 on production #169

Closed Vaib215 closed 1 month ago

Vaib215 commented 1 month ago

Prerequisites

Describe the issue

I launched my product with kinde's auth here: https://the-abc.tech I did most of the things correctly.

Library URL

https://github.com/kinde-oss/kinde-auth-nextjs/

Library version

2.2.5

Operating system(s)

macOS

Operating system version(s)

14.4.1

Further environment details

No response

Reproducible test case URL

https://the-abc.tech

Additional information

No response

DanielRivers commented 1 month ago

Hi could you explain at what stage the error was showing? Your URL is no longer use Kinde to check.

Vaib215 commented 1 month ago

I did change the auth from kinde to clerk. The error was visible sometimes before login, sometimes after login

DanielRivers commented 1 month ago

@Vaib215 it's hard to assist here, not much to go on. Are you able to provide some code or deploy with Kinde auth for me to check out?

AntoLepejian commented 1 month ago

I'm also running into a similar issue.

Works locally, but when deployed to vercel it's failing, and this is the only useful log I could find.

[GET] /api/auth/login?nxtPkindeAuth=login status=500

This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:

Unhandled Rejection: Error: NEXT_REDIRECT

All I have in my /api/auth/[kindeAuth]/route.ts is this

import { handleAuth } from '@kinde-oss/kinde-auth-nextjs/server';

export const GET = handleAuth();

I've made sure my env vars point to the production URL, and not localhost.

AntoLepejian commented 1 month ago

Okay so I've done a little more digging, and turns out the my problem was the fact that I was using Sentry...

I still believe there's a change required from Kinde SDK's side.

In NextJS, the redirect method simply throws a NEXT_REDIRECT Error, which is caught, and subsequently actioned. Which is why if/when we use Sentry, Kinde's attempt to redirect is caught by Sentry's Middleware causing the 500.

I think a solution here might be to use NextRedirect instead of redirect here, which is how API routes should redirect I believe.

For anyone wondering, the solution is:

// next.config.js
export default withSentryConfig(config, {
   ...

  // https://github.com/kinde-oss/kinde-auth-nextjs/issues/169#issuecomment-2133396389
  excludeServerRoutes: [new RegExp('/api/auth/.*')],
});
DanielRivers commented 1 month ago

@AntoLepejian Could you please try version 2.2.14-0 and let me know if this resolves the issue please?

AntoLepejian commented 1 month ago

@AntoLepejian Could you please try version 2.2.14-0 and let me know if this resolves the issue please?

@DanielRivers this was actually resolved in 2.2.12 since my suggestion was implemented in #173 🙌🏼

DanielRivers commented 1 month ago

Great! :)