private routes in react

import React from 'react';
import {
  BrowserRouter,
  Route,
  Switch,
  Redirect
} from 'react-router-dom';
import decode from 'jwt-decode';

import Home from './Home';
import Register from './Register';
import Login from './Login';
import PrivateComponent1 from './PrivateComponent1';
import PrivateComponent2 from './PrivateComponent2';


const isAuthenticated = () => { 
  	  const token = localStorage.getItem('token');
  try {
    if(token){
      return true;
    }
    else{
      return false;
    }
  } catch (error) {
    return false;
  }
}

function PrivateRoute({ component: Component, ...rest }) {
  return (
    <Route
      {...rest}
      render={props =>
        isAuthenticated() ? (
          <Component {...props} />
        ) : (
          <Redirect
            to={{
              pathname: "/login",
            }}
          />
        )
      }
    />
  );
}

export default () => (
  <BrowserRouter>
    <Switch>
      <Route path="/" exact component={Home}/>
      <Route path="/register" exact component={Register}/>
      <Route path="/login" exact component={Login}/>
      <PrivateRoute path="/private1/:id?" exact component={PrivateComponent1}/>
      <PrivateRoute path="/private2" exact component={PrivateComponent2}/>
    </Switch>
  </BrowserRouter>
);

3.6
5

                                    import React from 'react'
import {Route, Redirect} from 'react-router-dom'

const PrivateRoute = ({
    component: Component,
  	isAuthenticated,
  	userLevel,
    ...rest
}) =&gt; (
    &lt;Route {...rest} component={(props)=&gt; 
            isAuthenticated ? (
                &lt;Component {...props} /&gt;
            ):(
                &lt;Redirect to=&quot;/login&quot; /&gt;
            )
    } /&gt;
)
export default PrivateRoute

3.6 (5 Votes)
0
3.67
3

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

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

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

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

export default connect(mapStateToProps)(PrivateRoute);

3.67 (3 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
react routing private private routes react-router\ reactjs private routing private router using react router dom implement private routes in reactjs setup private route and public route react private route component react private route in reactjs private route in react router dom reactjs private route example react router private router create a private route in reactjs private route props react private routes reactjs make a private route in react reactjs private route component react js how to do a private route how to do private route in react use private route in react js router dom private route reactJS how to make react router with private routes react how to make private route creating a private route component in react react router route private route management how to use react privater route what is the purpose of private routes in react private routes react router dom private route reacct js best way to handle private routes in a reactjs app private route react props private routes react props private route in react with render component How to create a private route in React private route react router and props private route react javascript private routes react javascript how to set private routes in react private route and open route in react private and protected route in react js react private route props private route using react private route react route adding private router react reactjs what is private route reactjs private route private route for react creating private routes with react router dom how to make private routes in react how to define public route and private route in react private route react-router-dom react router dom private route docs react-routert private route how to add private routes in react router dom how to add private routes in react private route declaration react router dom use private route in react private route react router.com how to protect private routes in react how to make private to route in react js react private routes npm react private route npm public and private routes in react how to define private route in react class component private routes in react router dom private route inside private route react private routing in reactjs example reactjs private router React-router private Route how to access a private route from react how to set a private route in react how to show a private route in react.js react router 6 with private routes how to make a private route rreact private route in react router private router in react router private router in react private route connected react router react router dom private route using react router dom private route private routing react.js react create private routes react create private route how does private route work in react router private router in react js private route in react js simple example private routing in react js making routes public or private react handle with public and private routes react private router react-router-dom private redirect to private route what is the differnce between protected and private route Private route. react create private route react js medium in class component create private route react js medium How to do a class on a PrivateRoute with React Router Dom react router create a private route two private routes privateroute routing react private route with react-router-dom private router with react router dom react auth private route public route in react js private route mideum private route react.js how to create privet routes in react how to write custom private route private rout with react rotor dom more than 2 private route in reactjs private Routes in react in fucntional react PrivateRoute in react private component react what is react withPrivate react withPrivate react router privateroute reactjs private route react router example react private routes for dummies private routing in react private route in react js how to add PrivateRoute setting a very basci private route with private route.js react router dom link private how to create a private route react private route router how to set up a private route PrivateRoute in reactjs creating private routes react router private route vs route react private routes privateroute in react router quando usar private route react private route react router 5 how to make a private route in reactjs private react router private route on react privateroute - react privateRoute react router dom how to redireect to private route inreact rotuer navlink to private route with react router building private routes with react router react public and private routes react router PrivateRoute work with react router and private route react router dom private routes private routes in react js react js publc and private routes PrivateRoute login ract PrivateRoute go to route if private routes with react-router-dom creating private route with react router react-router-dom private route reactjs react-router-dom private route can you use react many privat routes react private route few component react private route component what is private route in react private route react node login react dom private private router react router how to create private routes in React native how to create private routes in React react-router dom private routing private router react PrivateRoute private routing in reactjs private route reactjs react router dom PrivateRoute react handle private routes how to set up private route in react how to make a privateroute component in react react public vs private routs react public private route create react private route privateroute reactjs private router react js react class with private and public to react hooks create private route react privateroute react router dom example how to implement private routes or components in react how to handle private route in react react router dom is private react router private route react rpuiter private route react usePrivateRouter how to make public and private routes in react react public private routes writing private routes react privateroute implementation react privet rout react router pivateroute react private route implementation react public and private route react architecture how to redirect to provate route in react router dom private route redirect to react js react private how to make private route in react react private routes and public routes public and private route react react private route how to create private and public route in react react private route example privateroute react private router react with switch react js private route private routes react private and public routes react private routes react router how to create private route in react private route react router private route react react router private routes private route react router dom how to use private routes in react how to set up private route react public route and private route in react react router dom private route private router react private route private route in react private routes in react
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source