Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Vue router guard

router.beforeEach((to, from, next) => {
  const publicPages = ['/login', '/register', '/home'];
  const authRequired = !publicPages.includes(to.path);
  const loggedIn = localStorage.getItem('user');

  if (authRequired && !loggedIn) {
    next('/profile');
  } else {
    next();
  }
});
 
PREVIOUS NEXT
Tagged: #Vue #router #guard
ADD COMMENT
Topic
Name
1+3 =