Untitled
unknown
plain_text
2 years ago
1.7 kB
11
Indexable
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
import Login from './Login.jsx'
import WeatherApp from './Weatherdata.jsx'
import Dashboard from './Dashboard.jsx'
import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
import ImageFetcher from './FetchImage.jsx'
const isAuthenticated = () => {
// Check if the user is authenticated, you can modify this based on your authentication logic
const username = localStorage.getItem('username');
const password = localStorage.getItem('password');
return username && password;
};
const isOnLoginPage = () => {
return window.location.pathname === '/';
};
const PrivateRoute = ({ element, path }) => {
if (!isAuthenticated() && !isOnLoginPage()) {
// If not authenticated and not on the login page, redirect to login
return <Navigate to="/" />;
}
return element;
};
const AppRouter = () => {
return (
<React.StrictMode>
<BrowserRouter>
<Routes>
<Route path="/" element={<Login />}></Route>
<Route
path="/Dashboard"
element={<PrivateRoute element={<Dashboard />} path="/Dashboard" />}
></Route>
<Route
path="/Weatherdata"
element={<PrivateRoute element={<WeatherApp />} path="/Weatherdata" />}
></Route>
<Route
path="/FetchImage"
element={<PrivateRoute element={<ImageFetcher />} path="/FetchImage" />}
></Route>
</Routes>
</BrowserRouter>
</React.StrictMode>
);
};
ReactDOM.createRoot(document.getElementById('root')).render(<AppRouter />);
Editor is loading...
Leave a Comment