fix
This commit is contained in:
@@ -3,13 +3,26 @@ import { Navigate, useLocation } from "react-router-dom";
|
||||
import { useAuth } from "@/contexts/AuthContext";
|
||||
|
||||
const PUBLIC_PATHS = ['login', '404','home'];
|
||||
|
||||
export const ProtectedRoute = ({ children }) => {
|
||||
const { user } = useAuth();
|
||||
const location = useLocation();
|
||||
const currentPath = location.pathname.replace(/^\//, '');
|
||||
|
||||
// 如果是公共路径,直接显示
|
||||
if (PUBLIC_PATHS.includes(currentPath) || currentPath === '*') {
|
||||
return children;
|
||||
}
|
||||
|
||||
// 如果用户未登录,重定向到登录页面,并携带当前路径
|
||||
if (!user?.id) {
|
||||
return <Navigate
|
||||
to={`/login?redirectTo=${encodeURIComponent(location.pathname + location.search)}`}
|
||||
replace
|
||||
/>;
|
||||
}
|
||||
|
||||
// 如果用户已登录,检查权限
|
||||
if (user?.id) {
|
||||
const hasPermission = user.menukeys?.some(key => {
|
||||
return currentPath === key || currentPath.startsWith(`${key}/`);
|
||||
|
||||
@@ -8,14 +8,13 @@ import { v4 as uuidv4 } from "uuid";
|
||||
const AuthContext = createContext({});
|
||||
|
||||
export const AuthProvider = ({ children }) => {
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const [searchParams] = useSearchParams();
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
const [user, setUser] = useState({});
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
//处理google登录
|
||||
const hash = window.location.hash.substring(1);
|
||||
const hashParams = new URLSearchParams(hash);
|
||||
const accessToken = hashParams.get("access_token");
|
||||
@@ -62,12 +61,7 @@ export const AuthProvider = ({ children }) => {
|
||||
}
|
||||
}, [user]);
|
||||
|
||||
// useEffect(() => {
|
||||
// const redirectTo = searchParams.get("redirectTo");
|
||||
// if (redirectTo) {
|
||||
// navigate(redirectTo);
|
||||
// }
|
||||
// }, [location.pathname]);
|
||||
|
||||
|
||||
//检查时候在管理模块团队中,没有就自动加入
|
||||
const checkInTeam = async (user) => {
|
||||
@@ -121,7 +115,21 @@ export const AuthProvider = ({ children }) => {
|
||||
message.error(error.message || "登录失败,请稍后重试");
|
||||
return;
|
||||
}
|
||||
setUser(data.user);
|
||||
|
||||
const role = await checkInTeam(data.user);
|
||||
const menukey = await fetchMenuList(role);
|
||||
setUser({ ...data.user, adminRole: role, menukeys: menukey });
|
||||
|
||||
// 获取重定向路径
|
||||
const redirectTo = searchParams.get("redirectTo");
|
||||
if (redirectTo) {
|
||||
// 如果有重定向路径,则导航到该路径
|
||||
navigate(decodeURIComponent(redirectTo), { replace: true });
|
||||
} else {
|
||||
// 如果没有重定向路径,则导航到首页
|
||||
navigate("/home", { replace: true });
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
message.error(error.message || "登录失败,请稍后重试");
|
||||
@@ -134,13 +142,12 @@ export const AuthProvider = ({ children }) => {
|
||||
const signInWithGoogle = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const redirectTo = searchParams.get("redirectTo");
|
||||
const redirectTo = searchParams.get("redirectTo") || "/home";
|
||||
|
||||
const { data, error } = await supabase.auth.signInWithOAuth({
|
||||
provider: "google",
|
||||
options: {
|
||||
redirectTo: `${window.location.origin}/login?redirectTo=${
|
||||
redirectTo ?? ""
|
||||
}`,
|
||||
redirectTo: `${window.location.origin}/login?redirectTo=${encodeURIComponent(redirectTo)}`,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -182,13 +189,13 @@ export const AuthProvider = ({ children }) => {
|
||||
}
|
||||
};
|
||||
const fetchMenuList = async (role) => {
|
||||
if(!role||!user?.id) return;
|
||||
if(!role) return;
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from('resources')
|
||||
.select('*')
|
||||
.eq('type', 'menuKey')
|
||||
.eq('attributes->>roleName', role) // 添加这行来筛选 OWNER 角色
|
||||
.eq('attributes->>roleName', role)
|
||||
.single();
|
||||
if (error) throw error;
|
||||
if(data?.attributes?.menuKeys){
|
||||
@@ -214,7 +221,8 @@ export const AuthProvider = ({ children }) => {
|
||||
}
|
||||
setUser({});
|
||||
message.success("已成功登出");
|
||||
navigate(`/login?redirectTo=${location.pathname}`, { replace: true });
|
||||
// 保存当前完整路径作为重定向 URL
|
||||
navigate(`/login?redirectTo=${encodeURIComponent(location.pathname + location.search)}`, { replace: true });
|
||||
} catch (error) {
|
||||
message.error(error.message || "登出失败,请稍后重试");
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user