Skip to content

Commit 47e95d4

Browse files
committed
chore(auth): refactor routes to use PrivateRoute guard
1 parent 516ed7f commit 47e95d4

File tree

3 files changed

+58
-27
lines changed

3 files changed

+58
-27
lines changed

src/index.jsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import { createBrowserHistory } from 'history';
44
import { BrowserRouter as Router, Route, Routes, Navigate } from 'react-router-dom';
5+
import { AuthProvider } from './ui/auth/AuthProvider';
56

67
// core components
78
import Admin from './ui/layouts/Admin';
89
import Login from './ui/views/Login/Login';
910
import './ui/assets/css/material-dashboard-react.css';
11+
import NotAuthorized from './ui/views/Extras/NotAuthorized';
12+
import NotFound from './ui/views/Extras/NotFound';
1013

1114
const hist = createBrowserHistory();
1215

1316
ReactDOM.render(
14-
<Router history={hist}>
15-
<Routes>
16-
<Route exact path='/admin/*' element={<Admin />} />
17-
<Route exact path='/login' element={<Login />} />
18-
<Route exact path='/' element={<Navigate from='/' to='/admin/repo' />} />
19-
</Routes>
20-
</Router>,
17+
<AuthProvider>
18+
<Router history={hist}>
19+
<Routes>
20+
<Route exact path='/admin/*' element={<Admin />} />
21+
<Route exact path='/login' element={<Login />} />
22+
<Route exact path='/not-authorized' element={<NotAuthorized />} />
23+
<Route exact path='/' element={<Navigate from='/' to='/admin/repo' />} />
24+
<Route path='*' element={<NotFound />} />
25+
</Routes>
26+
</Router>
27+
</AuthProvider>,
2128
document.getElementById('root'),
2229
);

src/routes.jsx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
1717
*/
1818

19+
import React from 'react';
20+
import PrivateRoute from './ui/components/PrivateRoute/PrivateRoute';
1921
import Person from '@material-ui/icons/Person';
2022
import OpenPushRequests from './ui/views/OpenPushRequests/OpenPushRequests';
2123
import PushDetails from './ui/views/PushDetails/PushDetails';
@@ -33,58 +35,58 @@ const dashboardRoutes = [
3335
path: '/repo',
3436
name: 'Repositories',
3537
icon: RepoIcon,
36-
component: RepoList,
38+
component: (props) => <PrivateRoute component={RepoList} />,
3739
layout: '/admin',
3840
visible: true,
3941
},
42+
{
43+
path: '/repo/:id',
44+
name: 'Repo Details',
45+
icon: Person,
46+
component: (props) => <PrivateRoute component={RepoDetails} />,
47+
layout: '/admin',
48+
visible: false,
49+
},
4050
{
4151
path: '/push',
4252
name: 'Dashboard',
4353
icon: Dashboard,
44-
component: OpenPushRequests,
54+
component: (props) => <PrivateRoute component={OpenPushRequests} />,
4555
layout: '/admin',
4656
visible: true,
4757
},
4858
{
4959
path: '/push/:id',
5060
name: 'Open Push Requests',
5161
icon: Person,
52-
component: PushDetails,
62+
component: (props) => <PrivateRoute component={PushDetails} />,
5363
layout: '/admin',
5464
visible: false,
5565
},
5666
{
5767
path: '/profile',
5868
name: 'My Account',
5969
icon: AccountCircle,
60-
component: User,
70+
component: (props) => <PrivateRoute component={User} />,
6171
layout: '/admin',
6272
visible: true,
6373
},
6474
{
65-
path: '/user/:id',
66-
name: 'User',
67-
icon: Person,
68-
component: User,
75+
path: '/user',
76+
name: 'Users',
77+
icon: Group,
78+
component: (props) => <PrivateRoute adminOnly component={UserList} />,
6979
layout: '/admin',
70-
visible: false,
80+
visible: true,
7181
},
7282
{
73-
path: '/repo/:id',
74-
name: 'Repo Details',
83+
path: '/user/:id',
84+
name: 'User',
7585
icon: Person,
76-
component: RepoDetails,
86+
component: (props) => <PrivateRoute adminOnly component={User} />,
7787
layout: '/admin',
7888
visible: false,
7989
},
80-
{
81-
path: '/user',
82-
name: 'Users',
83-
icon: Group,
84-
component: UserList,
85-
layout: '/admin',
86-
visible: true,
87-
},
8890
];
8991

9092
export default dashboardRoutes;

src/ui/services/auth.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const baseUrl = import.meta.env.VITE_API_URI
2+
? `${import.meta.env.VITE_API_URI}`
3+
: `${location.origin}`;
4+
5+
/**
6+
* Gets the current user's information
7+
* @return {Promise<Object>} The user's information
8+
*/
9+
export const getUserInfo = async () => {
10+
try {
11+
const response = await fetch(`${baseUrl}/api/auth/me`, {
12+
credentials: 'include', // Sends cookies
13+
});
14+
15+
if (!response.ok) throw new Error(`Failed to fetch user info: ${response.statusText}`);
16+
17+
return await response.json();
18+
} catch (error) {
19+
console.error('Error fetching user info:', error);
20+
return null;
21+
}
22+
};

0 commit comments

Comments
 (0)