private route in react js

Solutions on MaxInterview for private route in react js by the best coders in the world

showing results for - "private route in react js"
Salomé
08 Jul 2020
1Private Route--------
2
3import { Navigate,useLocation} from "react-router-dom"
4function Protecte({auth, children }) {   
5    return auth ? children : <Navigate to="/login" />;
6   
7}
8export default Protecte
9-----------------------------------
10
11App.js---
12<Route path="/admin" element={
13            <Protecte auth={isLoggedIn}>
14              <Admin />
15            </Protecte>
16    }
17   />
Stéphane
30 Jan 2019
1import React from 'react';
2import {
3  BrowserRouter,
4  Route,
5  Switch,
6  Redirect
7} from 'react-router-dom';
8import decode from 'jwt-decode';
9
10import Home from './Home';
11import Register from './Register';
12import Login from './Login';
13import PrivateComponent1 from './PrivateComponent1';
14import PrivateComponent2 from './PrivateComponent2';
15
16
17const isAuthenticated = () => { 
18  	  const token = localStorage.getItem('token');
19  try {
20    if(token){
21      return true;
22    }
23    else{
24      return false;
25    }
26  } catch (error) {
27    return false;
28  }
29}
30
31function PrivateRoute({ component: Component, ...rest }) {
32  return (
33    <Route
34      {...rest}
35      render={props =>
36        isAuthenticated() ? (
37          <Component {...props} />
38        ) : (
39          <Redirect
40            to={{
41              pathname: "/login",
42            }}
43          />
44        )
45      }
46    />
47  );
48}
49
50export default () => (
51  <BrowserRouter>
52    <Switch>
53      <Route path="/" exact component={Home}/>
54      <Route path="/register" exact component={Register}/>
55      <Route path="/login" exact component={Login}/>
56      <PrivateRoute path="/private1/:id?" exact component={PrivateComponent1}/>
57      <PrivateRoute path="/private2" exact component={PrivateComponent2}/>
58    </Switch>
59  </BrowserRouter>
60);
Manuel
02 Sep 2016
1import React from 'react'
2import {Route, Redirect} from 'react-router-dom'
3
4const PrivateRoute = ({
5    component: Component,
6  	isAuthenticated,
7  	userLevel,
8    ...rest
9}) => (
10    <Route {...rest} component={(props)=> 
11            isAuthenticated ? (
12                <Component {...props} />
13            ):(
14                <Redirect to="/login" />
15            )
16    } />
17)
18export default PrivateRoute
Ariana
16 Apr 2018
1const PrivateRoute = ({component: Component, ...rest}) => {
2  const {isAuthenticated} = rest;
3
4  return (
5    <Route {...rest} render={props => (
6      isAuthenticated ? (
7        <Component {...props}/>
8      ) : (
9        <Redirect to={{
10          pathname: '/login',
11          state: {from: props.location}
12        }}/>
13      )
14    )}
15    />
16  );
17};
18
19PrivateRoute.propTypes = {
20  isAuthenticated: PropTypes.bool.isRequired,
21};
22
23function mapStateToProps(state) {
24  return {
25    isAuthenticated: state.user.isAuthenticated,
26  };
27}
28
29export default connect(mapStateToProps)(PrivateRoute);
30
queries leading to this page
private routing with authentication react jsreaact private routeprivate route react routehow to do a class on a privateroute with react router domwith private route jsprivate router in react jsprivateroute implementation reactprivate route react router domprivate route inside private route reactprivate router using react router domrender component in private route reactprivateroute react router domreact js publc and private routesreact useprivaterouterwriting private routes reactract private routesreact handle private routespublic routes and private routes in reacthow to protect private routes in reactreact router private routeprivate route react propseasy way to make routes private reactreact router route private route managementreact create private routeprivate router react with switchhow to make a private route in react router domv6private route component reactreact public private routesreact router dom private routereact private route componenthow to set up private route reactprivate routing 2breact jsrouter dom private route reactjsreact private routereact routing privateprivate route react router 5private route in react 6private routes react router domreactjs private routehow to set up a private routeprivate route on reactprotected routes in react jshow to write private route in react router 6how to do private route in reactreact private routes and public routesset private route reactcreate private route react js medium in class componentprotected routing in react jsreact private routerreact private route npmreact router dom privaterouteprivate route react router domprivate routes reactjsjs react privatereact js private routeprivate route react router comprivate route for registration in react jspublic and private route reactprivate router react documentationprivate route in react router 6private routes react propsusing react router dom private routereact router dom is privatebest way to handle private routes in a reactjs appprivate route for reactsetting a very basci private routeprivate route implementation reactprivate route react private route redirect to reactcreating a private route component in reactreact router dom private route docspublic and private routes in reacthow to make react router with private routesreact public vs private routsreact public private routewhat is the purpose of private routes in reactprivate router react routerprivate routes with react router domhow to make private routes reactreact router privateprivate route in react jsprivate routing react jsprivate routes react javascripthow to set up private route in reactprivate route react jsreact router private routesprivate route react exampleprivate routes in react router domreact auth private routeprivate routes in react in fucntional reacthow to create a private route in react router 6how to make public and private routes in reactprivate router in reactcreating private route with react routerpublic route and private route in reactpublic and private route react architecturepublic route in react jshow to create private routes in reactprivate routerreact router private router private routes react routerprivate route mideumhow to make a private route rreactcreate private routes in react jsreact router dom link privatecreate a private route in reactjsprotected routes in react js5how to define private route in react class componenthow to make a private route in reactjsprivateroute react router dom exampleprivate route react router and propsreact private routingreact router dom private routeprivate routes react router 5cprivate route routerprivateroute reactprivate route in react router domreact router private routewhat is private route in reacthow to make private route in reactprivate route using reactuse private route in reactquando usar private route reactprivateroute in react routerprivate route reactjsprivate routereact private route few componentprivate router react jshow to add privateroutereact private route propsprivate route react router dom vprivate route react node loginprivate route for 2f in reacthow to create private and public route in reactprivate route declaration react router domprivateroute in reactjshow to make private to route in react jsprivate route props reactreact privaterouteprivate routing in reactjscreating a protected route in react router dom v6add a private route to react appprivate component reacthow to add a private route to react appwhat is react withprivatecreating private routes react routerprivate routes in react jshow to redireect to private route inreact rotuerprivate router with react router dom creating private routes with react router domprivate and public routes reactprivate route react js nedirprivate route vs routeprivet rout react router react handle with public and private routesprivate and protected route in react jshow to make private routes in reactprivate route reacct js how to create private routes in react nativereact private routing examplework with react router and private routehow to create protected and private route in reactcreate a private route with react router domreactjs private routingexample of private route in react jshow to define public route and private route in reactprivate routing in reactjs examplereactjs private route examplereact withprivatereact router privateroute reactjsprivateroute reactprivate route connected react routerreact private routeshow to create a private route reacthow to create private and public routes in reactprivate route in reactmake a private route in reactprivate route reactprivate routing in react jsprivateroute login ractprivate router in react router domreactjs private route componentcreating private routes in reacthow to use react privater routereact create private routessetup private route and public route reactprivate and protected routes in reactprivate route react jshow to create a set of private route in reactprivate route and open route in reacttwo private routesprivate router react router c3 b9reactjs what is private routereact private routes npmreactjs private routerhow does private route work in react routerreact public and private routesprivateroute go to route ifprotected route in react router 6reactjs react router dom private routereact router dom private routingprivate routes reactreact private route new react routerprotected routing in react router dompublic and private route in react jshow to create a private route in reactwhat is private routing in reactjsprivate route in reactjs adding private router reactprivate react routerwhat is the differnce between protected and private routehow to create private route in reactprivate router reacthow to make a privateroute component in reactcreate private route react js mediumhow to handle private route in reactreact router privaterouteprivate route in react with render component react router dom private routeprotected routes reacthow to create private routes in functional component reactprivate route react router exampleprivate router in react routerprotected route in reactprivate route react javascriptreact router dom private routesreact private route tutorialhow to show a private route in react jsprivateroute in reactreact rpuiter private routeuse private route in react jshow to access a private route from reacthow to create privet routes in reacthow to set private routes in reactmaking routes public or privatecan you use react many privat routesreact private route examplereact dom privatecreate react private routereact router private route examplereact how to make private routehow to implement private routes or components in reactprivateroute reactjsmore than 2 private route in reactjshow to add private routes in reactprivate route react routerprivate routes in reactprivate router react router domprivate routing in reactreact class with private and public to react hookshow to set a private route in reactreact router 6 creating private routereact router dom privateprotected routing in react router dom v6private route with user reactreact router create a private routeprivate route in react js simple examplehow to private routes in reactnavlink to private route with react routerhow to use private routes in reactcreate private route reacthow to create public and private routes using react routerbuilding private routes with react routerimplement private routes in reactjsreact js how to do a private routereact router 6 with private routesprivate route with react router domhow to redirect to provate route in react router domhow to write custom private routereact routert private route pivateroute reactprivateroute routing reactprivate route in react routerredirect to private routeprivate rout with react rotor domhow to add private routes in react router domreact private routes for dummiesreact router dom private route with propsprivate route react routerprivate route in react js