From ee7d23330cb39ce53369ea6c7d4c79cc1d0d8c33 Mon Sep 17 00:00:00 2001 From: Anja Rupnik Date: Fri, 16 Feb 2024 11:51:47 +0100 Subject: [PATCH] fix (refresh-token): handle error in refresh-token plugin and fix refreshOnlyToken type --- 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 4f7dd152..c5dc8836 100644 --- a/docs/content/2.configuration/2.nuxt-config.md +++ b/docs/content/2.configuration/2.nuxt-config.md @@ -285,10 +285,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 51490231..73eb9df7 100644 --- a/src/runtime/plugins/refresh-token.server.ts +++ b/src/runtime/plugins/refresh-token.server.ts @@ -19,52 +19,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, - config.provider.token.signInResponseTokenPointer - ) - if (typeof extractedToken !== 'string') { - console.error( - `Auth: string token expected, received instead: ${JSON.stringify( - extractedToken - )}. Tried to find token at ${ - config.token.signInResponseTokenPointer - } in ${JSON.stringify(response)}` - ) - return - } - - // check if refereshTokenOnly - if (!configToken.refreshOnlyToken) { - const extractedRefreshToken = jsonPointerGet( + const extractedToken = jsonPointerGet( response, - config.provider.refreshToken.signInResponseRefreshTokenPointer + config.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 ${ - config.refreshToken.signInResponseRefreshTokenPointer + config.token.signInResponseTokenPointer } in ${JSON.stringify(response)}` ) return - } else { - rawRefreshToken.value = extractedRefreshToken } - } - rawToken.value = extractedToken + // check if refereshTokenOnly + if (!configToken.refreshOnlyToken) { + const extractedRefreshToken = jsonPointerGet( + response, + config.provider.refreshToken.signInResponseRefreshTokenPointer + ) + if (typeof extractedRefreshToken !== 'string') { + console.error( + `Auth: string token expected, received instead: ${JSON.stringify( + extractedRefreshToken + )}. Tried to find token at ${ + config.refreshToken.signInResponseRefreshTokenPointer + } in ${JSON.stringify(response)}` + ) + return + } else { + rawRefreshToken.value = extractedRefreshToken + } + } - lastRefreshedAt.value = new Date() + rawToken.value = extractedToken + + lastRefreshedAt.value = new Date() + } catch (error) { + rawRefreshToken.value = null + rawToken.value = null + } } } }) diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 13f4d7a1..4e296fe1 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -194,8 +194,9 @@ type ProviderLocalRefresh = Omit & { /** * When refreshOnlyToken is set, only the token will be refreshed * + * @default true */ - refreshOnlyToken?: true; + refreshOnlyToken?: boolean; refreshToken?: { /**