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] 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?: { /**