Skip to content

Commit

Permalink
Merge pull request #238 from 2004vivek/main
Browse files Browse the repository at this point in the history
Issue #202 -Ensure Protection to all the routes of the website such about us and discussion
  • Loading branch information
Anuj3553 authored Oct 25, 2024
2 parents d2b078f + 1bd702c commit 6feebdc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 46 deletions.
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"/>
}
}

0 comments on commit 6feebdc

Please sign in to comment.