Search code examples
typescriptnext.jsnext-auth

NextAuth shows me errors when embedding with Typescript


If I try to integrate NextAuth into my application with Typescript, I get 2 errors. Once at NextAuth and at NextAuthOptions.

import NextAuth from "next-auth"
import { NextAuthOptions } from "next-auth"
import GoogleProvider from "next-auth/providers/google";

export const authOptions: NextAuthOptions = ({
 providers: [
    GoogleProvider({
      clientId: `${process.env.GOOGLE_CLIENT_ID}`,
      clientSecret: `${process.env.GOOGLE_CLIENT_SECRET}`, 
    })
  ]
})

const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }

Module '"next-auth"' has no exported member 'NextAuthOptions'.ts(2305)

This expression is not callable. Type 'typeof import("next-auth")' has no call signatures.


Solution

  • Add the following types/**/*.ts in tsconfig.json:

    { 
      .
      .
      .
      "include": ["next-env.d.ts", "types/**/*.ts", "**/*.ts", "**/*.tsx"],
      .
      .
      .
    }
    

    then add the following in next-auth.d.ts

    import NextAuth, { DefaultSession } from "next-auth";
    
        // this process is know as module augmentation
        declare module "next-auth" {
          interface Session {
            user: {
              id: string;
            } & DefaultSession["user"];
          }
        }