Search code examples
reactjsherokunext-auth

next-auth / Sign in issue


I am trying to work from this project: https://github.com/nextauthjs/next-auth-example to see how next-auth works and experiment with it. I hit this issue when trying to log in:

✓ Compiled /api/auth/[...nextauth] in 72ms (598 modules) ⨯ Detected default export in '/Users/me/Documents/Heroku/next-auth-example/app/api/auth/[...nextauth]/route.ts'. Export a named export for each HTTP method instead. ⨯ No HTTP methods exported in '/Users/me/Documents/Heroku/next-auth-example/app/api/auth/[...nextauth]/route.ts'. Export a named export for each HTTP method.

This is /api/auth/[...nextauth]/route.ts :

import NextAuth from "next-auth"
import Auth0Provider from "next-auth/providers/auth0"

export default NextAuth({
  providers: [
    Auth0Provider({
      clientId: process.env.AUTH0_ID,
      clientSecret: process.env.AUTH0_SECRET,
    }),
  ],
})

Solution

  • In the app router, for each http method you must export a handler function with the name of the method. For example:

    export const GET = () => {
       // code here
    }
    
    export const POST = () => {
       // code here
    }
    

    And if you want use the NextAuth handlers, you must destructure it and export it like this:

    const { handlers } = NextAuth({ ... })
    
    export const { GET, POST } = handlers
    

    in the example you provided, they exported the NextAuth from here and used it here