Skip to content

mevn-mongodb-login-req: How to add route guard #5

@jsfanatik

Description

@jsfanatik

How do you add route guards to this app? I tried adding the following in the index.js:

router.beforeEach((to, from, next) => {
  const token = localStorage.usertoken
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
  if (requiresAuth && !token) next('login')
  else if (!requiresAuth && token) next('profile')
  else next()
})
export default router

... in order to prevent the user from being logged out after refreshing the page, but to no avail. Any recommendations on how to add route guards?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions