-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (54 loc) · 1.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter as Router, Route, Redirect, Switch} from 'react-router-dom'
import './index.css';
import App from './App';
import LoginPage from './Login';
import Product from './Product';
import registerServiceWorker from './registerServiceWorker';
import Category from "./components/category";
class Index extends React.Component {
constructor(props) {
super(props)
this.handleAuth = this.handleAuth.bind(this);
this.handleLogout = this.handleLogout.bind(this);
this.state = {auth: false, authInfo: {}}
}
handleAuth(authInfo) {
this.setState({auth: true, authInfo});
}
handleLogout() {
this.setState({auth: false});
}
render() {
const auth = this.state.auth;
const authInfo = this.state.authInfo;
const PrivateRoute = ({component: Component, authed, authInfo, ...rest}) => {
return (
<Route
{...rest}
render={(props) => authed === true
? <Component {...props} authInfo={authInfo}/>
: <Redirect to={{pathname: '/login', state: {from: props.location}}}/>}>
{this.props.children}
</Route>
)
}
return (
<Router>
<div>
<Route exact path="/" component={App}/>
<Route path="/login" component={route => {
return <LoginPage {...route} onHandleAuth={this.handleAuth}/>
}}/>
<PrivateRoute authed={auth} path='/product' component={Product} authInfo={authInfo}>
</PrivateRoute>
</div>
</Router>
)
}
}
ReactDOM.render((
<Index/>
), document.getElementById('root'));
registerServiceWorker();