Next.js Discord

Discord Forum

Custom login page in nextAuth

Unanswered
kubas posted this in #help-forum
Open in Discord
Hi! I am making a login system in nextjs using nextAuth. I made a custom login and register page, but my middleware keeps trying to redirect me to the /api/auth/signin page when I'm not logged in. I want it to redirect me to the /login page.
My middleware has only one line of code:
  export { default } from "next-auth/middleware";


In api/auth/[...nextauth] in authOptions I added this:
export const authOptions: AuthOptions = {
   pages: {
       signIn: "/login",
       newUser: "/register",
     },
   // the rest of the code
}

Anyone know how to change it? Thanks you

8 Replies

Thrianta
i had the same error, deleting middleware.ts fixed it for me, for some reason it was redirecting to /api/auth/...
But I want the middleware to work and redirect me to /login. I want this page to be protected with login.
@kubas ./app/api/auth/[...nextauth]/route.ts
Ok, looks fine. Can you try putting the following into your middleware.ts:
export const config = {
  matcher: [
    '/((?!api/auth|login|register).*)',
  ],
}
Or do you want to just protect specific routes or your app?