Skip to content
Merged
Changes from 1 commit
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
10 changes: 2 additions & 8 deletions src/runtime/plugins/refresh-token.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@ export default defineNuxtPlugin({
const { rawToken, rawRefreshToken, refreshToken, token, lastRefreshedAt }
= useAuthState()

if (refreshToken.value && token.value) {
if (refreshToken.value && !token.value) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I fully agree with the proposed implementation, I think that we still should conditionally add the access token to headers as it was before to keep compatibility with servers which expect it:

Suggested change
if (refreshToken.value && !token.value) {
if (refreshToken.value) {
// include header in case of auth is required to avoid 403 rejection
const headers = token.value
  ? new Headers({
      [provider.token.headerName]: token.value
    } as HeadersInit)
  : undefined

Otherwise it can be considered a breaking change.

Note for the context: local provider would likely be deprecated in version 2 due to sometimes ambiguous implementations like the current one, where library doesn't know the full usecase, such as what is required by the underlying back-end for the refresh call. Instead, users will be able to customize the behaviour and exact requirements of their backend when #1062 lands (currently pending team review).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @phoenix-ru, thanks for the quick review. I’ve applied your suggestion. Keeping the access token in the headers conditionally makes sense here to preserve compatibility with backends that require it and to avoid a breaking change.

I also like the direction of the new approach, because cases like this show how hard it is for the library to support every backend-specific use case. For example, with the current approach, we may refresh the token unnecessarily on every page load, since we do not check whether the existing token is expired first.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will do a functional check tomorrow and we can merge this

const provider = useTypedBackendConfig(useRuntimeConfig(), 'local')

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({
[provider.token.headerName]: token.value
} as HeadersInit)

try {
const response = await _fetch<Record<string, any>>(nuxtApp, path, {
method,
body: objectFromJsonPointer(refreshRequestTokenPointer, refreshToken.value),
headers
body: objectFromJsonPointer(refreshRequestTokenPointer, refreshToken.value)
})

const tokenPointer = provider.refresh.token.refreshResponseTokenPointer || provider.token.signInResponseTokenPointer
Expand Down
Loading