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

Issue #202 -Ensure Protection to all the routes of the website such about us and discussion #238

Merged
merged 4 commits into from
Oct 25, 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
51 changes: 6 additions & 45 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import VerifyEmail from "./component/Verify";
import NotFound from "./component/NotFound";
import MiniChatbot from "./component/MiniChatbot";
import ProgressBar from "./component/ProgressBar/ProgressBar";
import ProtectedRoute from '../../client/src/component/ProtectedRoute'

// Main Layout Component

Expand Down Expand Up @@ -84,7 +85,7 @@ function App() {
};

const [progress, setProgress] = useState(0);

const [islogged,setloggedin]=useState(false)
useEffect(() => {
const savedMode = localStorage.getItem("mode");
if (savedMode) {
Expand Down Expand Up @@ -158,39 +159,9 @@ function App() {
/>
}
/>
<Route
exact
path='/discussion'
element={
<Discussion
mode={mode}
setProgress={setProgress}
showAlert={showAlert}
/>
}
/>
<Route
exact
path='/community'
element={
<Community
mode={mode}
setProgress={setProgress}
showAlert={showAlert}
/>
}
/>
<Route
exact
path='/about'
element={
<About
mode={mode}
setProgress={setProgress}
showAlert={showAlert}
/>
}
/>
<Route exact path="/discussion" element={<ProtectedRoute loggedin={islogged}><Discussion mode={mode} setProgress={setProgress} showAlert={showAlert} /></ProtectedRoute>} />
<Route exact path="/community" element={<ProtectedRoute loggedin={islogged}><Community mode={mode} setProgress={setProgress} showAlert={showAlert} /></ProtectedRoute>} />
<Route exact path="/about" element={<ProtectedRoute loggedin={islogged}><About mode={mode} setProgress={setProgress} showAlert={showAlert} /></ProtectedRoute>} />
<Route
exact
path='/myprofile'
Expand Down Expand Up @@ -224,17 +195,7 @@ function App() {
/>
}
/>
<Route
exact
path='/login'
element={
<Login
mode={mode}
setProgress={setProgress}
showAlert={showAlert}
/>
}
/>
<Route exact path="/login" element={<Login mode={mode} setProgress={setProgress} showAlert={showAlert} loggedin={islogged} setloggedin={setloggedin}/>} />
<Route
exact
path='/signup'
Expand Down
3 changes: 2 additions & 1 deletion client/src/component/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import toast from "react-hot-toast";

const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || 'https://bitbox-uxbo.onrender.com';

const Login = ({ mode, showAlert }) => {
const Login = ({ mode, showAlert,isloggedin,setloggedin }) => {
const [credentials, setCredentials] = useState({ email: "", password: "" });
const [loading, setLoading] = useState(false);

Expand All @@ -36,6 +36,7 @@ const Login = ({ mode, showAlert }) => {
localStorage.setItem("token", json.authtoken);
showAlert("Logged in Successfully", "success");
toast.success("Login Successfully!");
setloggedin(!isloggedin)
navigate("/");
} else {
showAlert("Invalid Credentials", "danger");
Expand Down
11 changes: 11 additions & 0 deletions client/src/component/ProtectedRoute.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import { Navigate } from 'react-router-dom'
export default function ProtectedRoute({loggedin,children}) {
console.log(loggedin);
if(loggedin){
return children
}
else{
return <Navigate to="/login"/>
}
}
Loading