Skip to content

Commit 94bb3a9

Browse files
committed
11-Protected Routes in React with Authorization
1 parent a644850 commit 94bb3a9

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

src/components/Account/index.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import React from 'react';
22

3+
import { AuthUserContext } from '../Session';
34
import { PasswordForgetForm } from '../PasswordForget';
45
import PasswordChangeForm from '../PasswordChange';
6+
import { withAuthorization } from '../Session';
57

68
const AccountPage = () => (
7-
<div>
8-
<h1>Account Page</h1>
9-
<PasswordForgetForm />
10-
<PasswordChangeForm />
11-
</div>
9+
<AuthUserContext.Consumer>
10+
{authUser => (
11+
<div>
12+
<h1>Account: {authUser.email}</h1>
13+
<PasswordForgetForm />
14+
<PasswordChangeForm />
15+
</div>
16+
)}
17+
</AuthUserContext.Consumer>
1218
);
1319

14-
export default AccountPage;
20+
const authCondition = authUser => !!authUser;
21+
22+
export default withAuthorization(authCondition)(AccountPage);

src/components/Home/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import React from 'react';
22

3-
const Home = () => (
3+
import { withAuthorization } from '../Session';
4+
5+
const HomePage = () => (
46
<div>
5-
<h1>Home</h1>
7+
<h1>Home Page</h1>
8+
<p>The Home Page is accessible by every signed in user.</p>
69
</div>
710
);
811

9-
export default Home;
12+
const condition = authUser => !!authUser;
13+
14+
export default withAuthorization(condition)(HomePage);

src/components/Session/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import AuthUserContext from './context';
22
import withAuthentication from './withAuthentication';
3+
import withAuthorization from './withAuthorization';
34

4-
export { AuthUserContext, withAuthentication };
5+
export { AuthUserContext, withAuthentication, withAuthorization };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from 'react';
2+
import { withRouter } from 'react-router-dom';
3+
import { compose } from 'recompose';
4+
5+
import AuthUserContext from './context';
6+
import { withFirebase } from '../Firebase';
7+
import * as ROUTES from '../../constants/routes';
8+
9+
const withAuthorization = condition => Component => {
10+
class WithAuthorization extends React.Component {
11+
componentDidMount() {
12+
this.listener = this.props.firebase.auth.onAuthStateChanged(
13+
authUser => {
14+
if (!condition(authUser)) {
15+
this.props.history.push(ROUTES.SIGN_IN);
16+
}
17+
},
18+
);
19+
}
20+
21+
componentWillUnmount() {
22+
this.listener();
23+
}
24+
25+
render() {
26+
return (
27+
<AuthUserContext.Consumer>
28+
{authUser =>
29+
condition(authUser) ? <Component {...this.props} /> : null
30+
}
31+
</AuthUserContext.Consumer>
32+
);
33+
}
34+
}
35+
36+
return compose(
37+
withRouter,
38+
withFirebase,
39+
)(WithAuthorization);
40+
};
41+
42+
export default withAuthorization;

0 commit comments

Comments
 (0)