Skip to content

Commit d449930

Browse files
authored
Merge pull request #11 from taming-the-state-in-react/make-it-work-production
Production Version
2 parents db89cc5 + d787502 commit d449930

File tree

29 files changed

+1356
-845
lines changed

29 files changed

+1356
-845
lines changed

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "all",
4+
"singleQuote": true,
5+
"printWidth": 70,
6+
}

package-lock.json

Lines changed: 1007 additions & 492 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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
"name": "gatsby-firebase-authentication",
33
"description": "Gatsby Firebase Authentication",
44
"version": "1.0.0",
5-
"author": "Robin Wieruch <wrobin@gmx.net>",
5+
"author": "Robin Wieruch <hello@rwieruch.com>",
66
"dependencies": {
7-
"firebase": "^5.0.4",
8-
"gatsby": "next",
9-
"gatsby-plugin-react-helmet": "next",
10-
"react": "^16.4.1",
11-
"react-dom": "^16.4.1",
7+
"firebase": "^5.5.2",
8+
"gatsby": "^2.0.12",
9+
"gatsby-plugin-react-helmet": "^3.0.0",
10+
"react": "^16.5.2",
11+
"react-dom": "^16.5.2",
1212
"react-helmet": "^5.2.0"
1313
},
1414
"keywords": [
@@ -23,6 +23,6 @@
2323
"test": "echo \"No test specified\" && exit 0"
2424
},
2525
"devDependencies": {
26-
"prettier": "^1.13.3"
26+
"prettier": "^1.14.3"
2727
}
2828
}

src/components/App/index.css

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/components/App/index.js

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
3+
const FirebaseContext = React.createContext(null);
4+
5+
export const withFirebase = Component => props => (
6+
<FirebaseContext.Consumer>
7+
{firebase => <Component {...props} firebase={firebase} />}
8+
</FirebaseContext.Consumer>
9+
);
10+
11+
export default FirebaseContext;

src/components/Firebase/index.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const prodConfig = {
2+
apiKey: YOUR_API_KEY,
3+
authDomain: YOUR_AUTH_DOMAIN,
4+
databaseURL: YOUR_DATABASE_URL,
5+
projectId: YOUR_PROJECT_ID,
6+
storageBucket: '',
7+
messagingSenderId: YOUR_MESSAGING_SENDER_ID,
8+
};
9+
10+
const devConfig = {
11+
apiKey: YOUR_API_KEY,
12+
authDomain: YOUR_AUTH_DOMAIN,
13+
databaseURL: YOUR_DATABASE_URL,
14+
projectId: YOUR_PROJECT_ID,
15+
storageBucket: '',
16+
messagingSenderId: YOUR_MESSAGING_SENDER_ID,
17+
};
18+
19+
const config =
20+
process.env.NODE_ENV === 'production' ? prodConfig : devConfig;
21+
22+
class Firebase {
23+
constructor(app) {
24+
app.initializeApp(config);
25+
26+
this.db = app.database();
27+
this.auth = app.auth();
28+
}
29+
30+
// *** Auth API ***
31+
32+
doCreateUserWithEmailAndPassword = (email, password) =>
33+
this.auth.createUserWithEmailAndPassword(email, password);
34+
35+
doSignInWithEmailAndPassword = (email, password) =>
36+
this.auth.signInWithEmailAndPassword(email, password);
37+
38+
doSignOut = () => this.auth.signOut();
39+
40+
doPasswordReset = email => this.auth.sendPasswordResetEmail(email);
41+
42+
doPasswordUpdate = password =>
43+
this.auth.currentUser.updatePassword(password);
44+
45+
// *** User API ***
46+
47+
doCreateUser = (id, username, email) =>
48+
this.db.ref(`users/${id}`).set({
49+
username,
50+
email,
51+
});
52+
53+
onceGetUsers = () => this.db.ref('users').once('value');
54+
}
55+
56+
let firebase;
57+
58+
function getFirebase(app, auth, database) {
59+
if (firebase) {
60+
return firebase;
61+
}
62+
63+
firebase = new Firebase(app, auth, database);
64+
65+
return firebase;
66+
}
67+
68+
export default getFirebase;

src/components/Navigation/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import React from 'react';
22
import { Link } from 'gatsby';
33

4+
import * as routes from '../../constants/routes';
45
import AuthUserContext from '../Session/AuthUserContext';
56
import SignOutButton from '../SignOut';
6-
import * as routes from '../../constants/routes';
77

