From b9306b7ff8c5a218e58d839dc9bbcfebe2017d16 Mon Sep 17 00:00:00 2001 From: Marsel Shaikhin Date: Thu, 2 May 2024 20:06:15 +0200 Subject: [PATCH] enh(#742): optimize internal $fetch calls --- src/runtime/composables/authjs/useAuth.ts | 10 +++++----- src/runtime/composables/commonAuthState.ts | 3 ++- src/runtime/composables/local/useAuthState.ts | 1 + src/runtime/types.ts | 1 + src/runtime/utils/url.ts | 12 +++++++++++- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/runtime/composables/authjs/useAuth.ts b/src/runtime/composables/authjs/useAuth.ts index caf7960d..583f9181 100644 --- a/src/runtime/composables/authjs/useAuth.ts +++ b/src/runtime/composables/authjs/useAuth.ts @@ -45,7 +45,7 @@ const getRequestCookies = async (nuxt: NuxtApp): Promise<{ cookie: string } | {} const getCsrfToken = async () => { const nuxt = useNuxtApp() const headers = await getRequestCookies(nuxt) - return _fetch<{ csrfToken: string }>(nuxt, 'csrf', { headers }).then(response => response.csrfToken) + return _fetch<{ csrfToken: string }>(nuxt, '/csrf', { headers }).then(response => response.csrfToken) } const getCsrfTokenWithNuxt = makeCWN(getCsrfToken) @@ -123,7 +123,7 @@ const signIn: SignInFunc = async (provider, op json: true }) - const fetchSignIn = () => _fetch<{ url: string }>(nuxt, `${action}/${provider}`, { + const fetchSignIn = () => _fetch<{ url: string }>(nuxt, `/${action}/${provider}`, { method: 'post', params: authorizationParams, headers, @@ -151,7 +151,7 @@ const signIn: SignInFunc = async (provider, op /** * Get all configured providers from the backend. You can use this method to build your own sign-in page. */ -const getProviders = () => _fetch, Omit | undefined>>(useNuxtApp(), 'providers') +const getProviders = () => _fetch, Omit | undefined>>(useNuxtApp(), '/providers') /** * Refresh and get the current session data. @@ -177,7 +177,7 @@ const getSession: GetSessionFunc = async (getSessionOptions) => { const headers = await getRequestCookies(nuxt) - return _fetch(nuxt, 'session', { + return _fetch(nuxt, '/session', { onResponse: ({ response }) => { const sessionData = response._data @@ -236,7 +236,7 @@ const signOut: SignOutFunc = async (options) => { } const callbackUrlFallback = requestURL - const signoutData = await _fetch<{ url: string }>(nuxt, 'signout', { + const signoutData = await _fetch<{ url: string }>(nuxt, '/signout', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' diff --git a/src/runtime/composables/commonAuthState.ts b/src/runtime/composables/commonAuthState.ts index 2ca1c606..d74f44b4 100644 --- a/src/runtime/composables/commonAuthState.ts +++ b/src/runtime/composables/commonAuthState.ts @@ -48,7 +48,8 @@ export const makeCommonAuthState = () => { lastRefreshedAt, status, _internal: { - baseURL + baseURL, + pathname } } } diff --git a/src/runtime/composables/local/useAuthState.ts b/src/runtime/composables/local/useAuthState.ts index c1ca2882..50c11d13 100644 --- a/src/runtime/composables/local/useAuthState.ts +++ b/src/runtime/composables/local/useAuthState.ts @@ -15,6 +15,7 @@ interface UseAuthStateReturn extends CommonUseAuthStateReturn { clearToken: () => void _internal: { baseURL: string, + pathname: string, rawTokenCookie: CookieRef } } diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 00c96fa8..01f2160c 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -440,6 +440,7 @@ export interface CommonUseAuthStateReturn { status: ComputedRef; _internal: { baseURL: string; + pathname: string; }; } diff --git a/src/runtime/utils/url.ts b/src/runtime/utils/url.ts index 4919c59a..29130af1 100644 --- a/src/runtime/utils/url.ts +++ b/src/runtime/utils/url.ts @@ -5,7 +5,17 @@ import type { ModuleOptionsNormalized } from '../types' import { useRequestEvent, useNuxtApp, abortNavigation, useAuthState } from '#imports' export const getRequestURL = (includePath = true) => getURL(useRequestEvent()?.node.req, includePath) -export const joinPathToApiURL = (path: string) => joinURL(useAuthState()._internal.baseURL, path) +export function joinPathToApiURL (path: string) { + const authStateInternal = useAuthState()._internal + + // For internal calls, use a different base + // https://github.com/sidebase/nuxt-auth/issues/742 + const base = path.startsWith('/') + ? authStateInternal.pathname + : authStateInternal.baseURL + + return joinURL(base, path) +} /** * Function to correctly navigate to auth-routes, necessary as the auth-routes are not part of the nuxt-app itself, so unknown to nuxt / vue-router.