From 28fce0f05db6c92c46ee290ff329f57f5bfcc729 Mon Sep 17 00:00:00 2001 From: Johannes Werner Date: Thu, 4 Apr 2024 12:04:35 +0200 Subject: [PATCH 1/2] feat(fetch): improve error handling (#713) --- src/runtime/utils/fetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/utils/fetch.ts b/src/runtime/utils/fetch.ts index 2651a539..644c272e 100644 --- a/src/runtime/utils/fetch.ts +++ b/src/runtime/utils/fetch.ts @@ -7,8 +7,8 @@ export const _fetch = async ( path: string, fetchOptions?: Parameters[1] ): Promise => { - const joinedPath = await callWithNuxt(nuxt, () => joinPathToApiURL(path)) try { + const joinedPath = await callWithNuxt(nuxt, () => joinPathToApiURL(path)) return $fetch(joinedPath, fetchOptions) } catch (error) { // TODO: Adapt this error to be more generic From a8000e011c42961a9f2d6d164ddea4b57bf5b04b Mon Sep 17 00:00:00 2001 From: Anja Rupnik <31987405+anjarupnik@users.noreply.github.com> Date: Thu, 4 Apr 2024 17:13:41 +0200 Subject: [PATCH 2/2] fix (refresh-token): handle error in refresh-token plugin and fix refreshOnlyToken type (#667) Co-authored-by: Marsel Shayhin <18054980+phoenix-ru@users.noreply.github.com> --- docs/content/2.configuration/2.nuxt-config.md | 6 +- src/runtime/plugins/refresh-token.server.ts | 73 ++++++++++--------- src/runtime/types.ts | 3 +- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/docs/content/2.configuration/2.nuxt-config.md b/docs/content/2.configuration/2.nuxt-config.md index 855fad4e..c0b6768c 100644 --- a/docs/content/2.configuration/2.nuxt-config.md +++ b/docs/content/2.configuration/2.nuxt-config.md @@ -292,10 +292,10 @@ type ProviderRefresh = { }, /** * When refreshOnlyToken is set, only the token will be refreshed - * - * + * + * @default true */ - refreshOnlyToken?: true; + refreshOnlyToken?: boolean; /** * Pages that `nuxt-auth` needs to know the location off for redirects. */ diff --git a/src/runtime/plugins/refresh-token.server.ts b/src/runtime/plugins/refresh-token.server.ts index 39d1a870..41cb7987 100644 --- a/src/runtime/plugins/refresh-token.server.ts +++ b/src/runtime/plugins/refresh-token.server.ts @@ -24,52 +24,57 @@ export default defineNuxtPlugin({ [configToken.token.headerName]: token.value } as HeadersInit) - const response = await _fetch>(nuxtApp, path, { - method, - body: { - refreshToken: refreshToken.value - }, - headers - }) + try { + const response = await _fetch>(nuxtApp, path, { + method, + body: { + refreshToken: refreshToken.value + }, + headers + }) - const extractedToken = jsonPointerGet( - response, - provider.token.signInResponseTokenPointer - ) - if (typeof extractedToken !== 'string') { - console.error( - `Auth: string token expected, received instead: ${JSON.stringify( - extractedToken - )}. Tried to find token at ${ - provider.token.signInResponseTokenPointer - } in ${JSON.stringify(response)}` - ) - return - } - - // check if refereshTokenOnly - if (!configToken.refreshOnlyToken) { - const extractedRefreshToken = jsonPointerGet( + const extractedToken = jsonPointerGet( response, - provider.refreshToken.signInResponseRefreshTokenPointer + provider.token.signInResponseTokenPointer ) - if (typeof extractedRefreshToken !== 'string') { + if (typeof extractedToken !== 'string') { console.error( `Auth: string token expected, received instead: ${JSON.stringify( - extractedRefreshToken + extractedToken )}. Tried to find token at ${ - provider.refreshToken.signInResponseRefreshTokenPointer + provider.token.signInResponseTokenPointer } in ${JSON.stringify(response)}` ) return - } else { - rawRefreshToken.value = extractedRefreshToken } - } - rawToken.value = extractedToken + // check if refereshTokenOnly + if (!configToken.refreshOnlyToken) { + const extractedRefreshToken = jsonPointerGet( + response, + provider.refreshToken.signInResponseRefreshTokenPointer + ) + if (typeof extractedRefreshToken !== 'string') { + console.error( + `Auth: string token expected, received instead: ${JSON.stringify( + extractedRefreshToken + )}. Tried to find token at ${ + provider.refreshToken.signInResponseRefreshTokenPointer + } in ${JSON.stringify(response)}` + ) + return + } else { + rawRefreshToken.value = extractedRefreshToken + } + } - lastRefreshedAt.value = new Date() + rawToken.value = extractedToken + + lastRefreshedAt.value = new Date() + } catch (err) { + rawRefreshToken.value = null + rawToken.value = null + } } } }) diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 094c446e..18c20dca 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -201,8 +201,9 @@ export type ProviderLocalRefresh = Omit & { /** * When refreshOnlyToken is set, only the token will be refreshed * + * @default true */ - refreshOnlyToken?: true; + refreshOnlyToken?: boolean; refreshToken?: { /**