tiefen_space_h5/components/WithAuth/index.js

44 lines
1.2 KiB
JavaScript

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