import { useDidShow } from "@tarojs/taro";
import Taro from "@tarojs/taro";
import { getToken } from "../utils/auth";
import { currentPageUrl } from "../utils/onboarding";

export function useAuthGuard() {
  useDidShow(() => {
    const token = getToken();
    if (!token) {
      const redirect = currentPageUrl();
      void Taro.reLaunch({ url: redirect ? `/pages/login/index?redirect=${encodeURIComponent(redirect)}` : "/pages/login/index" });
    }
  });
}
