import type { Router } from 'vue-router';
import NProgress from 'nprogress'; // progress bar

import { useUserStore } from '@/store';

export default function setupUserLoginInfoGuard(router: Router) {
  router.beforeEach(async (to, from, next) => {
    NProgress.start();
    const userStore = useUserStore();
    if (!to.meta.requiresAuth) {
      next();
      return;
    }
    if (userStore.token) {
      next();
    } else {
      next({
        name: 'Login',
      });
    }
  });
}
