tiefen_space_h5/components/WithAuth/index.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-07-03 19:59:39 +08:00
import { checkAuth } from "@/utils/auth";
2024-07-22 14:38:59 +08:00
import { useRouter, usePathname } from "next/navigation";
2024-07-19 16:22:43 +08:00
import { useEffect } from "react";
import { get } from "@/utils/storeInfo";
2024-07-22 14:38:59 +08:00
import { Toast } from "antd-mobile";
2024-07-03 19:59:39 +08:00
export default function WithAuth(WrappedComponent) {
2024-07-19 16:22:43 +08:00
const router = useRouter();
2024-07-22 14:38:59 +08:00
const pathname = usePathname();
2024-07-19 16:22:43 +08:00
useEffect(() => {
2024-07-22 14:38:59 +08:00
if(!pathname.includes("webView") && !pathname.includes("login") )
2024-07-19 16:22:43 +08:00
checkLogin();
2024-07-22 14:38:59 +08:00
console.log("isLogin",!pathname.includes("webView") && !pathname.includes("login"))
}, [pathname]);
2024-07-19 16:22:43 +08:00
const checkLogin = async () => {
2024-07-22 14:38:59 +08:00
const hasToken = await get("token");
2024-07-19 16:22:43 +08:00
console.log("hasToken", hasToken);
if (hasToken) {
const hasToken = get("token");
console.log("hasToken", hasToken);
if (hasToken) {
const currentIsLogin = await checkAuth();
if (!currentIsLogin) {
2024-07-22 14:38:59 +08:00
Toast.show({
icon: "fail",
content: "当前登录失效,请重新登录",
position: "top",
});
router.push("/login");
2024-07-19 16:22:43 +08:00
}
}
2024-07-22 14:38:59 +08:00
} else {
Toast.show({
icon: "fail",
content:"当前登录失效,请重新登录",
position: "top",
});
router.push("/login");
2024-07-19 16:22:43 +08:00
}
};
return WrappedComponent;
2024-07-03 19:59:39 +08:00
}