Skip to content

Commit

Permalink
fix: Refresh should be able to trigger if its token is known. Optiona…
Browse files Browse the repository at this point in the history
…lly send auth token too for the backends that might require it.
  • Loading branch information
cip8 committed Sep 6, 2024
1 parent ea5dfd6 commit dbf4cf7
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/runtime/plugins/refresh-token.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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<Record<string, any>>(nuxtApp, path, {
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit dbf4cf7

Please sign in to comment.