From dbf4cf7a02917c5cf4dfad336988bc00591e5d84 Mon Sep 17 00:00:00 2001 From: Ciprian Cimpan Date: Fri, 6 Sep 2024 10:56:11 +0200 Subject: [PATCH] fix: Refresh should be able to trigger if its token is known. Optionally send auth token too for the backends that might require it. --- src/runtime/plugins/refresh-token.server.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/runtime/plugins/refresh-token.server.ts b/src/runtime/plugins/refresh-token.server.ts index bd79d432..c386f98b 100644 --- a/src/runtime/plugins/refresh-token.server.ts +++ b/src/runtime/plugins/refresh-token.server.ts @@ -11,7 +11,7 @@ export default defineNuxtPlugin({ const { rawToken, rawRefreshToken, refreshToken, token, lastRefreshedAt } = useAuthState() - if (refreshToken.value && token.value) { + if (refreshToken.value) { const config = nuxtApp.$config.public.auth const configToken = useTypedBackendConfig(useRuntimeConfig(), 'local') @@ -20,10 +20,12 @@ export default defineNuxtPlugin({ const { path, method } = provider.refresh.endpoint const refreshRequestTokenPointer = provider.refresh.token.refreshRequestTokenPointer - // include header in case of auth is required to avoid 403 rejection - const headers = new Headers({ - [configToken.token.headerName]: token.value - } as HeadersInit) + const headers = new Headers() + + // To perform the refresh, some backends may require the auth token to also be set. + if (token.value) { + headers.set(configToken.token.headerName, token.value) + } try { const response = await _fetch>(nuxtApp, path, { @@ -40,8 +42,7 @@ export default defineNuxtPlugin({ console.error( `Auth: string token expected, received instead: ${JSON.stringify( extractedToken - )}. Tried to find token at ${ - provider.token.signInResponseTokenPointer + )}. Tried to find token at ${provider.token.signInResponseTokenPointer } in ${JSON.stringify(response)}` ) return @@ -57,8 +58,7 @@ export default defineNuxtPlugin({ console.error( `Auth: string token expected, received instead: ${JSON.stringify( extractedRefreshToken - )}. Tried to find token at ${ - provider.refresh.token.signInResponseRefreshTokenPointer + )}. Tried to find token at ${provider.refresh.token.signInResponseRefreshTokenPointer } in ${JSON.stringify(response)}` ) return