88
const Navigation = () => (
99
<AuthUserContext.Consumer>
10-
{authUser => (authUser ? <NavigationAuth /> : <NavigationNonAuth />)}
10+
{authUser =>
11+
authUser ? <NavigationAuth /> : <NavigationNonAuth />
12+
}
1113
</AuthUserContext.Consumer>
1214
);
1315

src/components/PasswordChange/index.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import React, { Component } from 'react';
22

3-
import { auth } from '../../firebase';
4-
5-
const updateByPropertyName = (propertyName, value) => () => ({
6-
[propertyName]: value,
7-
});
3+
import { withFirebase } from '../Firebase/FirebaseContext';
84

95
const INITIAL_STATE = {
106
passwordOne: '',
@@ -22,13 +18,13 @@ class PasswordChangeForm extends Component {
2218
onSubmit = event => {
2319
const { passwordOne } = this.state;
2420

25-
auth
21+
this.props.firebase
2622
.doPasswordUpdate(passwordOne)
2723
.then(() => {
2824
this.setState(() => ({ ...INITIAL_STATE }));
2925
})
3026
.catch(error => {
31-
this.setState(updateByPropertyName('error', error));
27+
this.setState({ error });
3228
});
3329

3430
event.preventDefault();
@@ -37,26 +33,25 @@ class PasswordChangeForm extends Component {
3733
render() {
3834
const { passwordOne, passwordTwo, error } = this.state;
3935

40-
const isInvalid = passwordOne !== passwordTwo || passwordOne === '';
36+
const isInvalid =
37+
passwordOne !== passwordTwo || passwordOne === '';
4138

4239
return (
4340
<form onSubmit={this.onSubmit}>
4441
<input
42+
name="passwordOne"
4543
value={passwordOne}
4644
onChange={event =>
47-
this.setState(
48-
updateByPropertyName('passwordOne', event.target.value)
49-
)
45+
this.setState({ [event.target.name]: event.target.value })
5046
}
5147
type="password"
5248
placeholder="New Password"
5349
/>
5450
<input
51+
name="passwordTwo"
5552
value={passwordTwo}
5653
onChange={event =>
57-
this.setState(
58-
updateByPropertyName('passwordTwo', event.target.value)
59-
)
54+
this.setState({ [event.target.name]: event.target.value })
6055
}
6156
type="password"
6257
placeholder="Confirm New Password"
@@ -71,4 +66,4 @@ class PasswordChangeForm extends Component {
7166
}
7267
}
7368

74-
export default PasswordChangeForm;
69+
export default withFirebase(PasswordChangeForm);

src/components/PasswordForget/index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import React, { Component } from 'react';
22
import { Link } from 'gatsby';
33

4-
import { auth } from '../../firebase';
54
import * as routes from '../../constants/routes';
6-
7-
const updateByPropertyName = (propertyName, value) => () => ({
8-
[propertyName]: value,
9-
});
5+
import { withFirebase } from '../Firebase/FirebaseContext';
106

117
const INITIAL_STATE = {
128
email: '',
@@ -23,13 +19,13 @@ class PasswordForgetForm extends Component {
2319
onSubmit = event => {
2420
const { email } = this.state;
2521

26-
auth
22+
this.props.firebase
2723
.doPasswordReset(email)
2824
.then(() => {
2925
this.setState(() => ({ ...INITIAL_STATE }));
3026
})
3127
.catch(error => {
32-
this.setState(updateByPropertyName('error', error));
28+
this.setState({ error });
3329
});
3430

3531
event.preventDefault();
@@ -43,9 +39,10 @@ class PasswordForgetForm extends Component {
4339
return (
4440
<form onSubmit={this.onSubmit}>
4541
<input
42+
name="email"
4643
value={this.state.email}
4744
onChange={event =>
48-
this.setState(updateByPropertyName('email', event.target.value))
45+
this.setState({ [event.target.name]: event.target.value })
4946
}
5047
type="text"
5148
placeholder="Email Address"
@@ -66,4 +63,6 @@ const PasswordForgetLink = () => (
6663
</p>
6764
);
6865

69-
export { PasswordForgetForm, PasswordForgetLink };
66+
export { PasswordForgetLink };
67+
68+
export default withFirebase(PasswordForgetForm);

0 commit comments

Comments
 (0)