Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export const authOptions produces error #7498

Closed
lwensveen opened this issue May 9, 2023 · 4 comments
Closed

export const authOptions produces error #7498

lwensveen opened this issue May 9, 2023 · 4 comments
Labels
question Ask how to do something or how something works

Comments

@lwensveen
Copy link

lwensveen commented May 9, 2023

Question 💬

I was following the examples on this page: https://next-auth.js.org/getting-started/example#add-api-route which gave me the following error

app/api/auth/[...nextauth]/route.ts
Type error: Route "app/api/auth/[...nextauth]/route.ts" does not match the required types of a Next.js Route.
  "authOptions" is not a valid Route export field.

which is confusing me, since

  const session = await getServerSession(req, res, authOptions)

is then unusable? The error goes away if you remove the export from export const authOptions

I'm on next@13.4.1 and next-auth@4.22.1 and I'm using the new app directory

How to reproduce ☕️

app/api/auth/[...nextauth]/route.ts
Type error: Route "app/api/auth/[...nextauth]/route.ts" does not match the required types of a Next.js Route.
  "authOptions" is not a valid Route export field.
const authOptions = {
  adapter: undefined,
  callbacks: undefined,
  cookies: undefined,
  debug: false,
  events: undefined,
  jwt: undefined,
  logger: undefined,
  pages: undefined,
  providers: [
    GithubProvider({
      clientId: process.env.GITHUB_ID as string,
      clientSecret: process.env.GITHUB_SECRET as string,
    }),
  ],

  secret: '',
  session: undefined,
  theme: undefined,
  useSecureCookies: false,
};
  const session = await getServerSession(req, res, authOptions)

Contributing 🙌🏽

Yes, I am willing to help answer this question in a PR

@lwensveen lwensveen added the question Ask how to do something or how something works label May 9, 2023
@OtisTemler
Copy link

Yeah, same problem here.

@austins
Copy link

austins commented May 10, 2023

Getting something similar, which I believe is a related if not same underlying issue with next-auth@4.22.1:

.next/types/app/auth/[...nextauth]/route.ts:8:13
Type error: Type 'OmitWithTag<typeof import("/src/app/auth/[...nextauth]/route"), "config" | "generateStaticParams" | "revalidate" | "dynamic" | "dynamicParams" | "fetchCache" | ... 8 more ... | "PATCH", "">' does not satisfy the constraint '{ [x: string]: never; }'.
  Property 'authOptions' is incompatible with index signature.
    Type 'AuthOptions' is not assignable to type 'never'.
import NextAuth, { type AuthOptions } from "next-auth";
import GitHubProvider from "next-auth/providers/github";

export const authOptions: AuthOptions = {
    providers: [GitHubProvider({ clientId: "example", clientSecret: "example" })]
};

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

@austins
Copy link

austins commented May 10, 2023

Fixed it by moving the authOptions export to another file that's not a route. Also, make sure you follow the Route Handler guide for /app dir, not /pages/api: https://next-auth.js.org/configuration/initialization#route-handlers-app

@theDanielJLewis
Copy link

This was the strangest thing! I was exporting authOptions from my route.ts folder, too, and had no problems until tonight. I suddenly started getting this same error message on next build.

So I imported authOptions from a different file (that exports on its own) and now it's working.

STRANGE! But I'm grateful that the answer was already shared!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Ask how to do something or how something works
Projects
None yet
Development

No branches or pull requests

4 participants