import type { RouteLocationRaw } from "vue-router";
import { ensureValidAuthToken } from "@/utils/request";
import { goLogin } from "./useNavigator";

function stopCurrentAction(): Promise<void> {
  return new Promise(() => {});
}

export const checkToken = async (
  redirect?: RouteLocationRaw,
): Promise<void> => {
  try {
    const token = await ensureValidAuthToken();
    if (!token) {
      goLogin({ redirect });
      return stopCurrentAction();
    }
  } catch {
    goLogin({ redirect });
    return stopCurrentAction();
  }
};
