@@ -48,15 +48,16 @@ Below is an example on how to use `FirebaseAuth` with a redirect upon sign-in:
48
48
// Import FirebaseAuth and firebase.
49
49
import React from ' react' ;
50
50
import StyledFirebaseAuth from ' react-firebaseui/StyledFirebaseAuth' ;
51
- import firebase from ' firebase' ;
51
+ import { initializeApp } from ' firebase/app' ;
52
+ import { getAuth , EmailAuthProvider , GoogleAuthProvider } from ' firebase/auth' ;
52
53
53
54
// Configure Firebase.
54
55
const config = {
55
56
apiKey: ' AIzaSyAeue-AsYu76MMQlTOM-KlbYBlusW9c1FM' ,
56
57
authDomain: ' myproject-1234.firebaseapp.com' ,
57
58
// ...
58
59
};
59
- firebase . initializeApp (config );
60
+ const firebaseApp = initializeApp (firebaseConfig );
60
61
61
62
// Configure FirebaseUI.
62
63
const uiConfig = {
@@ -66,17 +67,19 @@ const uiConfig = {
66
67
signInSuccessUrl: ' /signedIn' ,
67
68
// We will display Google and Facebook as auth providers.
68
69
signInOptions: [
69
- firebase . auth . GoogleAuthProvider .PROVIDER_ID ,
70
- firebase . auth . FacebookAuthProvider .PROVIDER_ID ,
70
+ GoogleAuthProvider .PROVIDER_ID ,
71
+ FacebookAuthProvider .PROVIDER_ID ,
71
72
],
72
73
};
73
74
74
75
function SignInScreen () {
76
+ const auth = getAuth (firebaseApp)
77
+
75
78
return (
76
79
< div>
77
80
< h1> My App< / h1>
78
81
< p> Please sign- in: < / p>
79
- < StyledFirebaseAuth uiConfig= {uiConfig} firebaseAuth= {firebase . auth () } / >
82
+ < StyledFirebaseAuth uiConfig= {uiConfig} firebaseAuth= {auth} / >
80
83
< / div>
81
84
);
82
85
}
@@ -92,24 +95,25 @@ Below is an example on how to use `StyledFirebaseAuth` with a state change upon
92
95
// Import FirebaseAuth and firebase.
93
96
import React , { useEffect , useState } from ' react' ;
94
97
import StyledFirebaseAuth from ' react-firebaseui/StyledFirebaseAuth' ;
95
- import firebase from ' firebase' ;
98
+ import { initializeApp } from ' firebase/app' ;
99
+ import { getAuth , onAuthStateChanged , signOut , EmailAuthProvider , GoogleAuthProvider } from ' firebase/auth' ;
96
100
97
101
// Configure Firebase.
98
102
const config = {
99
103
apiKey: ' AIzaSyAeue-AsYu76MMQlTOM-KlbYBlusW9c1FM' ,
100
104
authDomain: ' myproject-1234.firebaseapp.com' ,
101
105
// ...
102
106
};
103
- firebase . initializeApp (config );
107
+ const firebaseApp = initializeApp (firebaseConfig );
104
108
105
109
// Configure FirebaseUI.
106
110
const uiConfig = {
107
111
// Popup signin flow rather than redirect flow.
108
112
signInFlow: ' popup' ,
109
113
// We will display Google and Facebook as auth providers.
110
114
signInOptions: [
111
- firebase . auth . GoogleAuthProvider .PROVIDER_ID ,
112
- firebase . auth . FacebookAuthProvider .PROVIDER_ID
115
+ GoogleAuthProvider .PROVIDER_ID ,
116
+ FacebookAuthProvider .PROVIDER_ID
113
117
],
114
118
callbacks: {
115
119
// Avoid redirects after sign-in.
@@ -118,11 +122,12 @@ const uiConfig = {
118
122
};
119
123
120
124
function SignInScreen () {
125
+ const auth = getAuth (firebaseApp);
121
126
const [isSignedIn , setIsSignedIn ] = useState (false ); // Local signed-in state.
122
127
123
128
// Listen to the Firebase Auth state and set the local state.
124
129
useEffect (() => {
125
- const unregisterAuthObserver = firebase . auth (). onAuthStateChanged (user => {
130
+ const unregisterAuthObserver = onAuthStateChanged (auth, user => {
126
131
setIsSignedIn (!! user);
127
132
});
128
133
return () => unregisterAuthObserver (); // Make sure we un-register Firebase observers when the component unmounts.
@@ -133,15 +138,15 @@ function SignInScreen() {
133
138
< div>
134
139
< h1> My App< / h1>
135
140
< p> Please sign- in: < / p>
136
- < StyledFirebaseAuth uiConfig= {uiConfig} firebaseAuth= {firebase . auth () } / >
141
+ < StyledFirebaseAuth uiConfig= {uiConfig} firebaseAuth= {auth} / >
137
142
< / div>
138
143
);
139
144
}
140
145
return (
141
146
< div>
142
147
< 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>
148
+ < p> Welcome {auth .currentUser .displayName }! You are now signed- in ! < / p>
149
+ < a onClick= {() => signOut (auth )}> Sign- out< / a>
145
150
< / div>
146
151
);
147
152
}
@@ -161,7 +166,7 @@ return (
161
166
< div>
162
167
< h1> My App< / h1>
163
168
< p> Please sign- in: < / p>
164
- < StyledFirebaseAuth uiCallback= {ui => ui .disableAutoSignIn ()} uiConfig= {uiConfig} firebaseAuth= {firebase . auth () }/ >
169
+ < StyledFirebaseAuth uiCallback= {ui => ui .disableAutoSignIn ()} uiConfig= {uiConfig} firebaseAuth= {auth}/ >
165
170
< / div>
166
171
);
167
172
```
0 commit comments