nuxt-alt/auth

How I can pass env parameters (baseURL) to login at production mode?

Opened this issue · 4 comments

Environment


  • Operating System: Windows_NT
  • Node Version: v20.9.0
  • Nuxt Version: 3.9.3
  • CLI Version: 3.10.0
  • Nitro Version: 2.8.1
  • Package Manager: pnpm@8.12.1
  • Builder: -
  • User Config: app, routeRules, devtools, css, runtimeConfig, components, auth, http, plugins, imports, hooks, experimental, typescript, sourcemap, vite, swiper, build, modules
  • Runtime Modules: @vueuse/nuxt@10.7.2, @nuxtjs/device@3.1.1, @nuxt-alt/auth@3.1.6, @pinia/nuxt@0.5.1, nuxt-swiper@1.2.2
  • Build Modules: -

Nuxt Config

nuxt.config.ts

  runtimeConfig: {
    http: {
      baseURL: '',
    },
    public: {
      http: {
        browserBaseURL: '',
      },
    },
  },
  http: {
    baseURL: 'http://devapi.com',
    browserBaseURL: 'http://devapi.com',
  },
 auth: {
    // @ts-ignore
    strategies: {
      local: {
        scheme: 'refresh',
        token: {
          prefix: 'access_token.',
          property: 'results.access',
          maxAge: 60 * 15,
          global: true,
        },
        refreshToken: {
          prefix: 'refresh_token.',
          property: 'results.refresh',
          data: 'refresh',
          maxAge: 60 * 60 * 24 * 60,
        },
        user: {
          property: 'results.user',
          autoFetch: true,
        },
        endpoints: {
          login: { url: '/api/auth/token/', method: 'post' },
          refresh: { url: '/api/auth/token/refresh/', method: 'post' },
          user: { url: '/api/v1/users/me/', method: 'get' },
          logout: { url: '/api/auth/token/logout/', method: 'post' },
        },
      },
    },
    globalMiddleware: true,
    redirect: {
      login: '/auth/login',
      logout: '/',
      callback: '/',
      home: '/',
    },
  },

.env

NUXT_HTTP_BASE_URL=http://prodapi.com
NUXT_PUBLIC_HTTP_BROWSER_BASE_URL=http://prodapi.com

Reproduction


Describe the bug

When I try to login at production mode, I first receive an authorization request for http://devapi.com/api/auth/token/ and then request user from http://prodapi.com/api/v1/users/me/. Why is the correct URL not provided at the time of authorization? How can I use the dev environment to request authorization?
I try use

const config = useRuntimeConfig()
response = await $auth.loginWith('local', {
    baseURL: config.public.http.browserBaseURL,
    body: credentials.value,
  })

but baseURL deleted when SSR is On

Additional context

No response

Logs

No response

It is because at http module at http-plugin.nitro.ts does not use Runtimeconfig ((. Any ideas?

...I'm not sure what you're trying to do. If you want to specify different domains for the url endpoints then you can specify the domain in the url property instead of just the path (there's also a url property that you can enter to prefix all the endpoint baseURL):

Individual

endpoints: {
  login: { 
    url: 'https://domain.com/api/auth/token' 
  },
},

All endpoints

local: {
    url: 'https://domain.com',
    endpoints: {
       login: { 
            url: '/api/auth/token' 
       },
    },
}

baseURL & browserBaseURL are server and client baseURLs respectively. You specified baseURL as http://devapi.com so all nitro endpoints will use that under $http.

I want specify API URL at .env file. When apllications is run in production, not for build stage (npm build). I want to useRuntimeconfig() work correctly

From Nuxt doc:
Runtime config values are automatically replaced by matching environment variables at runtime.
I want change api url by env variables. But for nitro (http module) it is not work