nuxt-alt / auth

An alternative module to @nuxtjs/auth
https://nuxt-alt-auth.vercel.app/
MIT License
94 stars 20 forks source link

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

Open ftrsoft opened 5 months ago

ftrsoft commented 5 months ago

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 commented 5 months ago

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

Denoder commented 5 months ago

...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 commented 5 months ago

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 commented 5 months ago

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