From 54dd5a1fccf6210297ed0276b004f57c14be8301 Mon Sep 17 00:00:00 2001 From: Atharva Ombase <94031822+atharvaombase@users.noreply.github.com> Date: Sat, 19 Apr 2025 01:29:07 +0530 Subject: [PATCH] Feat:Added logic to redirect the user to login page if user is not logged in --- Frontend/src/pages/UserPages/Dashboard.jsx | 50 ++++++++++++++++------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/Frontend/src/pages/UserPages/Dashboard.jsx b/Frontend/src/pages/UserPages/Dashboard.jsx index 6845f59..7d8ff46 100644 --- a/Frontend/src/pages/UserPages/Dashboard.jsx +++ b/Frontend/src/pages/UserPages/Dashboard.jsx @@ -1,31 +1,52 @@ -import React, { useState, useEffect } from "react"; +import { useState, useEffect } from "react"; +import { useNavigate } from "react-router-dom"; import Sidebar from "../../components/Sidebar"; import FileList from "../../components/FileList"; -import FileUploadModal from "../../components/FileUploadModal"; // renamed the import accordingly - +import FileUploadModal from "../../components/FileUploadModal"; import { FiPlus } from "react-icons/fi"; const Dashboard = () => { const [files, setFiles] = useState([]); - // State to manage upload modal visibility const [isUploadModalOpen, setIsUploadModalOpen] = useState(false); + const [error, setError] = useState(""); + const navigate = useNavigate(); + const API_URL = import.meta.env.VITE_API_URL; + const isUserLoggedIn = () => { + const token = localStorage.getItem("token"); + const username = localStorage.getItem("username"); + const expiresIn = localStorage.getItem("expiresIn"); + + if (!token || !username || !expiresIn) return false; + + const expiryTime = new Date(expiresIn).getTime(); + const now = new Date().getTime(); + + if (now > expiryTime) { + localStorage.clear(); + return false; + } + + return true; + }; const fetchFiles = async () => { try { - const response = await fetch( - "http://192.168.29.61:8080/api/hdfs/listFiles?hdfsPath=/" - ); + const response = await fetch(`${API_URL}/api/hdfs/listFiles?hdfsPath=/`); const data = await response.json(); setFiles(data); } catch (error) { console.error("Failed to fetch files:", error); + setError("Failed to load files. Please try again later."); } }; useEffect(() => { - fetchFiles(); - // downloadFile("/sonali/cc.pptx", "kalas"); - }, []); + if (!isUserLoggedIn()) { + navigate("/login"); + } else { + fetchFiles(); + } + }, [navigate]); return ( <> @@ -34,7 +55,6 @@ const Dashboard = () => {

Dashboard

- {/* Use onClick to toggle the modal */}
- + {error ? ( +

{error}

+ ) : ( + + )}
- {/* Render the FileUploadModal with proper props */} setIsUploadModalOpen(false)} onUploadSuccess={() => { fetchFiles(); - // Optionally close the modal after upload success setIsUploadModalOpen(false); }} />