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