Skip to content

enh(#742): optimize internal $fetch calls #750

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

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/runtime/composables/authjs/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -123,7 +123,7 @@ const signIn: SignInFunc<SupportedProviders, SignInResult> = 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,
Expand Down Expand Up @@ -151,7 +151,7 @@ const signIn: SignInFunc<SupportedProviders, SignInResult> = 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<Record<Exclude<SupportedProviders, undefined>, Omit<AppProvider, 'options'> | undefined>>(useNuxtApp(), 'providers')
const getProviders = () => _fetch<Record<Exclude<SupportedProviders, undefined>, Omit<AppProvider, 'options'> | undefined>>(useNuxtApp(), '/providers')

/**
* Refresh and get the current session data.
Expand All @@ -177,7 +177,7 @@ const getSession: GetSessionFunc<SessionData> = async (getSessionOptions) => {

const headers = await getRequestCookies(nuxt)

return _fetch<SessionData>(nuxt, 'session', {
return _fetch<SessionData>(nuxt, '/session', {
onResponse: ({ response }) => {
const sessionData = response._data

Expand Down Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/composables/commonAuthState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export const makeCommonAuthState = <SessionData>() => {
lastRefreshedAt,
status,
_internal: {
baseURL
baseURL,
pathname
}
}
}
1 change: 1 addition & 0 deletions src/runtime/composables/local/useAuthState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface UseAuthStateReturn extends CommonUseAuthStateReturn<SessionData> {
clearToken: () => void
_internal: {
baseURL: string,
pathname: string,
rawTokenCookie: CookieRef<string | null>
}
}
Expand Down
1 change: 1 addition & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ export interface CommonUseAuthStateReturn<SessionData> {
status: ComputedRef<SessionStatus>;
_internal: {
baseURL: string;
pathname: string;
};
}

Expand Down
12 changes: 11 additions & 1 deletion src/runtime/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down