nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
22.49k stars 2.98k forks source link

Middleware Route Protection nextjs example result in Middleware relative urls error #10760

Closed Ucok23 closed 2 weeks ago

Ucok23 commented 2 weeks ago

What is the improvement or update you wish to see?

The next-auth docs about protecting route in getting started section resulting middleware relative urls error

import { auth } from "./auth"

export default auth((req) => {
  if (!req.auth) {
    return NextResponse.redirect("/login")
  }
})

Screenshot_20240429_145119_DWService

Is there any context that might help us understand?

NextResponse.redirect(url) url should be absolute url i think

Does the docs page already exist? Please link to it.

https://authjs.dev/getting-started/session-management/protecting

balazsorban44 commented 2 weeks ago

Fix in the docs: https://authjs.dev/getting-started/session-management/protecting#nextjs-middleware

In short, you can replace the current URLs pathname.