Skip to content

Commit 11ecd17

Browse files
committed
Add Route Protection
1 parent b7a8e54 commit 11ecd17

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

frontend/src/App.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import AuthenticationPage, {
1717
action as authAction,
1818
} from './pages/Authentication';
1919
import { action as logoutAction } from './pages/Logout';
20-
import { tokenLoader } from './util/auth';
20+
import { checkAuthLoader, tokenLoader } from './util/auth';
2121

2222
const router = createBrowserRouter([
2323
{
@@ -56,13 +56,15 @@ const router = createBrowserRouter([
5656
path: 'edit',
5757
element: <EditEventPage />,
5858
action: manipulateEventAction,
59+
loader: checkAuthLoader,
5960
},
6061
],
6162
},
6263
{
6364
path: 'new',
6465
element: <NewEventPage />,
6566
action: manipulateEventAction,
67+
loader: checkAuthLoader,
6668
},
6769
],
6870
},

frontend/src/util/auth.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { redirect } from 'react-router-dom';
2+
13
export function getAuthToken() {
24
const token = localStorage.getItem('token');
35
return token;
@@ -6,3 +8,13 @@ export function getAuthToken() {
68
export function tokenLoader() {
79
return getAuthToken();
810
}
11+
12+
export function checkAuthLoader() {
13+
const token = getAuthToken();
14+
15+
if (!token) {
16+
return redirect('/auth');
17+
}
18+
19+
return null;
20+
}

0 commit comments

Comments
 (0)