Skip to content

Commit

Permalink
Merge pull request #144 from WildCodeSchool/feat/132/route_protection…
Browse files Browse the repository at this point in the history
…_role_admin

Bloquage de la route admin et ajout des regles de linter
  • Loading branch information
AntoniSDev authored Dec 12, 2024
2 parents f2c6f33 + 06214ce commit 6d34dd7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
6 changes: 5 additions & 1 deletion frontend/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ module.exports = {
"warn",
{ allowConstantExport: true },
],
quotes: ["error", "double"],
"@typescript-eslint/quotes": ["error", "double"],
eqeqeq: ["error", "always"],
indent: ["error", 2],
"no-unused-vars": "warn",
"no-console": "warn",
"no-trailing-spaces": "error",
},
};
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"dev": "graphql-codegen --config codegen.ts && vite --host",
"build": "tsc -b && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"lint": "eslint . --ext .ts,.tsx",
"preview": "vite preview",
"test": "vitest"
},
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@ import Login from "./pages/Login";
import SearchPage from "./pages/search/[searchKeywords]";
import Profile from "./pages/Profile";
import { Cart } from "./pages/Cart";
import AdminRouteProtection from "./components/AdminRouteProtection";

const App = () => {
return (
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<HomePage />} />
<Route path="register" element={<Register />} />
<Route path="admin" element={<Admin />} />
<Route
path="admin"
element={
<AdminRouteProtection>
<Admin />
</AdminRouteProtection>
}
/>
<Route path="profile" element={<Profile />} />
<Route path="login" element={<Login />} />
<Route path="/product/:productId" element={<ProductDescription />} />
<Route path="search" element={<SearchPage />} />
<Route path="search" element={<SearchPage />} />
<Route path="/search/:keyword" element={<SearchPage />} />
<Route path="register" element={<Register />} />
<Route path="cart" element={<Cart />} />
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/components/AdminRouteProtection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useContext } from "react";
import { Navigate } from "react-router-dom";
import { UserContext } from "./Layout";

function AdminRouteProtection({ children }: { children: React.ReactNode }) {
const userInfo = useContext(UserContext);

if (!userInfo.isLoggedIn || userInfo.role !== "ADMIN") {
return <Navigate to="/" replace />;
}

return <>{children}</>;
}

export default AdminRouteProtection;

0 comments on commit 6d34dd7

Please sign in to comment.