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

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 :: canvas js in react 
Javascript :: componentDidmount event on fonctional component 
Javascript :: how to download array of files from aws s3 using aws sdk in nodejs 
Javascript :: Prerequisites before creating react-app 
Javascript :: plus sign javascript 
Javascript :: react admin data provider 
Javascript :: Create array literal 
Javascript :: search in array javascript 
Javascript :: how can i use exact in react router dom v6 
Javascript :: javascript get all options from select 
Javascript :: vue js 
Javascript :: react live chat widget 
Javascript :: express router 
Javascript :: how to add alert on javascript 
Javascript :: AJAX in reload a div container 
Javascript :: template engine javascript 
Javascript :: What is constructor.constructor()() in JavaScript 
Javascript :: js oop 
Javascript :: axios async await 
Javascript :: how to change background color in css and or js react 
Javascript :: node js + mongoose 
Javascript :: setjavascriptenabled why it is used 
Javascript :: react native store sensitive data in redux 
Javascript :: rect js 
Javascript :: js let vs var performance 
Javascript :: when uncheck remove value from div javascript 
Javascript :: javascript popup canvas 
Javascript :: cheatsheet addeventlistener 
Javascript :: tinymce image and links inputs disabled 
Javascript :: chandrachaan 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =