import { useAuthStore } from '@/stores/auth';
import type { NavigationGuardNext, RouteLocation } from 'vue-router';

export default async function (
  to: RouteLocation,
  _from: RouteLocation,
  next: NavigationGuardNext,
) {
  console.log(to, _from);
  const $auth = useAuthStore();

  await $auth.getMe();

  if ($auth.loggedIn) {
    if (to.path === '/auth') {
      next('/');
    }
  } else {
    if (to.path !== '/auth') {
      return next('/auth');
    }
  }
}
