Ric
Ric

Reputation: 319

Authjs - Error API call been made to "/auth/auth/" instead of "/auth"

Environment

System:

OS: macOS 13.5

CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz

Memory: 1.93 GB / 32.00 GB

Shell: 5.9 - /bin/zsh

Binaries:

Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
npm: 10.2.5 - ~/.nvm/versions/node/v20.10.0/bin/npm

Browsers:

Brave Browser: 111.1.49.132

Chrome: 122.0.6261.112

Safari: 16.6

npmPackages:
@auth/prisma-adapter: ^1.0.11 => 1.5.0
next: 14.0.4 => 14.0.4
next-auth: ^5.0.0-beta.15 => 5.0.0-beta.15
react: ^18 => 18.2.0

Reproduction URL https://github.com/RicSala/next-boilerplate.git

Issue When the user provides invalid credentials, an api call is made to:

http://localhost:3000/api/auth/auth/signin?error=CredentialsSignin&code=credentials

instead of:

http://localhost:3000/api/auth/signin?error=CredentialsSignin&code=credentials

(Note the double auth path)

resulting in a bad request, a UnknownAction error and the signin flow ending prematurely.

How to reproduce Clone de repo Run npm install && npm run dev Got to the signin page at: /auth/signin Try to signup with made up credentials You will be redirected to http://localhost:3000/api/auth/auth/signin?error=CredentialsSignin&code=credentials as a result of the invalid api call

Expected behavior The api call should be made to http://localhost:3000/api/auth/signin?error=CredentialsSignin&code=credentials

thus returning the user to the signin page with the error query param to give feedback to the user

Any help would be much appreciated.

Upvotes: 1

Views: 148

Answers (1)

Fabio Nettis
Fabio Nettis

Reputation: 2863

Found it! This would've been a tricky one if you wouldn't have known what you are searching for. Inside your middleware.ts file you have the following statement:

if (isPrivate && !isLoggedIn) {
  return Response.redirect(new URL('/auth/signin', nextUrl)); // <- your problem
}

You are adding the /auth/signin segment to whatever the current url is rather than using it as the final location. To fix this you can use the origin property. Here is an example that incorporates a fix:

if (isPrivate && !isLoggedIn) {
  return Response.redirect(new URL('/auth/signin', nextUrl.origin));
}

Upvotes: 1

Related Questions