Skip to content

enableOnWindowFocus must be used only in authenticated condition #834

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

Closed
YoshimiShima opened this issue Jul 26, 2024 · 0 comments
Closed
Labels
bug A bug that needs to be resolved

Comments

@YoshimiShima
Copy link
Contributor

YoshimiShima commented Jul 26, 2024

Environment

Reproduction

// nuxt.config.ts

provider: {
  type: 'refresh',
  pages: {
    login: '/login',
  },
},
 sessionRefresh: {
   enablePeriodically: 90000,
   enableOnWindowFocus: true,
 },
globalAppMiddleware: {
   isEnabled: true,
},
// login.vue

definePageMeta({
   auth: {
     unauthenticatedOnly: true,
   },
})

Describe the bug

The enableOnWindowFocus feature should not be triggered on pages that are accessible to unauthenticated users. Currently, when a user switches back to a tab containing an unauthenticated page (such as the login page of us), the refresh mechanism is still executed, which is unnecessary and potentially problematic.

In the current implementation in src/runtime/utils/refreshHandler.ts, there's an inconsistency in how enablePeriodically and enableOnWindowFocus are handled.
For enablePeriodically, there's a check for the authenticated state:

if (enablePeriodically !== false) {
const intervalTime = enablePeriodically === true ? 1000 : enablePeriodically
this.refetchIntervalTimer = setInterval(() => {
if (this.auth?.data.value) {
this.auth.refresh()
}
}, intervalTime)
}

However, for enableOnWindowFocus, no such check exists:

visibilityHandler (): void {
// Listen for when the page is visible, if the user switches tabs
// and makes our tab visible again, re-fetch the session, but only if
// this feature is not disabled.
if (this.config?.enableOnWindowFocus && document.visibilityState === 'visible') {
this.auth?.refresh()
}
}

To resolve this issue and ensure consistency, we should add an authentication check for enableOnWindowFocus as well. A proposed solution is to add a condition like:

if (this.config?.enableOnWindowFocus && document.visibilityState === 'visible' && this.auth?.status.value !== 
'unauthenticated') {
  this.auth?.refresh()
}

This change would prevent unnecessary refresh attempts on unauthenticated pages and align the behavior with enablePeriodically.
Thanks.

Additional context

No response

Logs

FetchError: [POST] "/app/main/api/auth/refresh": 403
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug that needs to be resolved
Projects
None yet
Development

No branches or pull requests

2 participants