sidebase / nuxt-auth

Authentication built for Nuxt 3! Easily add authentication via OAuth providers, credentials or Email Magic URLs!
https://auth.sidebase.io
MIT License
1.33k stars 164 forks source link

feat(#797, #878): set `baseURL` via environment variables and improve internal url detection #913

Open zoey-kaiser opened 2 months ago

zoey-kaiser commented 2 months ago

๐Ÿ”— Linked issue

closes #797, #878

โ“ Type of change

๐Ÿ“š Description

๐Ÿ“ Checklist

zoey-kaiser commented 2 months ago

Testing auth.baseURL and AUTH_ORIGIN:

Only setting auth.baseURL

Combination of auth.baseURL and AUTH_ORIGIN

Testing setting internal vs external URLs:

Note: I tested that the internal URLs actually request internal URLs, but setting the path to a non existent route and ensuring I receive the Vue Router warning

zoey-kaiser commented 2 months ago

This is a breaking change and needs to have its own minor release + docs if we settle on it.

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 /api/auth if no path is provided, makes the URLs listed above incompatible with the local provider, as there is no base path I can provide.

If I provide localhost:8080 as baseURL it gets transformed to: localhost:8080/api/auth which does not make sense IMO.

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:

This logic has been removed, in favour of checking if origin was provided. If you are using an internal API within your Nuxt Application, I do not expect the origin to be passed (as in all our our examples this is also not the case). If no origin is provided, the fetch requests are made internally, if one is provided they are handled as external URLs.

Example Nuxt configuration that currently breaks ```ts export 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 } }, }) ```
pkg-pr-new[bot] commented 1 week ago

Open in Stackblitz

npm i https://pkg.pr.new/@sidebase/nuxt-auth@913

commit: 5d8bd04