Skip to content
Discussion options

You must be logged in to vote

getUserSession() only reads the stored session (it's this.sessionStorage.get() in nhost.ts). It never checks expiry or refreshes, so after the page has been idle the guard lets you through with an expired access token.

Use refreshSession() instead. It's built for this: it only calls the auth server when the access token expires within marginSeconds (default 60). Otherwise it returns the stored session without a network request, and it returns null if there's no session or the refresh fails:

router.beforeEach(async (to) => {
  if (to.meta.requiresAuth === false) return true

  const session = await nhost.refreshSession() // refreshes only when close to expiry
  return session?.user?.id ? true

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@MarcelloTheArcane
Comment options

@dbarrosop
Comment options

Answer selected by MarcelloTheArcane
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants