Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.5 kB
1
Indexable
Never
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 PrivateRoute = ({ element, path }) => {
  return isAuthenticated() ? element : <Navigate to="/" />;
};

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 />);
Leave a Comment