Skip to content

Commit

Permalink
Merge pull request #310 from Aymanhki/Vishal
Browse files Browse the repository at this point in the history
  • Loading branch information
Aymanhki authored Mar 26, 2024
2 parents ed23f45 + 3e4b349 commit e989f9f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
28 changes: 28 additions & 0 deletions epoch_frontend/src/modules/ProtectedRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';

// Utility function to get the "epoch_session_id" cookie
export function getSessionCookie() {
return document.cookie.split('; ').find(row => row.startsWith('epoch_session_id='));
}

// Utility function to check if the session is active based on the "epoch_session_id" cookie
export function isSessionActive() {
return !!getSessionCookie();
}

const ProtectedRoute = ({ children }) => {
const navigate = useNavigate();

useEffect(() => {
// Check if the session is active; if not, redirect to the login page
if (!isSessionActive()) {
navigate('/epoch/login');
}
}, [navigate]);

// Render the children if the session is active
return children;
};

export default ProtectedRoute;
15 changes: 8 additions & 7 deletions epoch_frontend/src/modules/Router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {BrowserRouter, Route, Routes, Navigate, useParams} from 'react-router-dom';
import ProtectedRoute from './ProtectedRoute';

import Login from '../pages/login';
import Register from '../pages/register';
Expand Down Expand Up @@ -28,14 +29,14 @@ function Router() {
<Route path="/epoch/hashtags/:hashtag" element={<Hashtag />} />
<Route path="/epoch/comments/:post-id" element={<Comments />} />

<Route path="/epoch/favorites" element={<Favorites />} />
<Route path="/epoch/favorites" element={<ProtectedRoute><Favorites /></ProtectedRoute>} />
<Route path="/epoch/login" element={<Login />} />
<Route path="/epoch/register" element={<Register />} />
<Route path="/epoch/home" element={<Home />} />
<Route path="/epoch/:username" element={<ProfileRedirect />} />
<Route path="/epoch/userlist" element={<Userlist/>} />
<Route path="/epoch/search" element={<Userlist/>} />
<Route path="/:username" element={<ProfileRedirect />} />
<Route path="/epoch/home" element={<ProtectedRoute><Home /></ProtectedRoute>} />
<Route path="/epoch/:username" element={<ProtectedRoute><ProfileRedirect /></ProtectedRoute>} />
<Route path="/epoch/userlist" element={<ProtectedRoute><Userlist /></ProtectedRoute>} />
<Route path="/epoch/search" element={<ProtectedRoute><Userlist /></ProtectedRoute>} />
<Route path="/:username" element={<ProtectedRoute><ProfileRedirect /></ProtectedRoute>} />


{/* Catch-all route for 404 */}
Expand All @@ -60,4 +61,4 @@ function ProfileRedirect() {
}
}

export default Router;
export default Router;

0 comments on commit e989f9f

Please sign in to comment.