@@ -67,30 +67,30 @@ const uiConfig = {
67
67
// We will display Google and Facebook as auth providers.
68
68
signInOptions: [
69
69
firebase .auth .GoogleAuthProvider .PROVIDER_ID ,
70
- firebase .auth .FacebookAuthProvider .PROVIDER_ID
71
- ]
70
+ firebase .auth .FacebookAuthProvider .PROVIDER_ID ,
71
+ ],
72
72
};
73
73
74
- class SignInScreen extends React .Component {
75
- render () {
76
- return (
77
- < div>
78
- < h1> My App< / h1>
79
- < p> Please sign- in: < / p>
80
- < StyledFirebaseAuth uiConfig= {uiConfig} firebaseAuth= {firebase .auth ()}/ >
81
- < / div>
82
- );
83
- }
74
+ function SignInScreen () {
75
+ return (
76
+ < div>
77
+ < h1> My App< / h1>
78
+ < p> Please sign- in: < / p>
79
+ < StyledFirebaseAuth uiConfig= {uiConfig} firebaseAuth= {firebase .auth ()} / >
80
+ < / div>
81
+ );
84
82
}
83
+
84
+ export default SignInScreen
85
85
```
86
86
87
- ### Using ` FirebaseAuth ` with local state.
87
+ ### Using ` StyledFirebaseAuth ` with local state.
88
88
89
- Below is an example on how to use ` FirebaseAuth ` with a state change upon sign-in:
89
+ Below is an example on how to use ` StyledFirebaseAuth ` with a state change upon sign-in:
90
90
91
91
``` js
92
92
// Import FirebaseAuth and firebase.
93
- import React from ' react' ;
93
+ import React , { useEffect , useState } from ' react' ;
94
94
import StyledFirebaseAuth from ' react-firebaseui/StyledFirebaseAuth' ;
95
95
import firebase from ' firebase' ;
96
96
@@ -102,59 +102,51 @@ const config = {
102
102
};
103
103
firebase .initializeApp (config);
104
104
105
- class SignInScreen extends React .Component {
106
-
107
- // The component's Local state.
108
- state = {
109
- isSignedIn: false // Local signed-in state.
110
- };
111
-
112
- // Configure FirebaseUI.
113
- uiConfig = {
114
- // Popup signin flow rather than redirect flow.
115
- signInFlow: ' popup' ,
116
- // We will display Google and Facebook as auth providers.
117
- signInOptions: [
118
- firebase .auth .GoogleAuthProvider .PROVIDER_ID ,
119
- firebase .auth .FacebookAuthProvider .PROVIDER_ID
120
- ],
121
- callbacks: {
122
- // Avoid redirects after sign-in.
123
- signInSuccessWithAuthResult : () => false
124
- }
125
- };
105
+ // Configure FirebaseUI.
106
+ const uiConfig = {
107
+ // Popup signin flow rather than redirect flow.
108
+ signInFlow: ' popup' ,
109
+ // We will display Google and Facebook as auth providers.
110
+ signInOptions: [
111
+ firebase .auth .GoogleAuthProvider .PROVIDER_ID ,
112
+ firebase .auth .FacebookAuthProvider .PROVIDER_ID
113
+ ],
114
+ callbacks: {
115
+ // Avoid redirects after sign-in.
116
+ signInSuccessWithAuthResult : () => false ,
117
+ },
118
+ };
119
+
120
+ function SignInScreen () {
121
+ const [isSignedIn , setIsSignedIn ] = useState (false ); // Local signed-in state.
126
122
127
123
// Listen to the Firebase Auth state and set the local state.
128
- componentDidMount () {
129
- this .unregisterAuthObserver = firebase .auth ().onAuthStateChanged (
130
- (user ) => this .setState ({isSignedIn: !! user})
131
- );
132
- }
133
-
134
- // Make sure we un-register Firebase observers when the component unmounts.
135
- componentWillUnmount () {
136
- this .unregisterAuthObserver ();
137
- }
138
-
139
- render () {
140
- if (! this .state .isSignedIn ) {
141
- return (
142
- < div>
143
- < h1> My App< / h1>
144
- < p> Please sign- in: < / p>
145
- < StyledFirebaseAuth uiConfig= {this .uiConfig } firebaseAuth= {firebase .auth ()}/ >
146
- < / div>
147
- );
148
- }
124
+ useEffect (() => {
125
+ const unregisterAuthObserver = firebase .auth ().onAuthStateChanged (user => {
126
+ setIsSignedIn (!! user);
127
+ });
128
+ return () => unregisterAuthObserver (); // Make sure we un-register Firebase observers when the component unmounts.
129
+ }, []);
130
+
131
+ if (! isSignedIn) {
149
132
return (
150
133
< div>
151
134
< h1> My App< / h1>
152
- < p> Welcome { firebase . auth (). currentUser . displayName } ! You are now signed - in ! < / p>
153
- < a onClick = {() => firebase .auth (). signOut ()} > Sign - out < / a >
135
+ < p> Please sign - in: < / p>
136
+ < StyledFirebaseAuth uiConfig = {uiConfig} firebaseAuth = { firebase .auth ()} / >
154
137
< / div>
155
138
);
156
139
}
140
+ return (
141
+ < div>
142
+ < h1> My App< / h1>
143
+ < p> Welcome {firebase .auth ().currentUser .displayName }! You are now signed- in ! < / p>
144
+ < a onClick= {() => firebase .auth ().signOut ()}> Sign- out< / a>
145
+ < / div>
146
+ );
157
147
}
148
+
149
+ export default SignInScreen ;
158
150
```
159
151
160
152
### Accessing the FirebaseUI instance
@@ -165,15 +157,13 @@ To do this you can pass a `uiCallback` callback function that wil be passed the
165
157
``` js
166
158
// ...
167
159
168
- render () {
169
- return (
170
- < div>
171
- < h1> My App< / h1>
172
- < p> Please sign- in: < / p>
173
- < StyledFirebaseAuth uiCallback= {ui => ui .disableAutoSignIn ()} uiConfig= {this .uiConfig } firebaseAuth= {firebase .auth ()}/ >
174
- < / div>
175
- );
176
- }
160
+ return (
161
+ < div>
162
+ < h1> My App< / h1>
163
+ < p> Please sign- in: < / p>
164
+ < StyledFirebaseAuth uiCallback= {ui => ui .disableAutoSignIn ()} uiConfig= {uiConfig} firebaseAuth= {firebase .auth ()}/ >
165
+ < / div>
166
+ );
177
167
```
178
168
179
169
0 commit comments