-
-
Notifications
You must be signed in to change notification settings - Fork 164
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
feat(#797, #878): set baseURL
via environment variables and improve internal url detection
#913
base: main
Are you sure you want to change the base?
Conversation
commit: |
Testing
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general I understand the intent and very like the testing comment you left. However, there are several points:
- What happens to users who already use
baseURL
as e.g.http://localhost:3000
? In my understanding, requests are now made to this URL without adding/api/auth
? This is a breaking change and needs to have its own minor release + docs if we settle on it. - What happens to internal
$fetch
calls? Remember that they have a limitation - they only work when path starts with a/
. And here are two facts that break it: - When using
authjs
provider in which these calls are the most relevant, you need to setAUTH_ORIGIN
orbaseURL
to a fully-specified URL (i.e. protocol, hostname, etc.); - Calling a fully-specified URL using
$fetch
invokes external fetch, meaning that a real HTTP call is made from the server to itself - and it has a relatively high cost, bottlenecking performance.
Any implementation that doesn't provide backwards compatibility with at least these two points is therefore highly discouraged :/
I'd like for us to get involved in an RFC discussion and come up with a spec of how calls are made depending on what variable is set. For example, we know for sure:
authjs
provider is the same Nuxt server and therefore should always prefer internal calls, regardless of theAUTH_ORIGIN
(only taking into accountpathname
);local
provider is isomorphic (with lean towards external backends, it seems), and origin needs to be taken literally - if it is set, we assume all calls are external.
With these in mind, it might even make sense to disconnect the implementations of two providers into separate URL utils.
@@ -8,7 +8,7 @@ export default defineNuxtConfig({ | |||
globalAppMiddleware: { | |||
isEnabled: true | |||
}, | |||
baseURL: `http://localhost:${process.env.PORT || 3000}` | |||
baseURL: `http://localhost:${process.env.PORT || 3000}/api/auth` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a breaking change
It is! I had forgotten to add the label. However, I think these changes are desperately needed as the current logic makes no sense and limits how you can use it. E.g. I am working on a project where the external API routes are as follows:
Adding If I provide
This logic has been removed, in favour of checking if Example Nuxt configuration that currently breaksexport default defineNuxtConfig({
modules: ['@sidebase/nuxt-auth'],
runtimeConfig: {
public: {
api: {
baseURL: 'http://localhost:8080'
}
}
},
auth: {
originEnvKey: 'NUXT_PUBLIC_API_BASE_URL',
baseURL: 'http://localhost:8080',
provider: {
type: 'local',
endpoints: {
signIn: { path: '/auth/login', method: 'post' },
signUp: { path: '/auth/register', method: 'post' },
signOut: { path: '/auth/logout', method: 'post' },
getSession: { path: '/accounts/me', method: 'get' }
},
pages: {
login: '/auth/sign-in'
},
token: {
signInResponseTokenPointer: '/token',
maxAgeInSeconds: 60 * 60 * 24
},
session: {
dataType: {
id: 'string',
name: 'string',
email: 'string',
createdAt: 'number',
members: '{ id: number, email: string, roles: string[] }[]'
},
}
},
sessionRefresh: {
enableOnWindowFocus: true,
enablePeriodically: 5000
},
globalAppMiddleware: {
isEnabled: true
}
},
}) |
src/module.ts
Outdated
let baseURL = userOptions.baseURL ?? '/api/auth' | ||
if (userOptions.originEnvKey) { | ||
const envFromRuntimeConfig = extractFromRuntimeConfig(nuxt.options.runtimeConfig, userOptions.originEnvKey) | ||
const envOrigin = envFromRuntimeConfig ?? process.env[userOptions.originEnvKey] | ||
if (envOrigin) { | ||
baseURL = envOrigin | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't actually work the way you'd expect. It will set variables during build time. Because we are doing additional extractFromRuntimeConfig
later, it gets quite convoluted
If you do not set AUTH_ORIGIN
during build but set baseURL: 'http://localhost:3000/api/auth'
, and then during runtime set AUTH_ORIGIN=http://localhost:3001/other
, some requests are made to localhost:3000/api/auth
, while others are made to locahost:3001/api/auth
π Linked issue
closes #797, #878
β Type of change
π Description
π Checklist