PrivateRoute component

The client/auth/PrivateRoute.js defines the PrivateRoute component as shown in an auth flow example from https://reacttraining.com/react-router/web/example/auth-workflow in the React Router documentation. It will allow us to declare protected routes for the frontend to restrict view access based on user auth.

mern-skeleton/client/auth/PrivateRoute.js:

import React, { Component } from 'react'import { Route, Redirect } from 'react-router-dom'import auth from './auth-helper'const PrivateRoute = ({ component: Component, ...rest }) => (  <Route {...rest} render={props => (    auth.isAuthenticated() ? (      <Component {...props}/>    ) : (      <Redirect to={{        pathname: '/signin',        state: { from: props.location }      }}/>    )  )}/>)export default ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.