Skip to content

Commit

Permalink
gg
Browse files Browse the repository at this point in the history
  • Loading branch information
boody committed Apr 26, 2024
1 parent 802352c commit 46223c2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { ToastContainer } from "react-toastify";
import { RecoilRoot } from "recoil";
import NotLoggedIn from "./Components/ProtectedRoutes/NotLoggedIn.js";
import IsloggedIn from "./Components/ProtectedRoutes/IsloggedIn.js";
import IsAdmin from "./Components/ProtectedRoutes/IsAdmin.js";
import IsOwenr from "./Components/ProtectedRoutes/IsOwenr.js";
export default function App() {
return (
<div className="App">
Expand All @@ -43,12 +45,16 @@ export default function App() {
} />
<Route path="Dashboard" element={
<IsloggedIn>
<Dashboard />
<IsAdmin>
<Dashboard />
</IsAdmin>
</IsloggedIn>
} />
<Route path="Users" element={
<IsloggedIn>
<IsOwenr>
<Users />
</IsOwenr>
</IsloggedIn>
} />
<Route path="APPOINTMENT" element={
Expand Down
14 changes: 14 additions & 0 deletions src/Components/ProtectedRoutes/IsAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { Fragment } from 'react';
import { useRecoilState } from 'recoil';
import { Navigate } from 'react-router-dom';
import $AuthData from '../../store/index';

export default function IsAdmin({ children }) {
const [authRecoil] = useRecoilState($AuthData);

if (authRecoil.role !== 'owner' && authRecoil.role !== 'admin') {
return <Navigate to="/" />;
}

return <Fragment>{children}</Fragment>;
}
14 changes: 14 additions & 0 deletions src/Components/ProtectedRoutes/IsOwenr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { Fragment } from 'react';
import { useRecoilState } from 'recoil';
import { Navigate } from 'react-router-dom';
import $AuthData from '../../store/index';

export default function IsOwenr({ children }) {
const [authRecoil] = useRecoilState($AuthData);

if (authRecoil.role !== 'owner') {
return <Navigate to="/" />;
}

return <Fragment>{children}</Fragment>;
}
15 changes: 10 additions & 5 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { atom } from "recoil";

const localData = JSON.parse(localStorage.getItem('loginUser'));
const defaultAuthData = {
isAuth: false,
user: null,
role: null
};

if (localData && localData.role) {
defaultAuthData.role = localData.role;
}

const $AuthData = atom({
key: "$AuthData",
default: localData || {
isAuth: false,
user: null,
role: null || localData.role
},
default: localData || defaultAuthData,
});

export default $AuthData;

0 comments on commit 46223c2

Please sign in to comment.