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

loggedIn value is true if session is invalid. #339

Open
BilalShah3 opened this issue Feb 5, 2025 · 4 comments
Open

loggedIn value is true if session is invalid. #339

BilalShah3 opened this issue Feb 5, 2025 · 4 comments

Comments

@BilalShah3
Copy link

when i refresh the page than it redirects me to login page. During route change loggedIn is true.

export default defineNuxtRouteMiddleware(async() => {
    const { loggedIn } = useUserSession()
    if (!loggedIn.value) {
        return navigateTo('/auth/login')
    }
})
@atinux
Copy link
Owner

atinux commented Feb 5, 2025

Please provide a reproduction

@BilalShah3
Copy link
Author

BilalShah3 commented Feb 6, 2025

Please provide a reproduction

#https://stackblitz.com/edit/github-opumfjnu

scenario:

when i change cookie value in the browser. then i navigate to states page route middleware does not redirect me to login page unless i refresh the page than it redirects me to login page.

Copy link
Owner

atinux commented Feb 7, 2025

What if you open the preview in another window?

@BilalShah3
Copy link
Author

What if you open the preview in another window?

than it logged-out.

the below solution works but it sends session request to every navigation. Is this fuctionality handle auth utils it self.

export default defineNuxtRouteMiddleware(async() => {
      const { loggedIn, fetch: refreshSession, clear: clearSession } = useUserSession()
      
      await refreshSession()
      if (!loggedIn.value) {
        await clearSession()
        return navigateTo('/auth/login')
      }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants