0
votes

I made a login code with token with vue3, and vue-router. and I want to make redirect. Like the code below. but it is not working. why??

router.ts

import { createWebHistory, createRouter, NavigationGuard } from 'vue-router';
import routes from 'virtual:generated-pages';
import { getAuthToken } from '../../plugins/auth';

const router = createRouter({
  history: createWebHistory(),
  // routes,
  routes: routes.map((route) => {
    if (route.meta?.requiresAuth) {
      route.beforeEnter = [
        (to, from, next) => {
          if (getAuthToken()) {
            return next();
          } else {
            return next({ path: '/auth/login' });
          }
        },
      ] as NavigationGuard[];
    }
    return route;
  }),
});

export default router;

console.log(router) enter image description here