Skip to content

Commit 003d538

Browse files
authored
Merge pull request #3 from rwieruch/new-context
React 16.3: Using new Context API
2 parents 4d9c8fa + 3c5b2ea commit 003d538

File tree

11 files changed

+5013
-4633
lines changed

11 files changed

+5013
-4633
lines changed

gatsby-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ module.exports = {
22
siteMetadata: {
33
title: `Gatsby Firebase Authentication`,
44
},
5-
plugins: [`gatsby-plugin-react-helmet`],
5+
plugins: [`gatsby-plugin-react-helmet`, `gatsby-plugin-react-next`],
66
}

package-lock.json

Lines changed: 4964 additions & 4574 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"version": "1.0.0",
55
"author": "Robin Wieruch <wrobin@gmx.net>",
66
"dependencies": {
7-
"firebase": "^4.8.0",
8-
"gatsby": "^1.9.127",
9-
"gatsby-link": "^1.6.30",
10-
"gatsby-plugin-react-helmet": "^1.0.8",
11-
"prop-types": "^15.6.0"
7+
"firebase": "^5.0.4",
8+
"gatsby": "^1.9.266",
9+
"gatsby-link": "^1.6.44",
10+
"gatsby-plugin-react-helmet": "^2.0.11",
11+
"gatsby-plugin-react-next": "^1.0.11",
12+
"react-helmet": "^5.2.0"
1213
},
1314
"keywords": [
1415
"gatsby"
@@ -22,6 +23,6 @@
2223
"test": "echo \"No test specified\" && exit 0"
2324
},
2425
"devDependencies": {
25-
"prettier": "^1.8.2"
26+
"prettier": "^1.13.3"
2627
}
2728
}

src/components/Navigation/index.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
32
import Link from 'gatsby-link';
43

4+
import AuthUserContext from '../Session/AuthUserContext';
55
import SignOutButton from '../SignOut';
66
import * as routes from '../../constants/routes';
77

8-
const Navigation = (props, { authUser }) =>
9-
<div>
10-
{ authUser
11-
? <NavigationAuth />
12-
: <NavigationNonAuth />
8+
const Navigation = () =>
9+
<AuthUserContext.Consumer>
10+
{authUser => authUser
11+
? <NavigationAuth />
12+
: <NavigationNonAuth />
1313
}
14-
</div>
15-
16-
Navigation.contextTypes = {
17-
authUser: PropTypes.object,
18-
};
14+
</AuthUserContext.Consumer>
1915

2016
const NavigationAuth = () =>
2117
<ul>
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;
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
32

3+
import AuthUserContext from './AuthUserContext';
44
import { firebase } from '../../firebase';
55

6-
const withAuthentication = (Component) => {
6+
const withAuthentication = (Component) =>
77
class WithAuthentication extends React.Component {
88
constructor(props) {
99
super(props);
@@ -13,12 +13,6 @@ const withAuthentication = (Component) => {
1313
};
1414
}
1515

16-
getChildContext() {
17-
return {
18-
authUser: this.state.authUser,
19-
};
20-
}
21-
2216
componentDidMount() {
2317
firebase.auth.onAuthStateChanged(authUser => {
2418
authUser
@@ -28,17 +22,14 @@ const withAuthentication = (Component) => {
2822
}
2923

3024
render() {
25+
const { authUser } = this.state;
26+
3127
return (
32-
<Component { ...this.props } />
28+
<AuthUserContext.Provider value={authUser}>
29+
<Component { ...this.props } />
30+
</AuthUserContext.Provider>
3331
);
3432
}
3533
}
3634

37-
WithAuthentication.childContextTypes = {
38-
authUser: PropTypes.object,
39-
};
40-
41-
return WithAuthentication;
42-
}
43-
4435
export default withAuthentication;

src/components/Session/withAuthorization.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
32
import { withRouter } from 'react-router-dom';
43

4+
import AuthUserContext from '../Session/AuthUserContext';
55
import { firebase } from '../../firebase';
66
import * as routes from '../../constants/routes';
77

@@ -16,14 +16,14 @@ const withAuthorization = (condition) => (Component) => {
1616
}
1717

1818
render() {
19-
return this.context.authUser ? <Component { ...this.props } /> : null;
19+
return (
20+
<AuthUserContext.Consumer>
21+
{authUser => authUser ? <Component { ...this.props } /> : null}
22+
</AuthUserContext.Consumer>
23+
);
2024
}
2125
}
2226

23-
WithAuthorization.contextTypes = {
24-
authUser: PropTypes.object,
25-
};
26-
2727
return withRouter(WithAuthorization);
2828
}
2929

src/components/SignUp/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SignUpForm extends Component {
4141
.then(authUser => {
4242

4343
// Create a user in your own accessible Firebase Database too
44-
db.doCreateUser(authUser.uid, username, email)
44+
db.doCreateUser(authUser.user.uid, username, email)
4545
.then(() => {
4646
this.setState(() => ({ ...INITIAL_STATE }));
4747
history.push(routes.HOME);

src/firebase/firebase.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import * as firebase from 'firebase';
1+
import firebase from 'firebase/app';
2+
import 'firebase/auth';
3+
import 'firebase/database';
24

35
const prodConfig = {
46
apiKey: YOUR_API_KEY,

src/layouts/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
32
import Helmet from 'react-helmet';
43

54
import Navigation from '../components/Navigation';
@@ -24,10 +23,6 @@ const TemplateWrapper = ({ children }) => (
2423
{children()}
2524
</div>
2625
</div>
27-
)
28-
29-
TemplateWrapper.propTypes = {
30-
children: PropTypes.func,
31-
}
26+
);
3227

3328
export default withAuthentication(TemplateWrapper)

0 commit comments

Comments
 (0)