sidebase / nuxt-auth

Authentication built for Nuxt 3! Easily add authentication via OAuth providers, credentials or Email Magic URLs!
https://auth.sidebase.io
MIT License
1.31k stars 164 forks source link

Cannot find package 'next-auth' when importing in server using '#auth' #877

Closed FabianClemenz closed 2 months ago

FabianClemenz commented 2 months ago

Environment

Working directory: /frontend                                                                                                                                                                                               09:37:31
Nuxt project info: (copied to clipboard)                                                                                                                                                                                                                                  09:37:31

------------------------------
- Operating System: Darwin
- Node Version:     v22.6.0
- Nuxt Version:     3.12.4
- CLI Version:      3.12.0
- Nitro Version:    2.9.7
- Package Manager:  yarn@1.22.22
- Builder:          -
- User Config:      compatibilityDate, devtools, modules, auth
- Runtime Modules:  @nuxt/ui@2.18.4, @sidebase/nuxt-auth@0.8.2, @nuxt/eslint@0.5.1, @pinia/nuxt@0.5.4
- Build Modules:    -
------------------------------

Reproduction

Reproduction link

Describe the bug

I'm using nuxt-auth for JWT authentication with my own backend. I wanted to create a middleware, which adds the accessToken to every request.

As seen in the reproduction, i followed exactly the guide here https://auth.sidebase.io/guide/authjs/server-side/jwt-access#jwt-access but i get the error:

 WARN  "next-auth/core" is imported by "node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.mjs", but could not be resolved – treating it as an external dependency.                                                                     09:49:24

 WARN  "next-auth/jwt" is imported by "node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.mjs", but could not be resolved – treating it as an external dependency.                                                                      09:49:24

✔ Nuxt Nitro server built in 194 ms                                                                                                                                                                                                                                nitro 09:49:24

 ERROR  [worker reload] [worker init] Cannot find package 'next-auth' imported from /frontend/.nuxt/dev/index.mjs            

Additional context

No response

Logs

No response

zoey-kaiser commented 2 months ago

Hi @FabianClemenz 👋

Please ensure you are using next-auth@4.21.1 or earlier. In the subsequent version, they made a breaking change, where they did not expose the core anymore!

You can see more on this in our docs: https://auth.sidebase.io/guide/authjs/quick-start

FabianClemenz commented 2 months ago

Hi @zoey-kaiser

i'm not using the AuthJS Provider but the refresh provider. Do i also need to install it?

zoey-kaiser commented 2 months ago

i'm not using the AuthJS Provider but the refresh provider. Do i also need to install it?

The server-side functions are currently only supported by the authjs provider, which is why it prompts you to install the package. The local provider only supports client-side fetching. We have an open PR at the moment, which adds support for server-side token and session fetching for the local provider (#855)

Until it is released, you can fetch the Token manually on the server-side using:

Get Token:

export default defineEventHandler(() => {
  const rawToken = getCookie(event, 'auth.Token') // Or you custom cookie name
  const formattedToken = `Bearer ${rawToken}`
})
zoey-kaiser commented 2 months ago

If this works for you! I would close this Issue, feel free to comment in #854 where we previously discussed server-side access for the local provider 🤗

FabianClemenz commented 2 months ago

@zoey-kaiser ok i c - thanks for clarifying this!

i think i can get it working from here - thanks!