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

Wrong URL after calling signIn function when using custom sign-in page

Open iltan987 opened this issue 10 months ago • 8 comments

Environment

"next": "15.1.7",
"next-auth": "^5.0.0-beta.25",
"react": "^19.0.0"

OS: Windows 10 Version 22H2 (OS Build 19045.5487)
Node.js version: v23.6.0
npm version: 11.1.0
Browser: Microsoft Edge Version 133.0.3065.82 (Official build) (64-bit) and Google Chrome

Reproduction URL

https://github.com/iltan987/redirect-issue

Describe the issue

First of all, this example is using custom sign in page. As I understood, there are 2 ways to redirect user to sign-in page. Server-side and client-side. Using signIn function coming from the NextAuth instance in auth.ts (method 1 in my code) and using signIn function from "next-auth/react" (method 2) The problem is when you use method 1, URL is not correct but rendered page is correct, just the URL wrong. You can see the correct URL only if you refresh the page manually. Method 2 works completely fine; both URL seen in browser and rendered page are correct.

How to reproduce

  1. Run npm install
  2. Run npx auth secret or put your AUTH_SECRET to the .env file
  3. Run the app npm run dev
  4. In the main page, you will see 2 buttons. Press the one saying "Method 1", you will see wrong URL which includes "/api/auth/signin"
  5. Refresh the page (no need to do anything), and you will see the correct URL which is "/login"
  6. Go back to main page and press the other button. You will see that you will directly see the correct URL which is "/login"

Expected behavior

Same as method 2, without needing to refresh the page manually, URL in the browser should be same as the URL specified in NextAuth config

iltan987 avatar Feb 23 '25 23:02 iltan987

having the exact same issue.

fresh installed few days ago, then facing this issue

"next": "15.1.7",
"next-auth": "^5.0.0-beta.25",

in my old project with the following version, I didn't have this problem

"next": "14.2.0-canary.34",
"next-auth": "^5.0.0-beta.15",

benevbright avatar Feb 23 '25 23:02 benevbright

having the exact same issue.

fresh installed few days ago, then facing this issue

"next": "15.1.7",
"next-auth": "^5.0.0-beta.25",

in my old project with the following version, I didn't have this problem

"next": "14.2.0-canary.34",
"next-auth": "^5.0.0-beta.15",

I tried to see the issue by checking next-auth code. Everything seems fine. I debugged it and URL seems fine. Only thing I suspect is redirect function from next/navigation. It takes the URL. Maybe in that version, there is a bug with redirect function. I don't know. This code piece is responsible of redirecting: https://github.com/nextauthjs/next-auth/blob/af2cceacf4cb29d85528792975108ae815ea47fc/packages/next-auth/src/lib/actions.ts#L35-L39

I am searching but can someone please check if the issue is because of redirect function in next/navigation?

EDIT: Updating all packages to their latest available versions including next: 15.1.7 didn't work. Then, I updated node to version v22.14.0 and pnpm to v10.4.1 Again, didn't work. I didn't get any error, but I got the same result.

iltan987 avatar Feb 24 '25 03:02 iltan987

Example app in apps/dev/nextjs is somehow working. I think it's about layout file. Because when I put my code to the root layout file, it starts working... So strange

iltan987 avatar Feb 24 '25 06:02 iltan987

@iltan987 what do you mean by?

Because when I put my code to the root layout file, it starts working... So strange

hashirshoaeb avatar Feb 26 '25 19:02 hashirshoaeb

@iltan987 what do you mean by?

Because when I put my code to the root layout file, it starts working... So strange

There are redirect functions, right? As I mentioned in the issue. One from next-auth/react package, one from NextAuth instance. If you use redirect function (the one from NextAuth instance, again in the issue) in a page file in App Router, issue persists. But if you use redirect function in layout file, problem somehow solved. Was this explanation clear?

iltan987 avatar Feb 26 '25 19:02 iltan987

Facing the same issue with a custom login page but everything works just fine on /api/auth/signin . Just getting NEXT_REDIRECT issues regardless of how i configure Google auth.

PranavMurali avatar Mar 02 '25 07:03 PranavMurali

Not ideal, but if you remove the root /app/layout.tsx, it may work. It works for me. Not sure if it's Next.js bug or NextAuth.

benevbright avatar Apr 26 '25 16:04 benevbright

having same issue. when running locally, after clicking the sign in button, it will properly redirect to the custom signIn url. when deployed to vercel, clicking the sign in button will redirect to the default api/auth/signin url

etimmons avatar May 07 '25 04:05 etimmons