From 75879efd86fc2b595591497831b12650353eae8b Mon Sep 17 00:00:00 2001 From: Armillus <37043348+Armillus@users.noreply.github.com> Date: Sun, 16 Jun 2024 14:15:29 +0200 Subject: [PATCH 1/4] fix: do not try to fetch the session endpoint when it is not declared --- src/runtime/composables/local/useAuth.ts | 37 +++++++++++++----------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/runtime/composables/local/useAuth.ts b/src/runtime/composables/local/useAuth.ts index 3e320e6a..4004a009 100644 --- a/src/runtime/composables/local/useAuth.ts +++ b/src/runtime/composables/local/useAuth.ts @@ -71,7 +71,7 @@ const getSession: GetSessionFunc = async (getSessionO const nuxt = useNuxtApp() const config = useTypedBackendConfig(useRuntimeConfig(), 'local') - const { path, method } = config.endpoints.getSession + const getSessionConfig = config.endpoints.getSession const { data, loading, lastRefreshedAt, rawToken, token: tokenState, _internal } = useAuthState() let token = tokenState.value @@ -83,24 +83,27 @@ const getSession: GetSessionFunc = async (getSessionO return } - const headers = new Headers(token ? { [config.token.headerName]: token } as HeadersInit : undefined) - - loading.value = true - try { - const result = await _fetch(nuxt, path, { method, headers }) - const { dataResponsePointer: sessionDataResponsePointer } = config.session - data.value = jsonPointerGet(result, sessionDataResponsePointer) - } catch (err) { - if (!data.value && err instanceof Error) { - console.error(`Session: unable to extract session, ${err.message}`) + if getSessionConfig { + const headers = new Headers(token ? { [config.token.headerName]: token } as HeadersInit : undefined) + const { path, method } = getSessionConfig + + loading.value = true + try { + const result = await _fetch(nuxt, path, { method, headers }) + const { dataResponsePointer: sessionDataResponsePointer } = config.session + data.value = jsonPointerGet(result, sessionDataResponsePointer) + } catch (err) { + if (!data.value && err instanceof Error) { + console.error(`Session: unable to extract session, ${err.message}`) + } + + // Clear all data: Request failed so we must not be authenticated + data.value = null + rawToken.value = null } - - // Clear all data: Request failed so we must not be authenticated - data.value = null - rawToken.value = null + loading.value = false + lastRefreshedAt.value = new Date() } - loading.value = false - lastRefreshedAt.value = new Date() const { required = false, callbackUrl, onUnauthenticated, external } = getSessionOptions ?? {} if (required && data.value === null) { From aeba59573971f21f40ad616c98cfd62ee75603f4 Mon Sep 17 00:00:00 2001 From: Armillus <37043348+Armillus@users.noreply.github.com> Date: Sun, 16 Jun 2024 14:19:16 +0200 Subject: [PATCH 2/4] fix: accept false as a valid type for ProviderLocal.endpoints.getSession --- src/runtime/types.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/runtime/types.ts b/src/runtime/types.ts index ee5d0454..cb075178 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -96,13 +96,15 @@ export type ProviderLocal = { signUp?: { path?: string; method?: RouterMethod }; /** * What method and path to call to fetch user / session data from. `nuxt-auth` will send the token received upon sign-in as a header along this request to authenticate. + * Set to false to disable. * * Refer to the `token` configuration to configure how `nuxt-auth` uses the token in this request. By default it will be send as a bearer-authentication header like so: `Authentication: Bearer eyNDSNJDASNMDSA....` * * @default { path: '/session', method: 'get' } * @example { path: '/user', method: 'get' } + * @example false */ - getSession?: { path?: string; method?: RouterMethod }; + getSession?: { path?: string; method?: RouterMethod } | false; }; /** * Pages that `nuxt-auth` needs to know the location off for redirects. From 5cc8061437b00e54c7f1a61a88fd49e39c147fc5 Mon Sep 17 00:00:00 2001 From: Armillus <37043348+Armillus@users.noreply.github.com> Date: Fri, 28 Jun 2024 08:32:20 +0200 Subject: [PATCH 3/4] fix: add missing parentheses in conditional statement --- src/runtime/composables/local/useAuth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/composables/local/useAuth.ts b/src/runtime/composables/local/useAuth.ts index 193db886..12015096 100644 --- a/src/runtime/composables/local/useAuth.ts +++ b/src/runtime/composables/local/useAuth.ts @@ -83,7 +83,7 @@ const getSession: GetSessionFunc = async (getSessionO return } - if getSessionConfig { + if (getSessionConfig) { const headers = new Headers(token ? { [config.token.headerName]: token } as HeadersInit : undefined) const { path, method } = getSessionConfig From 2f96284b5b1563a18ea677414b69ebb269cfe6df Mon Sep 17 00:00:00 2001 From: Armillus <37043348+Armillus@users.noreply.github.com> Date: Fri, 28 Jun 2024 08:36:18 +0200 Subject: [PATCH 4/4] style: remove trailing spaces --- src/runtime/composables/local/useAuth.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/composables/local/useAuth.ts b/src/runtime/composables/local/useAuth.ts index 12015096..55f440de 100644 --- a/src/runtime/composables/local/useAuth.ts +++ b/src/runtime/composables/local/useAuth.ts @@ -86,7 +86,7 @@ const getSession: GetSessionFunc = async (getSessionO if (getSessionConfig) { const headers = new Headers(token ? { [config.token.headerName]: token } as HeadersInit : undefined) const { path, method } = getSessionConfig - + loading.value = true try { const result = await _fetch(nuxt, path, { method, headers }) @@ -96,7 +96,7 @@ const getSession: GetSessionFunc = async (getSessionO if (!data.value && err instanceof Error) { console.error(`Session: unable to extract session, ${err.message}`) } - + // Clear all data: Request failed so we must not be authenticated data.value = null rawToken.value = null