-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (64 loc) · 1.7 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App";
import Login from "./components/Auth/Login";
import Register from "./components/Auth/Register";
import Spinner from "./Spinner";
import registerServiceWorker from "./registerServiceWorker";
import firebase from "./firebase";
import "semantic-ui-css/semantic.min.css";
import {
BrowserRouter as Router,
Switch,
Route,
withRouter
} from "react-router-dom";
import { createStore } from "redux";
import { Provider, connect } from "react-redux";
import { composeWithDevTools } from "redux-devtools-extension";
import rootReducer from "./reducers";
import { setUser, clearUser } from "./actions";
const store = createStore(rootReducer, composeWithDevTools());
class Root extends React.Component {
componentDidMount() {
firebase.auth().onAuthStateChanged(user => {
if (user) {
// console.log(user);
this.props.setUser(user);
this.props.history.push("/");
} else {
this.props.history.push("/login");
this.props.clearUser();
}
});
}
render() {
return this.props.isLoading ? (
<Spinner />
) : (
<Switch>
<Route exact path="/" component={App} />
<Route path="/login" component={Login} />
<Route path="/register" component={Register} />
</Switch>
);
}
}
const mapStateFromProps = state => ({
isLoading: state.user.isLoading
});
const RootWithAuth = withRouter(
connect(
mapStateFromProps,
{ setUser, clearUser }
)(Root)
);
ReactDOM.render(
<Provider store={store}>
<Router>
<RootWithAuth />
</Router>
</Provider>,
document.getElementById("root")
);
registerServiceWorker();