Skip to content

Commit 0566373

Browse files
committed
call getAuth() directly all the time
1 parent e1fa9c1 commit 0566373

5 files changed

Lines changed: 28 additions & 55 deletions

File tree

docs/auth/multi-factor-auth.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ operations:
2323
```js
2424
import authModule, { PhoneAuthProvider, getAuth, multiFactor } from '@react-native-firebase/auth';
2525

26-
const auth = getAuth();
27-
const multiFactorUser = await multiFactor(auth.currentUser);
26+
const multiFactorUser = await multiFactor(getAuth().currentUser);
2827
```
2928

3029
Request the session identifier and use the phone number obtained from the user
@@ -38,7 +37,7 @@ const phoneOptions = {
3837
};
3938

4039
// Sends a text message to the user
41-
const verificationId = await auth.verifyPhoneNumberForMultiFactor(phoneOptions);
40+
const verificationId = await getAuth().verifyPhoneNumberForMultiFactor(phoneOptions);
4241
```
4342

4443
Once the user has provided the verification code received by text message, you
@@ -67,9 +66,7 @@ import authModule, {
6766
getMultiFactorResolver,
6867
} from '@react-native-firebase/auth';
6968

70-
const auth = getAuth();
71-
72-
signInWithEmailAndPassword(auth, email, password)
69+
signInWithEmailAndPassword(getAuth(), email, password)
7370
.then(() => {
7471
// User has not enrolled a second factor
7572
})
@@ -89,7 +86,7 @@ Using the error object you can obtain a
8986
continue the flow:
9087

9188
```js
92-
const resolver = getMultiFactorResolver(auth, error);
89+
const resolver = getMultiFactorResolver(getAuth(), error);
9390
```
9491

9592
The resolver object has all the required information to prompt the user for a
@@ -113,7 +110,7 @@ verification code to the user:
113110
const hint = resolver.hints[0];
114111
const sessionId = resolver.session;
115112

116-
auth
113+
getAuth()
117114
.verifyPhoneNumberWithMultiFactorInfo(hint, sessionId) // triggers the message to the user
118115
.then(verificationId => setVerificationId(verificationId));
119116
```
@@ -145,9 +142,7 @@ import authModule, {
145142
getMultiFactorResolver,
146143
} from '@react-native-firebase/auth';
147144

148-
const auth = getAuth();
149-
150-
signInWithEmailAndPassword(auth, email, password)
145+
signInWithEmailAndPassword(getAuth(), email, password)
151146
.then(() => {
152147
// User has not enrolled a second factor
153148
})
@@ -166,7 +161,7 @@ signInWithEmailAndPassword(auth, email, password)
166161
const hint = resolver.hints[0];
167162
const sessionId = resolver.session;
168163

169-
auth
164+
getAuth()
170165
.verifyPhoneNumberWithMultiFactorInfo(hint, sessionId) // triggers the message to the user
171166
.then(verificationId => setVerificationId(verificationId));
172167

docs/auth/oidc-auth.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,5 @@ const credential = OIDCAuthProvider.credential(
6464
authState.idToken,
6565
);
6666

67-
const auth = getAuth();
68-
await signInWithCredential(auth, credential);
67+
await signInWithCredential(getAuth(), credential);
6968
```

docs/auth/phone-auth.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ a code. Based on whether the code is correct for the device, the method rejects
5656
The example below demonstrates how you could setup such a flow within your own application:
5757

5858
```jsx
59-
import React, { useState, useEffect } from 'react';
59+
import { useState, useEffect } from 'react';
6060
import { Button, TextInput } from 'react-native';
6161
import { getAuth, onAuthStateChanged, signInWithPhoneNumber } from '@react-native-firebase/auth';
6262

63-
const auth = getAuth();
64-
6563
function PhoneSignIn() {
6664
// If null, no SMS has been sent
6765
const [confirm, setConfirm] = useState(null);
@@ -80,13 +78,13 @@ function PhoneSignIn() {
8078
}
8179

8280
useEffect(() => {
83-
const subscriber = onAuthStateChanged(auth, handleAuthStateChanged);
81+
const subscriber = onAuthStateChanged(getAuth(), handleAuthStateChanged);
8482
return subscriber; // unsubscribe on unmount
8583
}, []);
8684

8785
// Handle the button press
8886
async function handleSignInWithPhoneNumber(phoneNumber) {
89-
const confirmation = await signInWithPhoneNumber(auth, phoneNumber);
87+
const confirmation = await signInWithPhoneNumber(getAuth(), phoneNumber);
9088
setConfirm(confirmation);
9189
}
9290

@@ -148,8 +146,6 @@ import {
148146
verifyPhoneNumber,
149147
} from '@react-native-firebase/auth';
150148

151-
const auth = getAuth();
152-
153149
export default function PhoneVerification() {
154150
// Set an initializing state whilst Firebase connects
155151
const [initializing, setInitializing] = useState(true);
@@ -167,14 +163,14 @@ export default function PhoneVerification() {
167163
}
168164

169165
useEffect(() => {
170-
const subscriber = onAuthStateChanged(auth, handleAuthStateChanged);
166+
const subscriber = onAuthStateChanged(getAuth(), handleAuthStateChanged);
171167
return subscriber; // unsubscribe on unmount
172168
}, []);
173169

174170
// Handle create account button press
175171
async function createAccount() {
176172
try {
177-
await createUserWithEmailAndPassword(auth, 'jane.doe@example.com', 'SuperSecretPassword!');
173+
await createUserWithEmailAndPassword(getAuth(), 'jane.doe@example.com', 'SuperSecretPassword!');
178174
console.log('User account created & signed in!');
179175
} catch (error) {
180176
if (error.code === 'auth/email-already-in-use') {
@@ -190,15 +186,15 @@ export default function PhoneVerification() {
190186

191187
// Handle the verify phone button press
192188
async function handlePhoneNumberVerification(phoneNumber) {
193-
const confirmation = await verifyPhoneNumber(auth, phoneNumber);
189+
const confirmation = await verifyPhoneNumber(getAuth(), phoneNumber);
194190
setConfirm(confirmation);
195191
}
196192

197193
// Handle confirm code button press
198194
async function confirmCode() {
199195
try {
200196
const credential = PhoneAuthProvider.credential(confirm.verificationId, code);
201-
let userData = await auth.currentUser.linkWithCredential(credential);
197+
let userData = await getAuth().currentUser.linkWithCredential(credential);
202198
setUser(userData.user);
203199
} catch (error) {
204200
if (error.code == 'auth/invalid-verification-code') {

docs/auth/social-auth.md

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ async function onAppleButtonPress() {
7474
const appleCredential = AppleAuthProvider.credential(identityToken, nonce);
7575

7676
// Sign the user in with the credential
77-
const auth = getAuth();
78-
return signInWithCredential(auth, appleCredential);
77+
return signInWithCredential(getAuth(), appleCredential);
7978
}
8079
```
8180

@@ -85,7 +84,7 @@ with the new authentication state of the user.
8584
Apple also requires that the app revoke the `Sign in with Apple` token when the user chooses to delete their account. This can be accomplished with the `revokeToken` API.
8685

8786
```js
88-
import { getAuth } from '@react-native-firebase/auth';
87+
import { getAuth, revokeToken } from '@react-native-firebase/auth';
8988
import { appleAuth } from '@invertase/react-native-apple-authentication';
9089

9190
async function revokeSignInWithAppleToken() {
@@ -100,8 +99,7 @@ async function revokeSignInWithAppleToken() {
10099
}
101100

102101
// Revoke the token
103-
const auth = getAuth();
104-
return auth.revokeToken(auth, authorizationCode);
102+
return revokeToken(getAuth(), authorizationCode);
105103
}
106104
```
107105

@@ -158,8 +156,7 @@ async function onFacebookButtonPress() {
158156
const facebookCredential = FacebookAuthProvider.credential(data.accessToken);
159157

160158
// Sign-in the user with the credential
161-
const auth = getAuth();
162-
return signInWithCredential(auth, facebookCredential);
159+
return signInWithCredential(getAuth(), facebookCredential);
163160
}
164161
```
165162

@@ -200,8 +197,7 @@ async function onFacebookButtonPress() {
200197
const facebookCredential = FacebookAuthProvider.credential(data.authenticationToken, nonce);
201198

202199
// Sign-in the user with the credential
203-
const auth = getAuth();
204-
return signInWithCredential(auth, facebookCredential);
200+
return signInWithCredential(getAuth(), facebookCredential);
205201
}
206202
```
207203

@@ -277,8 +273,7 @@ async function onGoogleButtonPress() {
277273
const googleCredential = GoogleAuthProvider.credential(signInResult.data.idToken);
278274

279275
// Sign-in the user with the credential
280-
const auth = getAuth();
281-
return signInWithCredential(auth, googleCredential);
276+
return signInWithCredential(getAuth(), googleCredential);
282277
}
283278
```
284279
@@ -332,8 +327,7 @@ const onMicrosoftButtonPress = async () => {
332327
});
333328

334329
// Sign-in the user with the provider
335-
const auth = getAuth();
336-
return signInWithRedirect(auth, provider);
330+
return signInWithRedirect(getAuth(), provider);
337331
};
338332
```
339333
@@ -393,8 +387,7 @@ async function onTwitterButtonPress() {
393387
const twitterCredential = TwitterAuthProvider.credential(authToken, authTokenSecret);
394388

395389
// Sign-in the user with the credential
396-
const auth = getAuth();
397-
return signInWithCredential(auth, twitterCredential);
390+
return signInWithCredential(getAuth(), twitterCredential);
398391
}
399392
```
400393
@@ -417,8 +410,6 @@ This code demonstrates linking a Google provider to an account that is already s
417410
import { GoogleAuthProvider, getAuth } from '@react-native-firebase/auth';
418411
import { GoogleSignin } from '@react-native-google-signin/google-signin';
419412

420-
const auth = getAuth();
421-
422413
async function onGoogleLinkButtonPress() {
423414
// Ensure the device supports Google Play services
424415
await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
@@ -429,7 +420,7 @@ async function onGoogleLinkButtonPress() {
429420
const googleCredential = GoogleAuthProvider.credential(idToken);
430421

431422
// Link the user's account with the Google credential
432-
const firebaseUserCredential = await auth.currentUser.linkWithCredential(googleCredential);
423+
const firebaseUserCredential = await getAuth().currentUser.linkWithCredential(googleCredential);
433424
// Handle the linked account as needed in your app
434425
return;
435426
}

docs/auth/usage/index.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ import React, { useState, useEffect } from 'react';
5252
import { View, Text } from 'react-native';
5353
import { getAuth, onAuthStateChanged } from '@react-native-firebase/auth';
5454

55-
const auth = getAuth();
56-
5755
function App() {
5856
// Set an initializing state whilst Firebase connects
5957
const [initializing, setInitializing] = useState(true);
@@ -66,7 +64,7 @@ function App() {
6664
}
6765

6866
useEffect(() => {
69-
const subscriber = onAuthStateChanged(auth, onAuthStateChanged);
67+
const subscriber = onAuthStateChanged(getAuth(), onAuthStateChanged);
7068
return subscriber; // unsubscribe on unmount
7169
}, []);
7270

@@ -114,9 +112,7 @@ Ensure the "Anonymous" sign-in provider is enabled on the [Firebase Console](htt
114112
```js
115113
import { getAuth, signInAnonymously } from '@react-native-firebase/auth';
116114

117-
const auth = getAuth();
118-
119-
signInAnonymously()
115+
signInAnonymously(getAuth())
120116
.then(() => {
121117
console.log('User signed in anonymously');
122118
})
@@ -149,9 +145,7 @@ then signing them in.
149145
```js
150146
import { getAuth, createUserWithEmailAndPassword } from '@react-native-firebase/auth';
151147

152-
const auth = getAuth();
153-
154-
createUserWithEmailAndPassword(auth, 'jane.doe@example.com', 'SuperSecretPassword!')
148+
createUserWithEmailAndPassword(getAuth(), 'jane.doe@example.com', 'SuperSecretPassword!')
155149
.then(() => {
156150
console.log('User account created & signed in!');
157151
})
@@ -185,9 +179,7 @@ If you'd like to sign the user out of their current authentication state, call t
185179
```js
186180
import { getAuth, signOut } from '@react-native-firebase/auth';
187181

188-
const auth = getAuth();
189-
190-
signOut(auth).then(() => console.log('User signed out!'));
182+
signOut(getAuth()).then(() => console.log('User signed out!'));
191183
```
192184

193185
Once successfully signed out, any [`onAuthStateChanged`](#listening-to-authentication-state) listeners will trigger an event

0 commit comments

Comments
 (0)