File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import SignInPage from './SignInPage';
77import HomePage from './HomePage' ;
88import NavBar from './NavBar' ;
99import { User , Session } from '../requests'
10+ import AuthRoute from './AuthRoute' ;
1011
1112class App extends Component {
1213 constructor ( props ) {
@@ -44,9 +45,24 @@ class App extends Component {
4445 < Route path = "/session/new" exact render = {
4546 ( routeProps ) => < SignInPage { ...routeProps } onSignIn = { this . getUser } /> }
4647 />
47- < Route path = "/products/new" component = { NewProductPage } />
48- < Route path = "/products/:id" component = { ProductShowPage } />
49- < Route path = "/products" exact component = { ProductIndexPage } />
48+ < AuthRoute
49+ isAuth = { currentUser }
50+ path = "/products/new"
51+ exact
52+ component = { NewProductPage }
53+ />
54+ < AuthRoute
55+ isAuth = { currentUser }
56+ path = "/products/:id"
57+ exact
58+ component = { ProductShowPage }
59+ />
60+ < AuthRoute
61+ isAuth = { currentUser }
62+ path = "/products"
63+ exact
64+ component = { ProductIndexPage }
65+ />
5066 < Route path = "/" exact component = { HomePage } />
5167 </ Switch >
5268 </ div >
Original file line number Diff line number Diff line change 1+ import React from 'react' ;
2+ import { Route , Redirect } from 'react-router-dom' ;
3+
4+ const AuthRoute = props => {
5+ const { isAuth, component, ...restProps } = props ;
6+ const Component = component ;
7+ return (
8+ < Route
9+ render = { routeProps => {
10+ if ( isAuth ) {
11+ // return React.createElement(component, routeProps);
12+ return < Component { ...routeProps } /> ;
13+ } else {
14+ return < Redirect to = '/session/new' /> ;
15+ }
16+ } }
17+ { ...restProps }
18+ />
19+ ) ;
20+ } ;
21+ export default AuthRoute ;
You can’t perform that action at this time.
0 commit comments