Skip to content

Commit 585863b

Browse files
maxwellgordonansonws
authored andcommitted
Authenticated Route
1 parent cee5dfb commit 585863b

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

src/components/App.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import SignInPage from './SignInPage';
77
import HomePage from './HomePage';
88
import NavBar from './NavBar';
99
import { User, Session } from '../requests'
10+
import AuthRoute from './AuthRoute';
1011

1112
class 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>

src/components/AuthRoute.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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;

0 commit comments

Comments
 (0)