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.3k stars 165 forks source link

could not resolve import when module isEnabled = false #454

Open agracia-foticos opened 1 year ago

agracia-foticos commented 1 year ago

Environment

Reproduction

We are using the same code, for multi-site APP... same sites has login and others sites hasnt login.

We have a wrapper to use useAuth, but we have a problem with src/server/routes/api/auth/[...].ts file...

we have the module disabled but when in auth file try to import import { NuxtAuthHandler } from '#auth' throws an error

(node-resolve plugin) Could not resolve import "#auth" in /home/var/www/src/server/routes/api/auth/[...].ts using imports defined in /home/var/www/package.json. (node-resolve plugin) Could not resolve import "#auth" in /home/var/www/src/server/routes/api/auth/[...].ts using imports defined in /home/var/www/package.json. "#auth" is imported by "src/server/routes/api/auth/[...].ts", but could not be resolved – treating it as an external dependency.

If we use visual studio debugger, we cant start the app, only can with npx nuxi build and node start

Describe the bug

We are using the same code, for multi-site APP... same sites has login and others sites hasnt login.

We have a wrapper to use useAuth, but we have a problem with src/server/routes/api/auth/[...].ts file...

we have the module disabled but when in auth file try to import import { NuxtAuthHandler } from '#auth' throws an error

(node-resolve plugin) Could not resolve import "#auth" in /home/var/www/src/server/routes/api/auth/[...].ts using imports defined in /home/var/www/package.json. (node-resolve plugin) Could not resolve import "#auth" in /home/var/www/src/server/routes/api/auth/[...].ts using imports defined in /home/var/www/package.json. "#auth" is imported by "src/server/routes/api/auth/[...].ts", but could not be resolved – treating it as an external dependency.

If we use visual studio debugger, we cant start the app, only can with npx nuxi build and node start

Additional context

No response

Logs

No response

agracia-foticos commented 1 year ago

any solution?

treygrr commented 1 year ago

Make sure that in the application that you are referencing that imports #auth has the module for '@sidebase/nuxt-auth', imported in the nuxt.config.ts file.

I'm not totally sure how your mutli site is wired, but each nuxt instance needs the import to build out the type stubs/ definitions when the nuxt / vite lifecycle hooks are called. Without this module running, it will not build out your types and ts will show an error.

agracia-foticos commented 1 year ago

image

The problem is that when I put enabled=false, I can't import import { NuxtAuthHandler } from '#auth'

Therefore, the enabled does not solve anything... I want to enable and disable the module without having to change my programming

treygrr commented 1 year ago

I would then recommend you rename the above file to something else ie authApi.ts and then name a new file what it is currently, the import the old file based on if the auth is enabled or disabled. As of right now you are trying to import a reference when it's prescribing module import isn't built.

[...].ts' export default function () { // logic if the module is enabled // import your authApi.ts file https://stackoverflow.com/questions/13444064/typescript-conditional-module-import-export }

th3l0g4n commented 1 year ago

In my opinion it would make sense to still provide the virtual module (#auth) and all of its exports even if the auth package is disabled and "just" disable the whole authorization logic inside the package.

zoey-kaiser commented 1 year ago

@agracia-foticos I understand your problem, however I see multiple issues with exposing all the exports.

isEnabled tries to minimize how much is needed to load, to ensure that if you still have it inside, but don't need it your startup times are not hindered by NuxtAuth. Loading all the exports will have an effect on the performance and load times of the app, as there is just a lot that needs to be loaded. I personally only see two use cases where you would want isEnabled to be false:

  1. Testing it with mocked Data
  2. Keeping your authentication setup, but disabling auth for a while.

For 1. I would recommend you look at my answer here on how to properly mock an authentication request (without disabling the module). For 2. I personally think, commenting out the export would be enough for now, as loading all the files would not be worth it IMO.

If you have any other use cases for isEnabled or any ideas on how you would change the logic to:

please let me know and we can futhur discuss this matter!

phoenix-ru commented 7 months ago

Related to #596