🐛 fix(App.tsx): add setIsAdmin function to AuthContext to update isAdmin state when user data is fetched
✨ feat(App.tsx): set isAdmin state based on user's is_superuser value to conditionally render admin-related components 🐛 fix(headerComponent/index.tsx): fix conditional rendering of Sign out, Home, and Admin page buttons based on autoLogin and isAdmin values 🐛 fix(authContext.tsx): remove unnecessary useEffect to update isAdmin state when accessToken or isAdmin changes 🔥 chore(api.tsx): remove unused sleep function ✨ feat(AdminPage/index.tsx): update text in AdminPage to provide a better description of the page's purpose 🐛 fix(routes.tsx): wrap AdminPage and ApiKeysPage components with ProtectedAdminRoute component to restrict access to admin-only routes
This commit is contained in:
parent
1d14f24a97
commit
ada752309a
6 changed files with 40 additions and 46 deletions
|
|
@ -134,7 +134,7 @@ export default function App() {
|
||||||
};
|
};
|
||||||
|
|
||||||
//this function is to get the user logged in when the page is refreshed
|
//this function is to get the user logged in when the page is refreshed
|
||||||
const { setUserData, getAuthentication, login, setAutoLogin, logout } =
|
const { setUserData, getAuthentication, login, setAutoLogin, logout, setIsAdmin } =
|
||||||
useContext(AuthContext);
|
useContext(AuthContext);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -154,6 +154,8 @@ export default function App() {
|
||||||
.then((user) => {
|
.then((user) => {
|
||||||
setUserData(user);
|
setUserData(user);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
const isSuperUser = user.is_superuser;
|
||||||
|
setIsAdmin(isSuperUser);
|
||||||
})
|
})
|
||||||
.catch((error) => {});
|
.catch((error) => {});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,34 +26,37 @@ export default function Header(): JSX.Element {
|
||||||
<Link to="/">
|
<Link to="/">
|
||||||
<span className="ml-4 text-2xl">⛓️</span>
|
<span className="ml-4 text-2xl">⛓️</span>
|
||||||
</Link>
|
</Link>
|
||||||
{location.pathname === "/admin" && (
|
|
||||||
<Button
|
|
||||||
onClick={() => {
|
|
||||||
navigate("/");
|
|
||||||
}}
|
|
||||||
variant="outline"
|
|
||||||
className=""
|
|
||||||
>
|
|
||||||
Main page
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
{autoLogin === false && (
|
{autoLogin === false && (
|
||||||
<Button
|
<a
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
logout();
|
logout();
|
||||||
navigate("/login");
|
navigate("/login");
|
||||||
}}
|
}}
|
||||||
variant="outline"
|
className="text-sm font-medium text-muted-foreground transition-colors hover:text-primary cursor-pointer mx-5"
|
||||||
className=""
|
|
||||||
>
|
>
|
||||||
Sign out
|
Sign out
|
||||||
</Button>
|
</a>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{location.pathname === "/admin" && (
|
||||||
|
<a
|
||||||
|
onClick={() => {
|
||||||
|
navigate("/");
|
||||||
|
}}
|
||||||
|
className="text-sm font-medium text-muted-foreground transition-colors hover:text-primary cursor-pointer"
|
||||||
|
>
|
||||||
|
Home
|
||||||
|
</a>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{isAdmin && !autoLogin && location.pathname !== "/admin" && (
|
{isAdmin && !autoLogin && location.pathname !== "/admin" && (
|
||||||
<Button variant="outline" onClick={() => navigate("/admin")}>
|
<a
|
||||||
|
className="text-sm font-medium text-muted-foreground transition-colors hover:text-primary cursor-pointer"
|
||||||
|
onClick={() => navigate("/admin")}
|
||||||
|
>
|
||||||
Admin page
|
Admin page
|
||||||
</Button>
|
</a>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{flows.findIndex((f) => tabId === f.id) !== -1 && tabId !== "" && (
|
{flows.findIndex((f) => tabId === f.id) !== -1 && tabId !== "" && (
|
||||||
|
|
@ -138,7 +141,7 @@ export default function Header(): JSX.Element {
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</AlertDropdown>
|
</AlertDropdown>
|
||||||
|
{!autoLogin && (
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
navigate("/account/api-keys");
|
navigate("/account/api-keys");
|
||||||
|
|
@ -149,6 +152,7 @@ export default function Header(): JSX.Element {
|
||||||
className="side-bar-button-size text-muted-foreground hover:text-accent-foreground"
|
className="side-bar-button-size text-muted-foreground hover:text-accent-foreground"
|
||||||
/>
|
/>
|
||||||
</button>
|
</button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -47,14 +47,6 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
fetchStars();
|
fetchStars();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (accessToken) {
|
|
||||||
getLoggedUser().then((user) => {
|
|
||||||
const isSuperUser = user.is_superuser;
|
|
||||||
setIsAdmin(isSuperUser);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, [accessToken, isAdmin]);
|
|
||||||
|
|
||||||
function getAuthentication() {
|
function getAuthentication() {
|
||||||
const storedRefreshToken = cookies.get("refresh_token");
|
const storedRefreshToken = cookies.get("refresh_token");
|
||||||
|
|
|
||||||
|
|
@ -103,9 +103,5 @@ function ApiInterceptor() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to sleep for a given duration in milliseconds
|
|
||||||
function sleep(ms: number) {
|
|
||||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
||||||
}
|
|
||||||
|
|
||||||
export { ApiInterceptor, api };
|
export { ApiInterceptor, api };
|
||||||
|
|
|
||||||
|
|
@ -196,7 +196,7 @@ export default function AdminPage() {
|
||||||
Welcome back!
|
Welcome back!
|
||||||
</h2>
|
</h2>
|
||||||
<p className="text-muted-foreground">
|
<p className="text-muted-foreground">
|
||||||
Here's a list of all users!
|
Navigate through this section to efficiently oversee all application users. From here, you can seamlessly manage user accounts.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center space-x-2"></div>
|
<div className="flex items-center space-x-2"></div>
|
||||||
|
|
|
||||||
|
|
@ -88,9 +88,9 @@ const Router = () => {
|
||||||
<Route
|
<Route
|
||||||
path="/admin"
|
path="/admin"
|
||||||
element={
|
element={
|
||||||
|
<ProtectedAdminRoute>
|
||||||
<AdminPage />
|
<AdminPage />
|
||||||
|
</ProtectedAdminRoute>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
@ -106,9 +106,9 @@ const Router = () => {
|
||||||
<Route
|
<Route
|
||||||
path="api-keys"
|
path="api-keys"
|
||||||
element={
|
element={
|
||||||
<ProtectedRoute>
|
<ProtectedAdminRoute>
|
||||||
<ApiKeysPage />
|
<ApiKeysPage />
|
||||||
</ProtectedRoute>
|
</ProtectedAdminRoute>
|
||||||
}
|
}
|
||||||
></Route>
|
></Route>
|
||||||
</Route>
|
</Route>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue