Next.js Discord

Discord Forum

NextAuth Build Error with App dir

Answered
Japanese flying squid posted this in #help-forum
Open in Discord
Avatar
Japanese flying squidOP
The code bellow is running fine in development server, But when I tried to build the app it fails to build, Snapshot of error on the second code block. is anyone know how to fix this?

NextAuth API Handler
import { api } from "@CHECKPOINT/convex/_generated/api";
import { ConvexHttpClient } from "convex/browser";
import NextAuth, { type AuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";

const convex = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL);

export const authOptions: AuthOptions = {
  secret: process.env.NEXTAUTH_SECRET,
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      authorization: { params: { access_type: "offline", prompt: "consent" } },
    }),
  ],
  callbacks: {
    jwt: async ({ token, account }) => {
      if (account?.id_token) token.id_token = account.id_token;
      if (account?.refresh_token) token.refresh_token = account.refresh_token;
      return token;
    },
    signIn: async ({ profile, account }) => {
      const user = await convex.query(api.contents.users.queries.unique, {
        email: String(profile?.email),
      });

      if (user) return true;

      switch (account?.provider) {
        case "google": {
          await convex.mutation(api.contents.users.mutations.create, {
            email: String(profile?.email),
            image: String(profile?.picture),
            name: {
              family: String(profile?.family_name),
              given: String(profile?.given_name),
            },
          });
          break;
        }
      }

      return true;
    },
    session: async ({ session }) => {
      return session;
    },
  },
};

export const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

ERROR MESSAGE
PS C:\Users\Admin\Documents\@career\workspace\repositories\xyz> pnpm build

> checkpoint@0.1.0 build C:\Users\Admin\Documents\@career\workspace\repositories\xyz
> next build

 ✓ Creating an optimized production build   
 ✓ Compiled successfully
   Linting and checking validity of types .Failed to compile.

.next/types/app/api/auth/[...nextauth]/route.ts:8:13
Type error: Type 'OmitWithTag<typeof import("C:/Users/Admin/Documents/@career/workspace/repositories/xyz/app/api/auth/[...nextauth]/route"), "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS" | "PATCH" | ... 9 more ... | "HEAD", "">' does not satisfy the constraint '{ [x: string]: never; }'.
  Property 'authOptions' is incompatible with index signature.
    Type 'AuthOptions' is not assignable to type 'never'.

   6 | 
   7 | // Check that the entry is a valid entry
>  8 | checkFields<Diff<{
     |             ^
   9 |   GET?: Function
  10 |   HEAD?: Function
  11 |   OPTIONS?: Function
 ELIFECYCLE  Command failed with exit code 1.
Answered by joulev
don't export authOptions
View full answer

3 Replies

Avatar
don't export authOptions
Answer
Avatar
you can only export certain things from route handlers; authOptions isn't one of them
Avatar
Japanese flying squidOP
It works! Thank you 🙏