1
- import React , { Component } from 'react' ;
1
+ import React from 'react' ;
2
2
import Protected from './Protected' ;
3
3
import Public from './Public' ;
4
4
import netlifyIdentity from 'netlify-identity-widget' ;
@@ -10,7 +10,7 @@ import {
10
10
withRouter
11
11
} from 'react-router-dom' ;
12
12
13
- // https://reacttraining.com/react-router/web/example/auth-workflow
13
+ // copied straight from https://reacttraining.com/react-router/web/example/auth-workflow
14
14
////////////////////////////////////////////////////////////
15
15
// 1. Click the public page
16
16
// 2. Click the protected page
@@ -38,14 +38,30 @@ function AuthExample() {
38
38
) ;
39
39
}
40
40
41
+ const fakeAuth = {
42
+ isAuthenticated : false ,
43
+ authenticate ( cb ) {
44
+ this . isAuthenticated = true ;
45
+ netlifyIdentity . open ( ) ;
46
+ // setTimeout(cb, 100); // fake async
47
+ netlifyIdentity . on ( 'login' , cb ) ;
48
+ } ,
49
+ signout ( cb ) {
50
+ this . isAuthenticated = false ;
51
+ // setTimeout(cb, 100);
52
+ netlifyIdentity . logout ( ) ;
53
+ netlifyIdentity . on ( 'logout' , cb ) ;
54
+ }
55
+ } ;
56
+
41
57
const AuthButton = withRouter (
42
58
( { history } ) =>
43
- netlifyIdentity . currentUser ( ) ? (
59
+ fakeAuth . isAuthenticated ? (
44
60
< p >
45
61
Welcome!{ ' ' }
46
62
< button
47
63
onClick = { ( ) => {
48
- netlifyIdentity . logout ( ) . then ( ( ) => history . push ( '/' ) ) ;
64
+ fakeAuth . signout ( ( ) => history . push ( '/' ) ) ;
49
65
} }
50
66
>
51
67
Sign out
@@ -61,7 +77,7 @@ function PrivateRoute({ component: Component, ...rest }) {
61
77
< Route
62
78
{ ...rest }
63
79
render = { props =>
64
- netlifyIdentity . currentUser ( ) ? (
80
+ fakeAuth . isAuthenticated ? (
65
81
< Component { ...props } />
66
82
) : (
67
83
< Redirect
@@ -78,14 +94,13 @@ function PrivateRoute({ component: Component, ...rest }) {
78
94
79
95
class Login extends React . Component {
80
96
state = { redirectToReferrer : false } ;
81
- componentDidMount = ( ) => {
82
- netlifyIdentity . on ( 'login' , ( ) =>
83
- this . setState ( { redirectToReferrer : true } )
84
- ) ;
85
- } ;
97
+
86
98
login = ( ) => {
87
- netlifyIdentity . open ( ) ;
99
+ fakeAuth . authenticate ( ( ) => {
100
+ this . setState ( { redirectToReferrer : true } ) ;
101
+ } ) ;
88
102
} ;
103
+
89
104
render ( ) {
90
105
let { from } = this . props . location . state || { from : { pathname : '/' } } ;
91
106
let { redirectToReferrer } = this . state ;
0 commit comments