Skip to content

Commit b329f6e

Browse files
committed
09-Session Handling with Higher-Order Components
1 parent 8fa5588 commit b329f6e

File tree

5 files changed

+86
-70
lines changed

5 files changed

+86
-70
lines changed

src/components/App/index.js

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import { BrowserRouter as Router, Route } from 'react-router-dom';
33

44
import Navigation from '../Navigation';
@@ -11,62 +11,28 @@ import AccountPage from '../Account';
1111
import AdminPage from '../Admin';
1212

1313
import * as ROUTES from '../../constants/routes';
14-
import { withFirebase } from '../Firebase';
15-
16-
class App extends Component {
17-
constructor(props) {
18-
super(props);
19-
20-
this.state = {
21-
authUser: null,
22-
};
23-
}
24-
25-
componentDidMount() {
26-
this.listener = this.props.firebase.auth.onAuthStateChanged(
27-
authUser => {
28-
authUser
29-
? this.setState({ authUser })
30-
: this.setState({ authUser: null });
31-
},
32-
);
33-
}
34-
35-
componentWillUnmount() {
36-
this.listener();
37-
}
38-
39-
render() {
40-
return (
41-
<Router>
42-
<div>
43-
<Navigation authUser={this.state.authUser} />
44-
45-
<hr />
46-
47-
<Route
48-
exact
49-
path={ROUTES.LANDING}
50-
component={LandingPage}
51-
/>
52-
<Route exact path={ROUTES.SIGN_UP} component={SignUpPage} />
53-
<Route exact path={ROUTES.SIGN_IN} component={SignInPage} />
54-
<Route
55-
exact
56-
path={ROUTES.PASSWORD_FORGET}
57-
component={PasswordForgetPage}
58-
/>
59-
<Route exact path={ROUTES.HOME} component={HomePage} />
60-
<Route
61-
exact
62-
path={ROUTES.ACCOUNT}
63-
component={AccountPage}
64-
/>
65-
<Route exact path={ROUTES.ADMIN} component={AdminPage} />
66-
</div>
67-
</Router>
68-
);
69-
}
70-
}
71-
72-
export default withFirebase(App);
14+
import { withAuthentication } from '../Session';
15+
16+
const App = () => (
17+
<Router>
18+
<div>
19+
<Navigation />
20+
21+
<hr />
22+
23+
<Route exact path={ROUTES.LANDING} component={LandingPage} />
24+
<Route exact path={ROUTES.SIGN_UP} component={SignUpPage} />
25+
<Route exact path={ROUTES.SIGN_IN} component={SignInPage} />
26+
<Route
27+
exact
28+
path={ROUTES.PASSWORD_FORGET}
29+
component={PasswordForgetPage}
30+
/>
31+
<Route exact path={ROUTES.HOME} component={HomePage} />
32+
<Route exact path={ROUTES.ACCOUNT} component={AccountPage} />
33+
<Route exact path={ROUTES.ADMIN} component={AdminPage} />
34+
</div>
35+
</Router>
36+
);
37+
38+
export default withAuthentication(App);

src/components/Navigation/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ import { Link } from 'react-router-dom';
44
import SignOutButton from '../SignOut';
55
import * as ROUTES from '../../constants/routes';
66

7-
const Navigation = ({ authUser }) => (
8-
<div>{authUser ? <NavigationAuth /> : <NavigationNonAuth />}</div>
7+
import { AuthUserContext } from '../Session';
8+
9+
const Navigation = () => (
10+
<div>
11+
<AuthUserContext.Consumer>
12+
{authUser =>
13+
authUser ? <NavigationAuth /> : <NavigationNonAuth />
14+
}
15+
</AuthUserContext.Consumer>
16+
</div>
917
);
1018

1119
const NavigationAuth = () => (

src/components/Session/context.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
3+
const AuthUserContext = React.createContext(null);
4+
5+
export default AuthUserContext;

src/components/Session/index.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import React from 'react';
1+
import AuthUserContext from './context';
2+
import withAuthentication from './withAuthentication';
23

3-
const Session = () => (
4-
<div>
5-
<h1>Session</h1>
6-
</div>
7-
);
8-
9-
export default Session;
4+
export { AuthUserContext, withAuthentication };
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from 'react';
2+
3+
import AuthUserContext from './context';
4+
import { withFirebase } from '../Firebase';
5+
6+
const withAuthentication = Component => {
7+
class WithAuthentication extends React.Component {
8+
constructor(props) {
9+
super(props);
10+
11+
this.state = {
12+
authUser: null,
13+
};
14+
}
15+
16+
componentDidMount() {
17+
this.listener = this.props.firebase.auth.onAuthStateChanged(
18+
authUser => {
19+
authUser
20+
? this.setState({ authUser })
21+
: this.setState({ authUser: null });
22+
},
23+
);
24+
}
25+
26+
componentWillUnmount() {
27+
this.listener();
28+
}
29+
30+
render() {
31+
return (
32+
<AuthUserContext.Provider value={this.state.authUser}>
33+
<Component {...this.props} />
34+
</AuthUserContext.Provider>
35+
);
36+
}
37+
}
38+
39+
return withFirebase(WithAuthentication);
40+
};
41+
42+
export default withAuthentication;

0 commit comments

Comments
 (0)