Skip to content

Commit b6a2cf1

Browse files
committed
Wait for auth data to load and redirect logged-in users to homepage
1 parent c2fc089 commit b6a2cf1

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/App.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ import Homepage from './Homepage';
55
import PageRegister from './PageRegister';
66

77
import { Switch, Route } from 'react-router-dom';
8+
import { connect } from 'react-redux';
9+
import { isLoaded } from 'react-redux-firebase';
10+
11+
const App = props => {
12+
if (!isLoaded(props.auth, props.profile)) {
13+
return <div>Authentication loading...</div>;
14+
}
815

9-
const App = () => {
1016
return (
1117
<Switch>
1218
<Route exact path="/">
@@ -28,4 +34,8 @@ const App = () => {
2834
);
2935
};
3036

31-
export default App;
37+
const mapStateToProps = state => {
38+
return { auth: state.firebase.auth, profile: state.firebase.profile };
39+
};
40+
41+
export default connect(mapStateToProps)(App);

src/PageRegister.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import React from 'react';
22
import { firebaseConnect } from 'react-redux-firebase';
3+
import { connect } from 'react-redux';
4+
import { compose } from 'redux';
5+
import { Redirect } from 'react-router-dom';
36

47
class PageRegister extends React.Component {
58
constructor(props) {
@@ -27,6 +30,10 @@ class PageRegister extends React.Component {
2730
};
2831

2932
render() {
33+
if (this.props.isLoggedIn) {
34+
return <Redirect to="/" />;
35+
}
36+
3037
return (
3138
<div>
3239
<h2>Register</h2>
@@ -54,4 +61,11 @@ class PageRegister extends React.Component {
5461
}
5562
}
5663

57-
export default firebaseConnect()(PageRegister);
64+
const mapStateToProps = state => {
65+
return { isLoggedIn: state.firebase.auth.uid };
66+
};
67+
68+
export default compose(
69+
firebaseConnect(),
70+
connect(mapStateToProps),
71+
)(PageRegister);

0 commit comments

Comments
 (0)