Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

React Private Route

Private Route--------

import { Navigate,useLocation} from "react-router-dom"
function Protecte({auth, children }) {   
    return auth ? children : <Navigate to="/login" />;
   
}
export default Protecte
-----------------------------------

App.js---
<Route path="/admin" element={
            <Protecte auth={isLoggedIn}>
              <Admin />
            </Protecte>
    }
   />
Comment

react router dom private route

//WORKS ON react-router-dom v6
//Using Outlet and Navigate
import { Routes, Route, Outlet, Navigate} from "react-router-dom";

function PrivateOutlet() {
  const auth = useAuth();
  return auth ? <Outlet /> : <Navigate to="/login" />;
}

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/private-outlet" element={<PrivateOutlet />}>
          <Route element={<Private />} />
        </Route>
      </Routes>
    </BrowserRouter>
  );
}
Comment

private route in react js

const PrivateRoute = ({component: Component, ...rest}) => {
  const {isAuthenticated} = rest;

  return (
    <Route {...rest} render={props => (
      isAuthenticated ? (
        <Component {...props}/>
      ) : (
        <Redirect to={{
          pathname: '/login',
          state: {from: props.location}
        }}/>
      )
    )}
    />
  );
};

PrivateRoute.propTypes = {
  isAuthenticated: PropTypes.bool.isRequired,
};

function mapStateToProps(state) {
  return {
    isAuthenticated: state.user.isAuthenticated,
  };
}

export default connect(mapStateToProps)(PrivateRoute);
Comment

private routing in react

import React, { useEffect, useState } from "react";
import "./App.css";
import { Routes, Route, Navigate } from "react-router-dom";
// import Home from "./components/Home";
 import Dashboard from "./components/Dashboard";
// import SignIn from "./components/SignIn";
// import PrivateRoute from "./components/PrivateRoute";
// import PublicRoute from "./components/PublicRoute";
import Auth from "./Pages/auth";
import Profile from "./Pages/profile";

const App3 = () => {

  const[auth,setAuth]=useState(null);

  useEffect(() => {
    let user = localStorage.getItem("user");
    user && JSON.parse(user) ? setAuth(true) : setAuth(false);
  }, []);

  useEffect(() => {
    localStorage.setItem("user", auth);
  }, [auth]);


  return (
  
      <Routes>
      {!auth && (
        <Route path="/auth" 
        element={<Auth authenticate={()=>setAuth(true)}/>}
        />   
      )}

        {auth && <>
          <Route path="/profile"
          element={<Profile logout={()=>setAuth(false)}/>} />
          <Route path="/dashboard" element={<Dashboard/>} />
        </>
        
        }
        <Route path="/dashboard" element={<Dashboard />}/>

          <Route path="*" element={<Navigate to={auth ? "/profile" : "/auth"} />} />

        {/* <PublicRoute restricted={false} element={<Home/>} path="/" exact /> 
        <PublicRoute restricted={true} element={<SignIn/>} path="/signin" exact />
        <PrivateRoute element={<Dashboard />} path="/dashboard" exact /> */}
      </Routes>

  );
};

export default App3;
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular input date pattern validation 
Javascript :: how to export default class in javascript 
Javascript :: jquery select element with class 
Javascript :: 1 line password strength checker jquery 
Javascript :: svelte wait 
Javascript :: sequelize findall 
Javascript :: javascript function add two numbers 
Javascript :: javascript prevent value change in select option 
Javascript :: return statement javascript 
Javascript :: factors of a number 
Javascript :: how to get data from for loop in react native 
Javascript :: @input in angular 
Javascript :: node http2 post 
Javascript :: sort object with certain value at start of array js 
Javascript :: get numbers from a string 
Javascript :: javascript Create a RegEx 
Javascript :: find second smallest number in array 
Javascript :: js alertify.success parameters 
Javascript :: check if alpine js is activated in website 
Javascript :: how to add all files in a director to an array in javascript 
Javascript :: react carousel 
Javascript :: mongooseautoincrement 
Javascript :: rivets bind 
Javascript :: react scripts for browser 
Javascript :: js variables 
Javascript :: nodejs check if file is running on server or client 
Javascript :: find the length of checked in js 
Javascript :: get text selection javascript 
Javascript :: Recorrer Array con forEach 
Javascript :: how to check alphabet case in javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =