this only check.vue, do i have to check this in t...
# support-questions-legacy
p
this only check.vue, do i have to check this in the system every time the page changes.
Copy code
ts
<script setup lang="ts">
import * as Session from 'supertokens-web-js/recipe/session'

onMounted(async () => {
  if (await Session.doesSessionExist()) {
    // since a session already exists, we redirect the user to the HomeView.vue component
    window.location.assign('/home')
  }
})
</script>

<template>
  <IonPage>
    <IonRouterOutlet id="main" />
  </IonPage>
</template>
r
hey @productdevbook
are you talking about calling
doesSessionExist
on each page load / route change?
then yes, you should check this on each page
that you want protected
p
yes, is this the right decision
r
yes.
actually, you want to instead do if session does not exist, then redirect to the
/auth
page
p
Well, isn't this the server that will make a request every time the page changes? Or does it make requests when time runs out?
Copy code
ts
const sessionGuard: NavigationGuard = async (
  to: RouteLocationNormalized,
  from: RouteLocationNormalized,
  next: NavigationGuardNext,
) => {
  if (await Session.doesSessionExist()) {
    // Session exists
    next('/auth')
  }
  else {
    next()
  }
}

export const onBeforeEach: NavigationGuard = async (
  to: RouteLocationNormalized,
  from: RouteLocationNormalized,
  next: NavigationGuardNext,
) => {
  await sessionGuard(to, from, next)
}
add vue router.ts
r
im not sure i understand the question
p
onBeforeEach change all page working
Is it okay to check this every time you change a page?
r
yes
it is OK
it reads from the browser storage. It doesn't do any API call.. so it's quite fast
p
Copy code
ts

export const sessionGuard: NavigationGuard = async (
  to: RouteLocationNormalized,
  from: RouteLocationNormalized,
  next: NavigationGuardNext,
) => {
  const sessionExists = await Session.doesSessionExist()
  if (sessionExists) {
    if (await checkIfCookieExists()) {
      if (to.meta.auth === 'private')
        return next()
      else
        return next('/home')
    }
    return next()
  }
  else {
    const sessionExists = await Session.doesSessionExist()
    if (to.meta.auth === 'onboard' && !sessionExists)
      return next()
    else
      return next('/auth')
  }
}
When I use
await Session
, I get a white screen and the next time it doesn't start, the router breaks.
r
what does the browser console say?
p
empty
r
it seems like the backend isn't responding
p
only network see but dont login
r
this is probably an issue with your backend
p
it's always freezing in here
r
Not sure what may be happening. It;s difficult to debug without any logs or info about your setup
p
thank you, fixed
🤦‍♂️
r
right! cool!
10 Views