File tree 4 files changed +66
-10
lines changed
4 files changed +66
-10
lines changed Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
3
+ import { AuthUserContext } from '../Session' ;
3
4
import { PasswordForgetForm } from '../PasswordForget' ;
4
5
import PasswordChangeForm from '../PasswordChange' ;
6
+ import { withAuthorization } from '../Session' ;
5
7
6
8
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 >
12
18
) ;
13
19
14
- export default AccountPage ;
20
+ const authCondition = authUser => ! ! authUser ;
21
+
22
+ export default withAuthorization ( authCondition ) ( AccountPage ) ;
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
3
- const Home = ( ) => (
3
+ import { withAuthorization } from '../Session' ;
4
+
5
+ const HomePage = ( ) => (
4
6
< div >
5
- < h1 > Home</ h1 >
7
+ < h1 > Home Page</ h1 >
8
+ < p > The Home Page is accessible by every signed in user.</ p >
6
9
</ div >
7
10
) ;
8
11
9
- export default Home ;
12
+ const condition = authUser => ! ! authUser ;
13
+
14
+ export default withAuthorization ( condition ) ( HomePage ) ;
Original file line number Diff line number Diff line change 1
1
import AuthUserContext from './context' ;
2
2
import withAuthentication from './withAuthentication' ;
3
+ import withAuthorization from './withAuthorization' ;
3
4
4
- export { AuthUserContext , withAuthentication } ;
5
+ export { AuthUserContext , withAuthentication , withAuthorization } ;
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments