Feature/amiriis missions
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
"use client";
|
||||
import { useAuth } from "@/lib/contexts/auth";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
|
||||
function WithAuthMiddleware({ children }) {
|
||||
const router = useRouter();
|
||||
const pathName = usePathname();
|
||||
const { isAuth, initAuthState } = useAuth();
|
||||
|
||||
useEffect(() => {
|
||||
if (!initAuthState) return;
|
||||
if (!isAuth) {
|
||||
router.replace(`${process.env.NEXT_PUBLIC_API_URL}/login?_back=${encodeURIComponent(pathName)}`);
|
||||
}
|
||||
}, [isAuth, initAuthState]);
|
||||
|
||||
if (!initAuthState || !isAuth) return null;
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
export default WithAuthMiddleware;
|
||||
"use client";
|
||||
import { useAuth } from "@/lib/contexts/auth";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
|
||||
function WithAuthMiddleware({ children }) {
|
||||
const router = useRouter();
|
||||
const pathName = usePathname();
|
||||
const { isAuth, initAuthState } = useAuth();
|
||||
|
||||
useEffect(() => {
|
||||
if (!initAuthState) return;
|
||||
if (!isAuth) {
|
||||
router.replace(`${process.env.NEXT_PUBLIC_API_URL}/login?_back=${encodeURIComponent(pathName)}`);
|
||||
}
|
||||
}, [isAuth, initAuthState]);
|
||||
|
||||
if (!initAuthState || !isAuth) return null;
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
export default WithAuthMiddleware;
|
||||
|
||||
@@ -1,37 +1,37 @@
|
||||
"use client";
|
||||
|
||||
import { Box, Typography } from "@mui/material";
|
||||
import { usePermissions } from "@/lib/hooks/usePermissions";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
function WithPermission({ children, permission_name }) {
|
||||
const { data, error, isLoading } = usePermissions();
|
||||
const [cachedData, setCachedData] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
setCachedData(data);
|
||||
}
|
||||
}, [data]);
|
||||
|
||||
if (isLoading || !cachedData || !permission_name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const hasPermission =
|
||||
permission_name.includes("all") || permission_name.some((permission) => cachedData.includes(permission));
|
||||
|
||||
if (!hasPermission) {
|
||||
return (
|
||||
<Box sx={{ display: "flex", alignItems: "center", justifyContent: "center", height: "100%" }}>
|
||||
<Typography variant="h5" sx={{ letterSpacing: "2px", color: "error.main" }}>
|
||||
شما دسترسی لازم به این صفحه را ندارید
|
||||
</Typography>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
export default WithPermission;
|
||||
"use client";
|
||||
|
||||
import { Box, Typography } from "@mui/material";
|
||||
import { usePermissions } from "@/lib/hooks/usePermissions";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
function WithPermission({ children, permission_name }) {
|
||||
const { data, error, isLoading } = usePermissions();
|
||||
const [cachedData, setCachedData] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
setCachedData(data);
|
||||
}
|
||||
}, [data]);
|
||||
|
||||
if (isLoading || !cachedData || !permission_name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const hasPermission =
|
||||
permission_name.includes("all") || permission_name.some((permission) => cachedData.includes(permission));
|
||||
|
||||
if (!hasPermission) {
|
||||
return (
|
||||
<Box sx={{ display: "flex", alignItems: "center", justifyContent: "center", height: "100%" }}>
|
||||
<Typography variant="h5" sx={{ letterSpacing: "2px", color: "error.main" }}>
|
||||
شما دسترسی لازم به این صفحه را ندارید
|
||||
</Typography>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
export default WithPermission;
|
||||
|
||||
Reference in New Issue
Block a user