Skip to content

Commit

Permalink
fix (refresh-token): handle error in refresh-token plugin and fix ref…
Browse files Browse the repository at this point in the history
…reshOnlyToken type (#667)

Co-authored-by: Marsel Shayhin <18054980+phoenix-ru@users.noreply.github.com>
  • Loading branch information
anjarupnik and phoenix-ru authored Apr 4, 2024
1 parent 28fce0f commit a8000e0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
6 changes: 3 additions & 3 deletions docs/content/2.configuration/2.nuxt-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
73 changes: 39 additions & 34 deletions src/runtime/plugins/refresh-token.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,57 @@ export default defineNuxtPlugin({
[configToken.token.headerName]: token.value
} as HeadersInit)

const response = await _fetch<Record<string, any>>(nuxtApp, path, {
method,
body: {
refreshToken: refreshToken.value
},
headers
})
try {
const response = await _fetch<Record<string, any>>(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
}
}
}
})
3 changes: 2 additions & 1 deletion src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ export type ProviderLocalRefresh = Omit<ProviderLocal, 'type'> & {
/**
* When refreshOnlyToken is set, only the token will be refreshed
*
* @default true
*/
refreshOnlyToken?: true;
refreshOnlyToken?: boolean;

refreshToken?: {
/**
Expand Down

0 comments on commit a8000e0

Please sign in to comment.