Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RNF03: RBAC #53

Merged
merged 7 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 59 additions & 54 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SnackbarContext, SnackbarState } from './hooks/snackbarContext';

import { Snackbar } from '@mui/joy';
import ProtectedRoute from './components/common/ProtectedRoute';
import { EmployeeBodyType, EmployeeContext } from './hooks/employeeContext';
import Auth from './pages/Auth';
import Clients from './pages/Clients';
import Employees from './pages/Employees';
Expand All @@ -17,6 +18,7 @@ import { RoutesPath } from './utils/constants';

function App() {
const [state, setState] = useState<SnackbarState>({ open: false, message: '' });
const [employee, setEmployee] = useState<EmployeeBodyType | null>(null);

useEffect(() => {
if (state.open) {
Expand All @@ -29,61 +31,64 @@ function App() {
);
}
}, [state]);

return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<SnackbarContext.Provider value={{ state, setState }}>
<Router>
<Routes>
{<Route path={RoutesPath.ROOT} element={<Auth />} />}
<Route element={<Layout children={<Outlet />} />}>
<Route
path={RoutesPath.HOME}
element={
<ProtectedRoute>
<Home />
</ProtectedRoute>
}
/>
<Route
path={`${RoutesPath.PROJECTS}/*`}
element={
<ProtectedRoute>
<Projects />
</ProtectedRoute>
}
/>
<Route
path={RoutesPath.TASKS}
element={
<ProtectedRoute>
<Tasks />
</ProtectedRoute>
}
/>
<Route
path={`${RoutesPath.CLIENTS}/*`}
element={
<ProtectedRoute>
<Clients />
</ProtectedRoute>
}
/>
<Route
path={RoutesPath.EMPLOYEES}
element={
<ProtectedRoute>
<Employees />
</ProtectedRoute>
}
/>
</Route>
</Routes>
</Router>
<Snackbar open={state.open} color={state.type ?? 'neutral'} variant='solid'>
{state.message}
</Snackbar>
</SnackbarContext.Provider>
</LocalizationProvider>
<EmployeeContext.Provider value={{ employee, setEmployee }}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<SnackbarContext.Provider value={{ state, setState }}>
<Router>
<Routes>
{<Route path={RoutesPath.ROOT} element={<Auth />} />}
<Route element={<Layout children={<Outlet />} />}>
<Route
path={RoutesPath.HOME}
element={
<ProtectedRoute>
<Home />
</ProtectedRoute>
}
/>
<Route
path={`${RoutesPath.PROJECTS}/*`}
element={
<ProtectedRoute>
<Projects />
</ProtectedRoute>
}
/>
<Route
path={RoutesPath.TASKS}
element={
<ProtectedRoute>
<Tasks />
</ProtectedRoute>
}
/>
<Route
path={`${RoutesPath.CLIENTS}/*`}
element={
<ProtectedRoute>
<Clients />
</ProtectedRoute>
}
/>
<Route
path={RoutesPath.EMPLOYEES}
element={
<ProtectedRoute>
<Employees />
</ProtectedRoute>
}
/>
</Route>
</Routes>
</Router>
<Snackbar open={state.open} color={state.type ?? 'neutral'} variant='solid'>
{state.message}
</Snackbar>
</SnackbarContext.Provider>
</LocalizationProvider>
</EmployeeContext.Provider>
);
}

Expand Down
7 changes: 5 additions & 2 deletions src/components/common/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ReactNode } from 'react';
import { ReactNode, useContext } from 'react';
import { useLocation } from 'react-router-dom';
import { EmployeeContext } from '../../hooks/employeeContext';
import { RoutesPath } from '../../utils/constants';
import Header from './Header';
import SideBar from './SideBar';
Expand All @@ -11,6 +12,8 @@ type LayoutProps = {
const Layout = ({ children }: LayoutProps) => {
const location = useLocation();

const { employee } = useContext(EmployeeContext);

const pathToText = () => {
if (location.pathname === RoutesPath.CLIENTS) return 'Clients';
if (location.pathname === RoutesPath.PROJECTS) return 'Projects';
Expand All @@ -19,7 +22,7 @@ const Layout = ({ children }: LayoutProps) => {
if (location.pathname === `${RoutesPath.PROJECTS}/new`) return 'New Project';
if (location.pathname.startsWith(`${RoutesPath.PROJECTS}/report/`)) return 'Project Report';

return 'Welcome Back';
return `Welcome Back, ${employee?.employee.firstName}!`;
};

return (
Expand Down
28 changes: 28 additions & 0 deletions src/hooks/employeeContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createContext } from 'react';

type EmployeeDetails = {
id: string;
firstName: string;
lastName: string;
email: string;
imageUrl: string;
createdAt: string;
idDepartment: string;
idRole: string;
};

export type EmployeeBodyType = {
employee: EmployeeDetails;
role: string;
department: string;
};

export type EmployeeContextType = {
employee: EmployeeBodyType | null;
setEmployee: (employee: EmployeeBodyType) => void;
};

export const EmployeeContext = createContext<EmployeeContextType>({
employee: null,
setEmployee: () => {},
});
27 changes: 13 additions & 14 deletions src/pages/Auth/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react';
import { useNavigate } from 'react-router-dom';

import googleImage from '../../assets/images/google-logo.webp';
import loginImage from '../../assets/images/login-image.png';
import { RoutesPath } from '../../utils/constants';

import Button from '@mui/joy/Button';

import { signInWithPopup } from 'firebase/auth';
import { getToken } from 'firebase/messaging';
import React, { useContext } from 'react';
import { useNavigate } from 'react-router-dom';
import googleImage from '../../assets/images/google-logo.webp';
import loginImage from '../../assets/images/login-image.png';
import { auth, messaging, provider } from '../../config/firebase.config';
import { EmployeeContext } from '../../hooks/employeeContext';
import { RoutesPath } from '../../utils/constants';

const Auth: React.FC = () => {
const navigate = useNavigate();
const API_BASE_ROUTE = import.meta.env.VITE_BASE_API_URL;
const navigate = useNavigate();

const { setEmployee } = useContext(EmployeeContext);

const handleGoogleSignIn = async () => {
try {
Expand All @@ -29,18 +29,17 @@ const Auth: React.FC = () => {
'Content-Type': 'application/json',
Authorization: `Bearer ${idToken}`,
},
body: JSON.stringify({
name: result.user.displayName,
email: result.user.email,
imageUrl: result.user.photoURL,
}),
});

if (!response.ok) {
throw new Error('Failed to sign up');
}

const responseData = await response.json();

setEmployee(responseData.data);
handleGetDeviceToken(result.user.email);

navigate(RoutesPath.HOME);
} catch (error) {
console.error('Firebase Sign-in error:', error);
Expand Down
Loading