private routes in react

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

showing results for - "private routes in react"
Eloi
26 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);
Hanna
04 Jul 2017
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
Alessia
02 Jan 2019
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 routes react javascriptreact private route few componentprivate react routerprivate routerbuilding private routes with react routerreactjs private routerreact public and private routeshow to implement private routes or components in reactreact create private routesreact router privateroute reactjscan you use react many privat routesreactjs what is private routeprivate route implementation reactprivateroute in reactjsreact how to make private routeprivateroute reactprivate routeprivate router in reacthow to create a private route in reactreact private routes npmpublic routes and private routes in reactprivate route and open route in reactpublic and private routes in reactreact router dom private routesprivate route component reactreact router route private route managementprivate router react with switchprivate route react routerprivate route react jsprivate routing in reactjs exampleprivate route react js nedirhow to private routes in reacthow to create private route in reactprivate routes with react router domreact routing privateadding private router reactreactjs private route componenthow does private route work in react routerreact private routertwo private routesreact useprivaterouterjs react privatepublic route and private route in reacthow to make a private route in reactjshow to create private and public route in reactnavlink to private route with react routerprivateroute login ractprivate routing in react jsprivateroute go to route ifhow to create private routes in reactreact private routes for dummiesmore than 2 private route in reactjsusing react router dom private routeprivate routes reactjshow to make private to route in react jshow to set private routes in reactreact private route npmprivate route react router 5react router private routesreact router private routereact auth private routereact router dom private routeprivate router in react routerreact handle private routesprivate router reactprivateroute implementation reactprivate route props reacthow to add privateroutereact router dom privaterouteprivate routes react routerprivate route reactjsreact router private routeprivate routes in react jsprivate routing in reactreact router dom private routingprivateroute routing reactprivate route react router and propsprivate route on reactreact router dom is privatehow to create privet routes in reactprivate routing with authentication react jsuse private route in react jsreact router dom link privatehow to make public and private routes in reactprivate route vs routeprivate route react routerprivateroute in react routerwork with react router and private routesetting a very basci private routeprivateroute reacthow to set a private route in reactmaking routes public or privatehow to make a privateroute component in reacthow to handle private route in reactreactjs private routingprivate route in react routermake a private route in reacthow to make private route in reactract private routesprivate route reactprivate route connected react routerhow to make private routes in reactcreate private routes in react jsprivate route reacct js react private routes and public routesprivate route react javascriptprivate route using reacthow to redirect to provate route in react router domhow to add a private route to react appprivate router with react router dom how to set up private route reactprivate and protected route in react jsprivate route mideumhow to create private routes in functional component reactreactjs react router dom private routehow to define private route in react class componentwhat is private route in reactpublic and private route in react jsprivate route react jsprivate routing 2breact jsprivate route react router exampleprivate route react propsreact private routeprivate route in reactjs private route for 2f in reactwriting private routes reactprivate routes in reactprivate rout with react rotor domprivate routing in reactjsreact router private route examplequando usar private route reactreact class with private and public to react hooksprivateroute in reactprivate routes react router 5cpublic and private route reactprivate route react react public vs private routsreact public private routeswhat is the purpose of private routes in reactreact private routing examplecreate private route react js medium in class componentreaact private routehow to create a private route reactprivate routing react jshow to protect private routes in reactprivet rout react router redirect to private routereact private routesprivate route react router domprivate route in react router domcreate a private route in reactjshow to add private routes in react router domprivate route in react with render componentcreate private route react js mediumprivate route routerprivate route react exampleprivate route in react js simple examplepublic route in react jshow to create public and private routes using react routerreact withprivatehow to set up a private routereact router dom private routereact create private routeprivate routes in react router domprivateroute react router domhow to access a private route from reacthow to use react privater routereact router dom private route with propshow to show a private route in react jsreact js publc and private routesreact public private routereact handle with public and private routesreact dom privateprivate routes reactreact js how to do a private routecreating private route with react routerhow to define public route and private route in reactprivate route react router comwhat is the differnce between protected and private routeprivate route for reactreact router 6 with private routesreact router privaterouteprivate component reactprivateroute react router dom examplereact private route examplecreating private routes react routerreact js private routereact router dom private route docssetup private route and public route reacthow to make a private route rreactprivate and public routes reactpublic and private route react architectureprivate routes in react in fucntional reactbest way to handle private routes in a reactjs appprivate route for registration in react jsreact rpuiter private routereactjs private route example react router dom private routecreate react private routeprivate route in reactrender component in private route reactcreating private routes with react router domprivate route react routeuse private route in reactrouter dom private route reactjshow to do a class on a privateroute with react router domprivate route inside private route reactprivate routes react router domwhat is react withprivateprivate route in react jsprivate router react router domhow to make react router with private routesreactjs private routeprivate router using react router domprivate route declaration react router domhow to write custom private routeprivate router react jscreate private route reacthow to set up private route in reacthow to do private route in react pivateroute reactreact private route propsprivate router in react jsreact privateroutewith private route jshow to redireect to private route inreact rotuerreact private route tutorialprivate route react node loginprivate router react routerreact router dom privateprivate route redirect to reactprivateroute reactjsadd a private route to react apphow to use private routes in reactreact routert private routeprivate route react router domreact router create a private routeprivate routes react propscreating a private route component in reacthow to create private and public routes in reacthow to add private routes in reactreact private route componentwhat is private routing in reactjsprivate route with react router domhow to create private routes in react nativereact router private router implement private routes in reactjsprivate routes in react