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

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

Open
ftrsoft opened this issue Jan 31, 2024 · 4 comments
Open

Comments

@ftrsoft
Copy link

ftrsoft commented Jan 31, 2024

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
Copy link
Author

ftrsoft commented Jan 31, 2024

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

@Denoder
Copy link
Member

Denoder commented Jan 31, 2024

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

@ftrsoft
Copy link
Author

ftrsoft commented Jan 31, 2024

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
Copy link
Author

ftrsoft commented Jan 31, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants