auth icon indicating copy to clipboard operation
auth copied to clipboard

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

Open ftrsoft opened this issue 2 years ago • 4 comments

Environment



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

ftrsoft avatar Jan 31 '24 02:01 ftrsoft

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

ftrsoft avatar Jan 31 '24 03:01 ftrsoft

...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.

Denoder avatar Jan 31 '24 10:01 Denoder

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

ftrsoft avatar Jan 31 '24 12:01 ftrsoft

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

ftrsoft avatar Jan 31 '24 12:01 ftrsoft