From ef4355dfb8989022aa3032df3b36128a8b9f217b Mon Sep 17 00:00:00 2001 From: amNobodyyy Date: Tue, 28 Jan 2025 12:03:22 +0530 Subject: [PATCH] protected route --- client/package-lock.json | 10 ++++++++++ client/package.json | 1 + client/src/App.js | 19 +++++++------------ client/src/components/PrivateRoute.js | 12 ++++++++++++ server/server.js | 17 +++++++++++------ 5 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 client/src/components/PrivateRoute.js diff --git a/client/package-lock.json b/client/package-lock.json index fade6f1..1ea1d6c 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -13,6 +13,7 @@ "@testing-library/user-event": "^13.5.0", "axios": "^1.6.8", "bootstrap": "^5.3.3", + "js-cookie": "^3.0.5", "jspdf": "^2.5.2", "jspdf-autotable": "^3.8.4", "md5": "^2.3.0", @@ -12533,6 +12534,15 @@ "jiti": "bin/jiti.js" } }, + "node_modules/js-cookie": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz", + "integrity": "sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==", + "license": "MIT", + "engines": { + "node": ">=14" + } + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", diff --git a/client/package.json b/client/package.json index 44fd21f..486512c 100644 --- a/client/package.json +++ b/client/package.json @@ -8,6 +8,7 @@ "@testing-library/user-event": "^13.5.0", "axios": "^1.6.8", "bootstrap": "^5.3.3", + "js-cookie": "^3.0.5", "jspdf": "^2.5.2", "jspdf-autotable": "^3.8.4", "md5": "^2.3.0", diff --git a/client/src/App.js b/client/src/App.js index 4b4b0d3..fa4d222 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -1,9 +1,7 @@ import React from "react"; import { BrowserRouter as Router, Route, Routes } from "react-router-dom"; import CourseForm from "./Pages/CourseForm"; -import FacultyForm from "./Pages/FacultyForm"; import './App.css'; -import Welcome from "./Pages/Welcome" import AuthPage from "./Pages/Login"; import HomePage from "./Pages/HomePage"; import ForgetPwPage from "./Pages/ForgetPw"; @@ -12,26 +10,23 @@ import FilterPage from "./Pages/FilterPage"; import WelcomeWithFilter from "./Pages/WelcomeWithFilter"; import "react-toastify/dist/ReactToastify.css"; import CourseTable from "./Pages/CourseTable"; -import GenerateCSV from "./Pages/GenerateCSV"; import ConsolidatedTable from "./Pages/ConsolidatedTable"; import CourseConsolidated from "./Pages/courseConsolidated"; +import PrivateRoute from "./components/PrivateRoute"; function App() { return ( }> - } /> - } /> - } /> - } /> - }> + } />} /> + } />} /> }> }> }> - } /> - } /> - } /> - } /> + } />} /> + } />} /> + } />} /> + } />} /> ); } diff --git a/client/src/components/PrivateRoute.js b/client/src/components/PrivateRoute.js new file mode 100644 index 0000000..33ddffe --- /dev/null +++ b/client/src/components/PrivateRoute.js @@ -0,0 +1,12 @@ +import React from 'react'; +import { Navigate } from 'react-router-dom'; // Use Navigate for redirect +import Cookies from "js-cookie"; + +const PrivateRoute = ({ element: Element, ...rest }) => { + const token = Cookies.get("token"); + + // If token exists, render the element. Otherwise, redirect to the login page + return token ? Element : ; +}; + +export default PrivateRoute; diff --git a/server/server.js b/server/server.js index 2b1baa9..1d95dfc 100644 --- a/server/server.js +++ b/server/server.js @@ -6,6 +6,7 @@ const passport = require("passport"); const bodyParser = require("body-parser"); const path = require("path"); const bcrypt = require("bcryptjs"); +const jwt = require("jsonwebtoken"); require("dotenv").config(); // Import Routes @@ -52,12 +53,13 @@ require("./config/passport"); // Routes app.use("/password", authRoutes); -app.use("/api/courses", courseRoutes); -app.use("/api/faculty", facultyRoutes); -app.use("/api/appointments", appointmentRoutes); -app.use("/api/options", optionsRoutes); -app.use("/api/data", consolidatedRoutes); // Moved after `app` initialization -app.use("/api/send-email", emailRoutes); + +app.use("/api/courses", courseRoutes); +app.use("/api/faculty", facultyRoutes); +app.use("/api/appointments", appointmentRoutes); +app.use("/api/options", optionsRoutes); +app.use("/api/data", consolidatedRoutes); +app.use("/api/send-email", emailRoutes); // Google OAuth Routes app.get( @@ -69,6 +71,9 @@ app.get( "/auth/google/callback", passport.authenticate("google", { failureRedirect: "/" }), (req, res) => { + const token = jwt.sign({ userId: req.user._id }, process.env.JWT_SECRET, { expiresIn: "1h" }); + // Set token as a cookie or send it in the response + res.cookie("token", token, { httpOnly: false, secure: false }); res.redirect("http://localhost:3000/Welcom"); // Redirect to a frontend route after successful login } );