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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trivial but Frustrating: Cannot find module 'next-auth/next' or its corresponding type declarations.ts(2307) #8263

Closed
0xE-acc opened this issue Aug 8, 2023 · 10 comments
Assignees
Labels
good first issue Good issue to take for first time contributors question Ask how to do something or how something works TypeScript Issues relating to TypeScript

Comments

@0xE-acc
Copy link

0xE-acc commented Aug 8, 2023

Question 馃挰

I'm using NextJS 13.4.7 with Typescript and I simply cannot find or import next-auth/next to use getServerSession()

Any help would be greatly appreciated, I'm completely at a loss for where to look or what to try at this point.

Thanks!

How to reproduce 鈽曪笍

Screen Shot 2023-08-08 at 11 23 15 AM

Contributing 馃檶馃徑

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

@0xE-acc 0xE-acc added the question Ask how to do something or how something works label Aug 8, 2023
@ericvoshall
Copy link

I import from next-auth itself:

import { getServerSession } from 'next-auth';

@balazsorban44 balazsorban44 added TypeScript Issues relating to TypeScript good first issue Good issue to take for first time contributors labels Aug 9, 2023
@balazsorban44
Copy link
Member

"exports": {
".": "./index.js",
"./jwt": "./jwt/index.js",
"./react": "./react/index.js",
"./core": "./core/index.js",
"./next": "./next/index.js",
"./middleware": "./middleware.js",
"./client/_utils": "./client/_utils.js",
"./providers/*": "./providers/*.js"
},

Likely we should expose the type declarations here. See https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing

@0xE-acc
Copy link
Author

0xE-acc commented Aug 10, 2023

@ericvoshall when I try that I get Module '"next-auth"' has no exported member 'getServerSession'.ts(2305)

@balazsorban44 my next-auth exports and file structure are a bit different it seems, for example it appears im missing the next directory?
Screen Shot 2023-08-09 at 7 01 46 PM
Screen Shot 2023-08-09 at 7 01 22 PM

@ericvoshall
Copy link

Is it in your package.json?

I had a similar issue where next-auth was in my node_modules folder but wasn't listed as a dependency in package.json for some reason.

@balazsorban44
Copy link
Member

@eyeHORUSdotETH this is not an issue then. You installed next-auth@experimental which is a major release bump. See #7443 for more info/migration/documentation.

@0xE-acc
Copy link
Author

0xE-acc commented Aug 10, 2023

@ericvoshall @balazsorban44 thanks so much for your help 馃檹

@SamuelOliveira-M
Copy link

How was the problem resolved?
I'm using version 5.0.0-beta.5 of next-auth and I'm facing the same problem.

@gerhat
Copy link

gerhat commented Apr 27, 2024

@SamuelOliveira-M you need to use the auth() function instead.
Check this link: https://authjs.dev/getting-started/migrating-to-v5#details

import { auth } from '@/lib/auth';

const Page = async () => {
  const session = await auth();
  console.log(session);

  return <p>Welcome to admin page!</p>;
};

export default Page;

@SamuelOliveira-M
Copy link

Thank you, I hadn't found this page containing some changes from version 5.

@SamuelOliveira-M
Copy link

SamuelOliveira-M commented Apr 30, 2024

How can I add a token from a custom API to the NextAuth v5 session when I authenticate a user using the authorize method with the Credentials provider? When performing authentication, the authorize method returns an object with the structure:

{
  user: {
    email: 'sam123456@gmail.com'
  },
  expires: '2024-05-29T23:55:06.026Z'
}

How can I extract the token from this custom API and save it in the NextAuth session for later use, such as authorizing requests?

This is the complete code:

export const { auth, signIn, signOut } = NextAuth({
  ...authConfig,
  providers: [
    Credentials({
      async authorize(credentials) {
        const parsedCredentials = z
          .object({ email: z.string().email(), password: z.string().min(6) })
          .safeParse(credentials);

        if (parsedCredentials.success) {
          const { email, password } = parsedCredentials.data;
          const user = await getUser(email);
          if (!user) return null;

          const passwordsMatch = await bcrypt.compare(password, user.senha);
          
          if (passwordsMatch) return user;
        }

        console.log('Invalid credentials');
        return null;
      },
    }),
  ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good issue to take for first time contributors question Ask how to do something or how something works TypeScript Issues relating to TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants