Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (refresh-token): handle error in refresh-token plugin and fix reshOnlyToken type #667

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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