Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable user permision not working #52

Open
krispstack opened this issue Apr 4, 2024 · 1 comment
Open

Enable user permision not working #52

krispstack opened this issue Apr 4, 2024 · 1 comment

Comments

@krispstack
Copy link

`import useAuth from "app/hooks/useAuth";
import { Navigate, useLocation } from "react-router-dom";

// const userHasPermission = (pathname, user, routes) => {
// if (!user) {
// return false;
// }
// const matched = routes.find((r) => r.path === pathname);

// const authenticated =
// matched && matched.auth && matched.auth.length ? matched.auth.includes(user.role) : true;
// return authenticated;
// };

const AuthGuard = ({ children }) => {
const { pathname } = useLocation();
const { isAuthenticated } = useAuth();
// const flatRoutes = flat(routes);

// const hasPermission = userHasPermission(pathname, user, routes);
// let authenticated = isAuthenticated && hasPermission;

// // IF YOU NEED ROLE BASED AUTHENTICATION,
// // UNCOMMENT ABOVE LINES
// // AND COMMENT OUT BELOW authenticated VARIABLE

if (isAuthenticated) return <>{children}</>;

return <Navigate replace to="/session/signin" state={{ from: pathname }} />;
};`

export default AuthGuard;

changed to

`import useAuth from "app/hooks/useAuth";
import { Navigate, useLocation } from "react-router-dom";
import routes from "app/routes";
//import {flat} from 'react'

const userHasPermission = (pathname, user, routes) => {
if (!user) {
return false;
}
const matched = routes.find((r) => r.path === pathname);

const authenticated =
matched && matched.auth && matched.auth.length ? matched.auth.includes(user.role) : true;
return authenticated;
};

export default function AuthGuard({ children }) {
const { pathname } = useLocation();
const { isAuthenticated,user } = useAuth();
const flatRoutes = flat(routes);

const hasPermission = userHasPermission(pathname, user, flatRoutes);
let authenticated = isAuthenticated && hasPermission;

// // IF YOU NEED ROLE BASED AUTHENTICATION,
// // UNCOMMENT ABOVE LINES
// // AND COMMENT OUT BELOW authenticated VARIABLE

if (authenticated) return <>{children}</>;

return <Navigate replace to="/session/signin" state={{ from: pathname }} />;
}`

issue

`Failed to compile.

[eslint]
src/app/auth/AuthGuard.jsx
Line 19:22: 'flat' is not defined no-undef
Line 19:27: 'routes' is not defined no-undef

Search for the keywords to learn more about each error.
ERROR in [eslint]
src/app/auth/AuthGuard.jsx
Line 19:22: 'flat' is not defined no-undef
Line 19:27: 'routes' is not defined no-undef

Search for the keywords to learn more about each error.

webpack compiled with 1 error`

im a pro user, please support

@krispstack
Copy link
Author

how to setup RBAC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant