@@ -67,30 +67,30 @@ const uiConfig = {
6767 // We will display Google and Facebook as auth providers.
6868 signInOptions: [
6969 firebase .auth .GoogleAuthProvider .PROVIDER_ID ,
70- firebase .auth .FacebookAuthProvider .PROVIDER_ID
71- ]
70+ firebase .auth .FacebookAuthProvider .PROVIDER_ID ,
71+ ],
7272};
7373
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+ );
8482}
83+
84+ export default SignInScreen
8585```
8686
87- ### Using ` FirebaseAuth ` with local state.
87+ ### Using ` StyledFirebaseAuth ` with local state.
8888
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:
9090
9191``` js
9292// Import FirebaseAuth and firebase.
93- import React from ' react' ;
93+ import React , { useEffect , useState } from ' react' ;
9494import StyledFirebaseAuth from ' react-firebaseui/StyledFirebaseAuth' ;
9595import firebase from ' firebase' ;
9696
@@ -102,59 +102,51 @@ const config = {
102102};
103103firebase .initializeApp (config);
104104
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.
126122
127123 // 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) {
149132 return (
150133 < div>
151134 < 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 ()} / >
154137 < / div>
155138 );
156139 }
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+ );
157147}
148+
149+ export default SignInScreen ;
158150```
159151
160152### Accessing the FirebaseUI instance
@@ -165,15 +157,13 @@ To do this you can pass a `uiCallback` callback function that wil be passed the
165157``` js
166158// ...
167159
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+ );
177167```
178168
179169
0 commit comments