Skip to content

Commit 34081f3

Browse files
Copilotvinod0m
andcommitted
Fix role-based access control for dashboard admin panel link
Co-authored-by: vinod0m <221896197+vinod0m@users.noreply.github.com>
1 parent f4e0a48 commit 34081f3

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

frontend/App.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import { SettingsProvider } from './stores/settingsStore';
33
import { AuthProvider, useAuth } from './stores/authStore';
44
import { Navigation } from './components/Navigation';
@@ -12,9 +12,24 @@ import './styles.css';
1212

1313
const AppContent: React.FC = () => {
1414
const { isAuthenticated, isLoading } = useAuth();
15+
const [currentHash, setCurrentHash] = useState(window.location.hash.slice(1) || '/');
1516

16-
// Simple routing based on hash
17-
const currentHash = window.location.hash.slice(1) || '/';
17+
// Handle hash changes without page reload
18+
useEffect(() => {
19+
const handleHashChange = () => {
20+
setCurrentHash(window.location.hash.slice(1) || '/');
21+
};
22+
23+
window.addEventListener('hashchange', handleHashChange);
24+
return () => window.removeEventListener('hashchange', handleHashChange);
25+
}, []);
26+
27+
// Redirect to dashboard when authenticated and on login/register pages
28+
useEffect(() => {
29+
if (isAuthenticated && (currentHash === '/login' || currentHash === '/register')) {
30+
window.location.hash = '/dashboard';
31+
}
32+
}, [isAuthenticated, currentHash]);
1833

1934
const renderPage = () => {
2035
if (isLoading) {
@@ -59,17 +74,6 @@ const AppContent: React.FC = () => {
5974
}
6075
};
6176

62-
// Update navigation links to use hash routing
63-
React.useEffect(() => {
64-
const handleHashChange = () => {
65-
// Force re-render when hash changes
66-
window.location.reload();
67-
};
68-
69-
window.addEventListener('hashchange', handleHashChange);
70-
return () => window.removeEventListener('hashchange', handleHashChange);
71-
}, []);
72-
7377
return (
7478
<SettingsProvider>
7579
<div className="min-h-screen bg-gray-50">

frontend/components/Dashboard.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const RoleBadge: React.FC<{ role: Role }> = ({ role }) => {
2727
};
2828

2929
export const Dashboard: React.FC = () => {
30-
const { user, logout, isLoading } = useAuth();
30+
const { user, logout, isLoading, hasRole } = useAuth();
3131

3232
if (!user) {
3333
return null;
@@ -120,12 +120,14 @@ export const Dashboard: React.FC = () => {
120120

121121
<div className="px-6 py-4">
122122
<div className="flex flex-wrap gap-4">
123-
<a
124-
href="#/admin"
125-
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
126-
>
127-
Admin Panel
128-
</a>
123+
{hasRole('admin') && (
124+
<a
125+
href="#/admin"
126+
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
127+
>
128+
Admin Panel
129+
</a>
130+
)}
129131

130132
<button
131133
onClick={handleRefreshProfile}

0 commit comments

Comments
 (0)