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

Declaration TypeScript Bug in NextAuth v5 #10568

Open
SiebeBaree opened this issue Apr 12, 2024 · 9 comments
Open

Declaration TypeScript Bug in NextAuth v5 #10568

SiebeBaree opened this issue Apr 12, 2024 · 9 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@SiebeBaree
Copy link

Environment

System:
OS: macOS 14.4.1
CPU: (8) arm64 Apple M1
Memory: 63.70 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 21.7.1 - /opt/homebrew/bin/node
npm: 10.5.0 - /opt/homebrew/bin/npm
pnpm: 8.15.6 - /opt/homebrew/bin/pnpm
bun: 1.1.3 - ~/.bun/bin/bun
Watchman: 2024.03.25.00 - /opt/homebrew/bin/watchman
Browsers:
Brave Browser: 123.1.64.122
Chrome: 123.0.6312.123
Safari: 17.4.1
npmPackages:
next: 13.5.4 => 13.5.4
next-auth: ^5.0.0-beta.16 => 5.0.0-beta.16
react: ^18 => 18.2.0

Reproduction URL

https://github.com/SiebeBaree/declaration-ts-bug-next-auth-v5

Describe the issue

When turning on declararation and declarationMap in the tsconfig.json NextAuth v5 beta 16 will give the following error:

The inferred type of 'auth' cannot be named without a reference to '../node_modules/next-auth/lib'. This is likely not portable. A type annotation is necessary.

The code works perfectly fine in dev mode but will cause build errors during deployment.
The error happens in src/auth.ts in the reproduction repo linked above

How to reproduce

  1. npx create-next-app@latest (enable TypeScript)
  2. npm install next-auth@beta
  3. Create the auth.ts file like the installation guide recommends
  4. Set the declaration and declarationMap to true in the compilerOptions of your tsconfig.json

This repository uses Bun but this error is the same on npm, pnpm with node.js runtime.

Expected behavior

That even with declaration and declarationMap set to true NextAuth should give any issues when using the new auth() function.

@SiebeBaree SiebeBaree added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Apr 12, 2024
@andrevenancio

This comment has been minimized.

@brunowego

This comment was marked as off-topic.

@SiebeBaree
Copy link
Author

@SiebeBaree same here. Did you find a workaround?

Yes, turn of declaration and declarationMap in your tsconfig

jeremy-code added a commit to jeremy-code/defraud that referenced this issue Jun 22, 2024
- Add `next-auth` to drizzle schema in `packages/database`
- Remove `declaration` and `declarationMap` due to `next-auth` type errors (see nextauthjs/next-auth#10568)
- Add `auth.ts` to `apps/client/src/lib` for `next-auth` initialization and config
- Add `api/auth/[...nextauth]/route.ts` to `apps/client/src/app` for `next-auth` API routes
- Add `auth` route group and pages to `apps/client/src/app` for `next-auth` pages (signin, signup, verify-request)
  - Add `OAuthButton` and `OAuthIcon` components to `apps/client/src/app/(auth)/_components` for `next-auth` OAuth providers
jeremy-code added a commit to jeremy-code/defraud that referenced this issue Jun 22, 2024
- Add `next-auth` to drizzle schema in `packages/database`
- Remove `declaration` and `declarationMap` due to `next-auth` type errors (see nextauthjs/next-auth#10568)
- Add `auth.ts` to `apps/client/src/lib` for `next-auth` initialization and config
- Add `api/auth/[...nextauth]/route.ts` to `apps/client/src/app` for `next-auth` API routes
- Add `auth` route group and pages to `apps/client/src/app` for `next-auth` pages (signin, signup, verify-request)
  - Add `OAuthButton` and `OAuthIcon` components to `apps/client/src/app/(auth)/_components` for `next-auth` OAuth providers
@mj-schweiger
Copy link

Does anyone know what to do if another part of the project requires declarationMap & declaration to be set to "true"? Is this a bug in NextAuth?

@shuaibu-shehu
Copy link

Team! please respond to this as i am also facing same issue

  The inferred type of 'auth' cannot be named without a reference to '@/node_modules/next-auth/lib'. This is likely not portable. A type annotation is necessary.ts(2742)
The inferred type of 'auth' cannot be named without a reference to '@/node_modules/next-auth/lib/types'. This is likely not portable. A type annotation is necessary.ts(2742)

export const { handlers:{GET, POST},signIn, signOut, auth }  = NextAuth({
  callbacks: {
    async signIn({ user, account }) {
      console.log(user);
      //Allow 0Auth
      
      if(account?.provider!=="credentials") return true

      const existingUser= await getUserById(user.id!);
      
      // prevent sign in without email verification
       if(!existingUser || !existingUser.emailVerified){
        return false
      }

      return true
      },
    async session({ session, token }) {
      if(token.sub && session.user){
        session.user.id = token.sub
      }

      return session
    },
    async jwt({ token }) {

      if(!token.sub) return token
      const existingUser= await getUserById(token.sub)

      return token
  }
},
secret: process.env.NEXTAUTH_SECRET || "secr3t",
  adapter: PrismaAdapter(db),
  session: { strategy: "jwt"},
  ...authConfig,
})

the type error comes from the auth(), but i am using turbo repo and when i remove the "extents"
property from tsconfig.ts it works without the error

{
 "extends": "@repo/typescript-config/nextjs.json",
 "compilerOptions": {
   "lib": ["dom", "dom.iterable", "esnext"],
   "allowJs": true,
   "skipLibCheck": true,
   "strict": true,
   "noEmit": true,
   "esModuleInterop": true,
   "module": "esnext",
   "moduleResolution": "bundler",
   "resolveJsonModule": true,
   "isolatedModules": true,
   "jsx": "preserve",
   "incremental": true,
   "plugins": [
     {
       "name": "next"
     }
   ],
   "paths": {
     "@/*": ["./*"]
   }
 },
 "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
 "exclude": ["node_modules"]
}

@ghbishal
Copy link

ghbishal commented Jul 3, 2024

@shuaibu-shehu just add below lines inside the compilerOptions

    "declaration": false,
    "declarationMap": false,

@coldflyingcats
Copy link

sharing workaround (declarationMap & declaration to be set to "true")

import NextAuth, { NextAuthResult } from "next-auth";

const nextAuth = NextAuth(authConfig);

const signIn: NextAuthResult["signIn"] = nextAuth.signIn;
const auth: NextAuthResult["auth"] = nextAuth.auth;

@pantharshit007
Copy link

pantharshit007 commented Oct 19, 2024

It's been months and this issue is still not resolved.
they meant it when they said beta ^5.0.0-beta.24

well in turbo-repo declaration and declarationMap is true by default so the only change is this:

const nextAuth = NextAuth(authOptions);
export const auth: NextAuthResult["auth"] = nextAuth.auth;
export const {
  handlers: { GET, POST },
  signIn,
  signOut,
  unstable_update
} = nextAuth;

EDIT:
after 3 weeks now I am getting error in signIn :/
The inferred type of signIn cannot be named without a reference to next-auth/node_modules/@auth/core/providers . This is likely not portable. A type annotation is necessary.

reason: deleted and re-installed node_modules with .lock.json files

after changes:

// need to do that coz of: https://github.com/nextauthjs/next-auth/issues/10568
export const auth: NextAuthResult["auth"] = nextAuth.auth;
export const signIn: NextAuthResult["signIn"] = nextAuth.signIn;
export const {
  handlers: { GET, POST },
  // signIn, // giving stupid error after just a simple re-install of node_modules 
  signOut,
  unstable_update,
} = nextAuth;
import type { Adapter } from "next-auth/adapters";
adapter: PrismaAdapter(db) as Adapter,

@Zoot01
Copy link

Zoot01 commented Jan 5, 2025

I removed the extendes from my tsconifg.json > saved > restarted TS server > and added the extends back. After that it worked. 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

9 participants