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

originEnvKey not respected when ssr: false #991

Open
Islam260301 opened this issue Jan 24, 2025 · 5 comments
Open

originEnvKey not respected when ssr: false #991

Islam260301 opened this issue Jan 24, 2025 · 5 comments
Labels
bug A bug that needs to be resolved needs-investigation An issue where futhur investigation is required

Comments

@Islam260301
Copy link

Environment

I use:

  • nuxt3 @3.5.3
  • Typescript
  • @sidebase/nuxt-auth @0.9.4

runtimeConfig: { public: { apiBaseUrl: process.env.NUXT_PUBLIC_API_BASE_URL, }, { auth: { originEnvKey: 'NUXT_PUBLIC_API_BASE_URL', provider: { type: 'local', // another configs } } } }

Reproduction

runtimeConfig: { public: { apiBaseUrl: process.env.NUXT_PUBLIC_API_BASE_URL, }, { auth: { originEnvKey: 'NUXT_PUBLIC_API_BASE_URL', provider: { type: 'local', // another configs } } } }

Describe the bug

Nxut3. Dont work originEnvKey when ssr: false in nuxt.config. Because originEnvKey used just at getServerOrigin function, which in node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/utils.js. There used useRuntimeConfig(). But we dont have access to config[normalized], because in client at config not private variables from .env.

My solution is possible. Add a condition "envVariableName.startsWith(NUXT_PUBLIC_)"

Additional context

No response

Logs

@Islam260301 Islam260301 added bug A bug that needs to be resolved pending An issue waiting for triage labels Jan 24, 2025
@Islam260301
Copy link
Author

Solution with baseURL: process.env.NUXT_PUBLIC_API_BASE_URL+ "/api/auth", not for me. Because I provide env vars in runtime (in start app). At build I don`t have them

@phoenix-ru
Copy link
Collaborator

phoenix-ru commented Jan 30, 2025

upd2: I don't think you are setting originEnvKey correctly

It should be:

export default defineNuxtConfig({
  auth: {
    originEnvKey: 'NUXT_PUBLIC_API_BASE_URL',
  },
})


You only have two options with Nuxt, as far as I know:

  • using build-time variables;
  • using runtime variables, which are passed from the server onto the client via a plugin

Also please check if 0.10.0 works for you


upd1: I think I know how to reproduce an issue similar to yours and potentially fix it. Not sure if it will help you though. I will try to find time for it the next week as part of 0.10 pathing improvements.

@phoenix-ru phoenix-ru added question A question about NuxtAuth and removed bug A bug that needs to be resolved pending An issue waiting for triage labels Jan 30, 2025
@phoenix-ru phoenix-ru changed the title Nxut3. Don`t work originEnvKey when ssr: false in nuxt.config originEnvKey not respected when ssr: false Jan 30, 2025
@phoenix-ru phoenix-ru added bug A bug that needs to be resolved needs-investigation An issue where futhur investigation is required and removed question A question about NuxtAuth labels Jan 30, 2025
@Islam260301
Copy link
Author

Islam260301 commented Jan 31, 2025

I checked 0.10.0 version. It1s don't work for me. But I don't understand this using runtime variables, which are passed from the server onto the client via a [plugin]

@Islam260301
Copy link
Author

@phoenix-ru, Can you make an example, where request origin provided from runtime, please?

@phoenix-ru
Copy link
Collaborator

Whenever you set AUTH_ORIGIN (or your custom env var specified in originEnvKey) during an application start (AUTH_ORIGIN=https://your.origin/api/auth node .output/server/index.mjs) the plugin will transfer this variable from SSR into CSR:

// Set the correct `baseURL` on the server,
// because the client would not have access to environment variables
if (import.meta.server) {
runtimeConfig.baseURL = resolveApiBaseURL(wholeRuntimeConfig)
}

I would assume that setting ssr: false does not run the plugin on server-side, thus not sending the environment variable to the client. This means that you need to use a build-time configuration (baseURL or env). There is simply no way for Nuxt to pick up the variable without server running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug that needs to be resolved needs-investigation An issue where futhur investigation is required
Projects
None yet
Development

No branches or pull requests

2 participants