Skip to content

Commit e6ccd18

Browse files
authored
Merge pull request #5 from ckj/master
Gatsby 2.0
2 parents fdd1728 + ad5937b commit e6ccd18

File tree

25 files changed

+284
-209
lines changed

25 files changed

+284
-209
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`, `gatsby-plugin-react-next`],
5+
plugins: [`gatsby-plugin-react-helmet`],
66
}

package.json

Lines changed: 6 additions & 5 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+
"bluebird": "^3.5.1",
78
"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",
9+
"gatsby": "next",
10+
"gatsby-plugin-react-helmet": "next",
11+
"react": "^16.4.1",
12+
"react-dom": "^16.4.1",
1213
"react-helmet": "^5.2.0"
1314
},
1415
"keywords": [
@@ -19,7 +20,7 @@
1920
"scripts": {
2021
"build": "gatsby build",
2122
"develop": "gatsby develop",
22-
"format": "prettier --trailing-comma es5 --no-semi --single-quote --write \"src/**/*.js\"",
23+
"format": "prettier --trailing-comma es5 --single-quote --write \"src/**/*.js\"",
2324
"test": "echo \"No test specified\" && exit 0"
2425
},
2526
"devDependencies": {

src/components/App/index.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import React from 'react';
2-
import {
3-
BrowserRouter as Router,
4-
Route,
5-
} from 'react-router-dom';
2+
import { BrowserRouter as Router, Route } from 'react-router-dom';
63

74
import Navigation from '../Navigation';
85
import LandingPage from '../Landing';
@@ -16,24 +13,44 @@ import * as routes from '../../constants/routes';
1613

1714
import './index.css';
1815

19-
const App = () =>
16+
const App = () => (
2017
<Router>
2118
<div className="app">
2219
<Navigation />
23-
24-
<hr/>
25-
20+
<hr />
2621
<Route exact path={routes.LANDING} component={() => <LandingPage />} />
2722
<Route exact path={routes.SIGN_UP} component={() => <SignUpPage />} />
2823
<Route exact path={routes.SIGN_IN} component={() => <SignInPage />} />
29-
<Route exact path={routes.PASSWORD_FORGET} component={() => <PasswordForgetPage />} />
24+
<Route
25+
exact
26+
path={routes.PASSWORD_FORGET}
27+
component={() => <PasswordForgetPage />}
28+
/>
3029
<Route exact path={routes.HOME} component={() => <HomePage />} />
3130
<Route exact path={routes.ACCOUNT} component={() => <AccountPage />} />
32-
33-
<hr/>
34-
35-
<span>Found in <a href="https://roadtoreact.com/course-details?courseId=TAMING_THE_STATE">Taming the State in React</a></span> | <span>Star the <a href="https://github.com/rwieruch/react-firebase-authentication">Repository</a></span> | <span>Receive a <a href="https://www.getrevue.co/profile/rwieruch">Developer's Newsletter</a></span>
31+
<hr />
32+
<span>
33+
Found in{' '}
34+
<a href="https://roadtoreact.com/course-details?courseId=TAMING_THE_STATE">
35+
Taming the State in React
36+
</a>
37+
</span>{' '}
38+
|{' '}
39+
<span>
40+
Star the{' '}
41+
<a href="https://github.com/rwieruch/react-firebase-authentication">
42+
Repository
43+
</a>
44+
</span>{' '}
45+
|{' '}
46+
<span>
47+
Receive a{' '}
48+
<a href="https://www.getrevue.co/profile/rwieruch">
49+
Developer's Newsletter
50+
</a>
51+
</span>
3652
</div>
3753
</Router>
54+
);
3855

39-
export default withAuthentication(App);
56+
export default withAuthentication(App);

src/components/Navigation/index.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
11
import React from 'react';
2-
import Link from 'gatsby-link';
2+
import { Link } from 'gatsby';
33

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

8-
const Navigation = () =>
8+
const Navigation = () => (
99
<AuthUserContext.Consumer>
10-
{authUser => authUser
11-
? <NavigationAuth />
12-
: <NavigationNonAuth />
13-
}
10+
{authUser => (authUser ? <NavigationAuth /> : <NavigationNonAuth />)}
1411
</AuthUserContext.Consumer>
12+
);
1513

16-
const NavigationAuth = () =>
14+
const NavigationAuth = () => (
1715
<ul>
18-
<li><Link to={routes.LANDING}>Landing</Link></li>
19-
<li><Link to={routes.HOME}>Home</Link></li>
20-
<li><Link to={routes.ACCOUNT}>Account</Link></li>
21-
<li><SignOutButton /></li>
16+
<li>
17+
<Link to={routes.LANDING}>Landing</Link>
18+
</li>
19+
<li>
20+
<Link to={routes.HOME}>Home</Link>
21+
</li>
22+
<li>
23+
<Link to={routes.ACCOUNT}>Account</Link>
24+
</li>
25+
<li>
26+
<SignOutButton />
27+
</li>
2228
</ul>
29+
);
2330

24-
const NavigationNonAuth = () =>
31+
const NavigationNonAuth = () => (
2532
<ul>
26-
<li><Link to={routes.LANDING}>Landing</Link></li>
27-
<li><Link to={routes.SIGN_IN}>Sign In</Link></li>
33+
<li>
34+
<Link to={routes.LANDING}>Landing</Link>
35+
</li>
36+
<li>
37+
<Link to={routes.SIGN_IN}>Sign In</Link>
38+
</li>
2839
</ul>
40+
);
2941

3042
export default Navigation;

src/components/PasswordChange/index.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ class PasswordChangeForm extends Component {
1919
this.state = { ...INITIAL_STATE };
2020
}
2121

22-
onSubmit = (event) => {
22+
onSubmit = event => {
2323
const { passwordOne } = this.state;
2424

25-
auth.doPasswordUpdate(passwordOne)
25+
auth
26+
.doPasswordUpdate(passwordOne)
2627
.then(() => {
2728
this.setState(() => ({ ...INITIAL_STATE }));
2829
})
@@ -31,41 +32,43 @@ class PasswordChangeForm extends Component {
3132
});
3233

3334
event.preventDefault();
34-
}
35+
};
3536

3637
render() {
37-
const {
38-
passwordOne,
39-
passwordTwo,
40-
error,
41-
} = this.state;
38+
const { passwordOne, passwordTwo, error } = this.state;
4239

43-
const isInvalid =
44-
passwordOne !== passwordTwo ||
45-
passwordOne === '';
40+
const isInvalid = passwordOne !== passwordTwo || passwordOne === '';
4641

4742
return (
4843
<form onSubmit={this.onSubmit}>
4944
<input
5045
value={passwordOne}
51-
onChange={event => this.setState(updateByPropertyName('passwordOne', event.target.value))}
46+
onChange={event =>
47+
this.setState(
48+
updateByPropertyName('passwordOne', event.target.value)
49+
)
50+
}
5251
type="password"
5352
placeholder="New Password"
5453
/>
5554
<input
5655
value={passwordTwo}
57-
onChange={event => this.setState(updateByPropertyName('passwordTwo', event.target.value))}
56+
onChange={event =>
57+
this.setState(
58+
updateByPropertyName('passwordTwo', event.target.value)
59+
)
60+
}
5861
type="password"
5962
placeholder="Confirm New Password"
6063
/>
6164
<button disabled={isInvalid} type="submit">
6265
Reset My Password
6366
</button>
6467

65-
{ error && <p>{error.message}</p> }
68+
{error && <p>{error.message}</p>}
6669
</form>
6770
);
6871
}
6972
}
7073

71-
export default PasswordChangeForm;
74+
export default PasswordChangeForm;
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react';
2-
import { Link } from 'react-router-dom';
2+
import { Link } from 'gatsby';
33

44
import { auth } from '../../firebase';
55
import * as routes from '../../constants/routes';
@@ -20,10 +20,11 @@ class PasswordForgetForm extends Component {
2020
this.state = { ...INITIAL_STATE };
2121
}
2222

23-
onSubmit = (event) => {
23+
onSubmit = event => {
2424
const { email } = this.state;
2525

26-
auth.doPasswordReset(email)
26+
auth
27+
.doPasswordReset(email)
2728
.then(() => {
2829
this.setState(() => ({ ...INITIAL_STATE }));
2930
})
@@ -32,40 +33,37 @@ class PasswordForgetForm extends Component {
3233
});
3334

3435
event.preventDefault();
35-
}
36+
};
3637

3738
render() {
38-
const {
39-
email,
40-
error,
41-
} = this.state;
39+
const { email, error } = this.state;
4240

4341
const isInvalid = email === '';
4442

4543
return (
4644
<form onSubmit={this.onSubmit}>
4745
<input
4846
value={this.state.email}
49-
onChange={event => this.setState(updateByPropertyName('email', event.target.value))}
47+
onChange={event =>
48+
this.setState(updateByPropertyName('email', event.target.value))
49+
}
5050
type="text"
5151
placeholder="Email Address"
5252
/>
5353
<button disabled={isInvalid} type="submit">
5454
Reset My Password
5555
</button>
5656

57-
{ error && <p>{error.message}</p> }
57+
{error && <p>{error.message}</p>}
5858
</form>
5959
);
6060
}
6161
}
6262

63-
const PasswordForgetLink = () =>
63+
const PasswordForgetLink = () => (
6464
<p>
6565
<Link to={routes.PASSWORD_FORGET}>Forgot Password?</Link>
6666
</p>
67+
);
6768

68-
export {
69-
PasswordForgetForm,
70-
PasswordForgetLink,
71-
};
69+
export { PasswordForgetForm, PasswordForgetLink };

src/components/Session/AuthUserContext.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import React from 'react';
22

33
const AuthUserContext = React.createContext(null);
44

5-
export default AuthUserContext;
5+
export default AuthUserContext;

src/components/Session/withAuthentication.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react';
33
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);
@@ -14,22 +14,24 @@ const withAuthentication = (Component) =>
1414
}
1515

1616
componentDidMount() {
17-
firebase.auth.onAuthStateChanged(authUser => {
18-
authUser
19-
? this.setState(() => ({ authUser }))
20-
: this.setState(() => ({ authUser: null }));
21-
});
17+
if (typeof window !== 'undefined') {
18+
firebase.auth.onAuthStateChanged(authUser => {
19+
authUser
20+
? this.setState(() => ({ authUser }))
21+
: this.setState(() => ({ authUser: null }));
22+
});
23+
}
2224
}
2325

2426
render() {
2527
const { authUser } = this.state;
2628

2729
return (
2830
<AuthUserContext.Provider value={authUser}>
29-
<Component { ...this.props } />
31+
<Component {...this.props} />
3032
</AuthUserContext.Provider>
3133
);
3234
}
33-
}
35+
};
3436

35-
export default withAuthentication;
37+
export default withAuthentication;

src/components/Session/withAuthorization.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@ import AuthUserContext from '../Session/AuthUserContext';
55
import { firebase } from '../../firebase';
66
import * as routes from '../../constants/routes';
77

8-
const withAuthorization = (condition) => (Component) => {
8+
const withAuthorization = condition => Component => {
99
class WithAuthorization extends React.Component {
1010
componentDidMount() {
11-
firebase.auth.onAuthStateChanged(authUser => {
12-
if (!condition(authUser)) {
13-
this.props.history.push(routes.SIGN_IN);
14-
}
15-
});
11+
if (typeof window !== 'undefined') {
12+
firebase.auth.onAuthStateChanged(authUser => {
13+
if (!condition(authUser)) {
14+
this.props.history.push(routes.SIGN_IN);
15+
}
16+
});
17+
}
1618
}
1719

1820
render() {
1921
return (
2022
<AuthUserContext.Consumer>
21-
{authUser => authUser ? <Component { ...this.props } /> : null}
23+
{authUser => (authUser ? <Component {...this.props} /> : null)}
2224
</AuthUserContext.Consumer>
2325
);
2426
}
2527
}
2628

2729
return withRouter(WithAuthorization);
28-
}
30+
};
2931

30-
export default withAuthorization;
32+
export default withAuthorization;

0 commit comments

Comments
 (0)