diff --git a/client/src/App.js b/client/src/App.js index 3255440..c6daecc 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -77,6 +77,7 @@ import RouteReportWithSignature from './components/trans-routes/RouteReportWithS import Layout from "./components/home/layout"; import Home from "./components/home/home"; +import NoDashboardAccess from "./components/home/NoDashboardAccess"; import Seating from "./components/seating/Seating"; import CenterCalendar from "./components/center-calendar/CenterCalendar"; @@ -231,6 +232,7 @@ function App() { } /> } /> + } /> } /> } /> diff --git a/client/src/components/admin-view/AdminView.js b/client/src/components/admin-view/AdminView.js index 676b713..89d6216 100644 --- a/client/src/components/admin-view/AdminView.js +++ b/client/src/components/admin-view/AdminView.js @@ -73,7 +73,7 @@ const AdminView = () => { useEffect(() => { // Check if user has admin access - if (!AuthService.canAccessLegacySystem()) { + if (!AuthService.canViewAdminView()) { window.alert('You haven\'t login yet OR this user does not have access to this page. Please change an admin account to login.'); AuthService.logout(); window.location.href = '/login'; diff --git a/client/src/components/dashboard/Dashboard.js b/client/src/components/dashboard/Dashboard.js index 3efe0e0..a975135 100644 --- a/client/src/components/dashboard/Dashboard.js +++ b/client/src/components/dashboard/Dashboard.js @@ -1,4 +1,5 @@ import React, { useState, useEffect } from 'react'; +import { useNavigate } from 'react-router-dom'; import { Breadcrumb, BreadcrumbItem, Card, Row, Col, Dropdown, Spinner } from 'react-bootstrap'; import { AuthService, EventsService, CustomerService, TransRoutesService, ResourceService } from '../../services'; import DashboardCustomersList from './DashboardCustomersList'; @@ -7,6 +8,7 @@ import moment from 'moment'; import './Dashboard.css'; const Dashboard = () => { + const navigate = useNavigate(); const [todayAttendance, setTodayAttendance] = useState(0); const [todayMedicalAppointments, setTodayMedicalAppointments] = useState(0); const [membersCount, setMembersCount] = useState(0); @@ -195,6 +197,10 @@ const Dashboard = () => { }; useEffect(() => { + if (!AuthService.canViewDashboard()) { + navigate('/home-empty'); + return; + } setShowSpinner(true); Promise.all([ fetchTodayAttendance(), @@ -208,7 +214,7 @@ const Dashboard = () => { ]).finally(() => { setShowSpinner(false); }); - }, []); + }, [navigate]); // Separate useEffect for events that depends on selectedEventType, customers, and resources useEffect(() => { diff --git a/client/src/components/home/NoDashboardAccess.js b/client/src/components/home/NoDashboardAccess.js new file mode 100644 index 0000000..4f88ff8 --- /dev/null +++ b/client/src/components/home/NoDashboardAccess.js @@ -0,0 +1,7 @@ +import React from "react"; + +const NoDashboardAccess = () => { + return
; +}; + +export default NoDashboardAccess; diff --git a/client/src/components/home/home.js b/client/src/components/home/home.js index 5aff554..e8462ab 100644 --- a/client/src/components/home/home.js +++ b/client/src/components/home/home.js @@ -1,11 +1,16 @@ import { useNavigate } from 'react-router-dom'; import { useEffect } from 'react'; +import { AuthService } from '../../services'; function Home() { const navigate = useNavigate(); useEffect(() => { if (localStorage.getItem('user') && localStorage.getItem('token')) { - navigate('/dashboard/dashboard') + if (AuthService.canViewDashboard()) { + navigate('/dashboard/dashboard'); + return; + } + navigate('/home-empty'); } }) diff --git a/client/src/components/home/menu.js b/client/src/components/home/menu.js index d70774b..38fe5f7 100644 --- a/client/src/components/home/menu.js +++ b/client/src/components/home/menu.js @@ -26,7 +26,7 @@ const SideMenu = () => { name: 'Admin View', link: '/dashboard/admin-view', category: '/dashboard/admin-view', - roleFunc: AuthService.canAccessLegacySystem + roleFunc: AuthService.canViewAdminView } ] }, @@ -207,11 +207,13 @@ const SideMenu = () => { { name: 'Meal Status', link: '/meal-status', + category: '/meal-status', roleFunc: AuthService.canViewMealStatus }, { name: 'Seating Chart', link: '/seating', + category: '/seating', roleFunc: AuthService.canViewSeatingChart } ] diff --git a/client/src/components/login/Login.js b/client/src/components/login/Login.js index e5e2a5f..e902930 100644 --- a/client/src/components/login/Login.js +++ b/client/src/components/login/Login.js @@ -10,13 +10,18 @@ const Login = ({ setMenu}) => { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const navigate = useNavigate(); + const getPostLoginPath = () => { + if (AuthService.canAccessLegacySystem()) { + return '/landing'; + } + if (AuthService.canViewDashboard()) { + return '/dashboard/dashboard'; + } + return '/home-empty'; + }; useEffect(() => { if (localStorage.getItem('user') && localStorage.getItem('token')) { - if (AuthService.canAccessLegacySystem()) { - navigate(`/landing`); - } else { - navigate(`/admin`); - } + navigate(getPostLoginPath()); } }, []); const loginAndRedirect = () => { @@ -26,11 +31,7 @@ const Login = ({ setMenu}) => { }).then(({data}) => { localStorage.setItem('token', data.accessToken); localStorage.setItem('user', JSON.stringify(data)); - if (AuthService.canAccessLegacySystem()) { - navigate(`/landing`); - } else { - navigate(`/dashboard/dashboard`); - } + navigate(getPostLoginPath()); setMenu(); }).catch((error) => { window.alert(error?.response?.data?.message); diff --git a/client/src/services/AuthService.js b/client/src/services/AuthService.js index 8f2c95f..fb4383c 100644 --- a/client/src/services/AuthService.js +++ b/client/src/services/AuthService.js @@ -362,6 +362,10 @@ const canAddOrEditResources = () => { return canEditProviderInfo(); } +const canViewAdminView = () => { + return isAdmin() || hasPermission('Admin View'); +} + const canAccessLegacySystem = () => { return isAdmin() || hasAnyPermission([ 'Admin View', @@ -460,5 +464,6 @@ export const AuthService = { canAddOrEditCustomers, canAddOrEditAttendance, canViewAttendance, + canViewAdminView, canAccessLegacySystem };