import type { Router, LocationQueryRaw } from 'vue-router';
import { useUserStore } from '@/store/modules/user';

export default function setupUserLoginInfoGuard(router: Router) {
  router.beforeEach(async (to, from, next) => {
    const userStore = useUserStore();
    if (to.meta.requiresAuth && !userStore.isLoggedIn) {
      if (to.name == 'login') {
        next();
        return;
      } else {
        next({
          path: '/login',
          query: { redirect: to.fullPath } as LocationQueryRaw,
        });
        return;
      }
    } else {
      next();
    }
  });
}
