Skip to content

Commit 484b04c

Browse files
authored
docs(auth): update examples to use modular API (#8327)
1 parent af435e8 commit 484b04c

File tree

5 files changed

+106
-83
lines changed

5 files changed

+106
-83
lines changed

docs/auth/multi-factor-auth.md

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ instance for the current user. This is the entry point for most multi-factor
2121
operations:
2222

2323
```js
24-
import auth from '@react-native-firebase/auth';
25-
const multiFactorUser = await auth().multiFactor(auth().currentUser);
24+
import {
25+
PhoneAuthProvider,
26+
PhoneMultiFactorGenerator,
27+
getAuth,
28+
multiFactor,
29+
} from '@react-native-firebase/auth';
30+
31+
const multiFactorUser = await multiFactor(getAuth().currentUser);
2632
```
2733

2834
Request the session identifier and use the phone number obtained from the user
@@ -36,15 +42,15 @@ const phoneOptions = {
3642
};
3743

3844
// Sends a text message to the user
39-
const verificationId = await auth().verifyPhoneNumberForMultiFactor(phoneOptions);
45+
const verificationId = await new PhoneAuthProvider(getAuth()).verifyPhoneNumber(phoneOptions);
4046
```
4147

4248
Once the user has provided the verification code received by text message, you
4349
can complete the process:
4450

4551
```js
46-
const cred = auth.PhoneAuthProvider.credential(verificationId, verificationCode);
47-
const multiFactorAssertion = auth.PhoneMultiFactorGenerator.assertion(cred);
52+
const cred = PhoneAuthProvider.credential(verificationId, verificationCode);
53+
const multiFactorAssertion = PhoneMultiFactorGenerator.assertion(cred);
4854
await multiFactorUser.enroll(multiFactorAssertion, 'Optional display name for the user');
4955
```
5056

@@ -58,10 +64,15 @@ default sign-in methods, for example email and password. If the account requires
5864
a second factor to complete login, an exception will be raised:
5965

6066
```js
61-
import auth from '@react-native-firebase/auth';
62-
63-
auth()
64-
.signInWithEmailAndPassword(email, password)
67+
import {
68+
PhoneAuthProvider,
69+
PhoneMultiFactorGenerator,
70+
getAuth,
71+
signInWithEmailAndPassword,
72+
getMultiFactorResolver,
73+
} from '@react-native-firebase/auth';
74+
75+
signInWithEmailAndPassword(getAuth(), email, password)
6576
.then(() => {
6677
// User has not enrolled a second factor
6778
})
@@ -81,7 +92,7 @@ Using the error object you can obtain a
8192
continue the flow:
8293

8394
```js
84-
const resolver = auth().getMultiFactorResolver(error);
95+
const resolver = getMultiFactorResolver(getAuth(), error);
8596
```
8697

8798
The resolver object has all the required information to prompt the user for a
@@ -93,7 +104,7 @@ if (resolver.hints.length > 1) {
93104
}
94105

95106
// Currently only phone based factors are supported
96-
if (resolver.hints[0].factorId === auth.PhoneMultiFactorGenerator.FACTOR_ID) {
107+
if (resolver.hints[0].factorId === PhoneMultiFactorGenerator.FACTOR_ID) {
97108
// Continue with the sign-in flow
98109
}
99110
```
@@ -105,18 +116,18 @@ verification code to the user:
105116
const hint = resolver.hints[0];
106117
const sessionId = resolver.session;
107118

108-
auth()
109-
.verifyPhoneNumberWithMultiFactorInfo(hint, sessionId) // triggers the message to the user
119+
new PhoneAuthProvider(getAuth())
120+
.verifyPhoneNumber(hint, sessionId) // triggers the message to the user
110121
.then(verificationId => setVerificationId(verificationId));
111122
```
112123

113124
Once the user has entered the verification code you can create a multi-factor
114125
assertion and finish the flow:
115126

116127
```js
117-
const credential = auth.PhoneAuthProvider.credential(verificationId, verificationCode);
128+
const credential = PhoneAuthProvider.credential(verificationId, verificationCode);
118129

119-
const multiFactorAssertion = auth.PhoneMultiFactorGenerator.assertion(credential);
130+
const multiFactorAssertion = PhoneMultiFactorGenerator.assertion(credential);
120131

121132
resolver.resolveSignIn(multiFactorAssertion).then(userCredential => {
122133
// additionally onAuthStateChanged will be triggered as well
@@ -130,39 +141,42 @@ will trigger with the new authentication state of the user.
130141
To put the example together:
131142

132143
```js
133-
import auth from '@react-native-firebase/auth';
134-
135-
const authInstance = auth();
136-
137-
authInstance
138-
.signInWithEmailAndPassword(email, password)
144+
import {
145+
PhoneAuthProvider,
146+
PhoneMultiFactorGenerator,
147+
getAuth,
148+
signInWithEmailAndPassword,
149+
getMultiFactorResolver,
150+
} from '@react-native-firebase/auth';
151+
152+
signInWithEmailAndPassword(getAuth(), email, password)
139153
.then(() => {
140154
// User has not enrolled a second factor
141155
})
142156
.catch(error => {
143157
const { code } = error;
144158
// Make sure to check if multi factor authentication is required
145159
if (code === 'auth/multi-factor-auth-required') {
146-
const resolver = auth.getMultiFactorResolver(error);
160+
const resolver = getMultiFactorResolver(error);
147161

148162
if (resolver.hints.length > 1) {
149163
// Use resolver.hints to display a list of second factors to the user
150164
}
151165

152166
// Currently only phone based factors are supported
153-
if (resolver.hints[0].factorId === auth.PhoneMultiFactorGenerator.FACTOR_ID) {
167+
if (resolver.hints[0].factorId === PhoneMultiFactorGenerator.FACTOR_ID) {
154168
const hint = resolver.hints[0];
155169
const sessionId = resolver.session;
156170

157-
authInstance
158-
.verifyPhoneNumberWithMultiFactorInfo(hint, sessionId) // triggers the message to the user
171+
new PhoneAuthProvider(getAuth())
172+
.verifyPhoneNumber(hint, sessionId) // triggers the message to the user
159173
.then(verificationId => setVerificationId(verificationId));
160174

161175
// Request verificationCode from user
162176

163-
const credential = auth.PhoneAuthProvider.credential(verificationId, verificationCode);
177+
const credential = PhoneAuthProvider.credential(verificationId, verificationCode);
164178

165-
const multiFactorAssertion = auth.PhoneMultiFactorGenerator.assertion(credential);
179+
const multiFactorAssertion = PhoneMultiFactorGenerator.assertion(credential);
166180

167181
resolver.resolveSignIn(multiFactorAssertion).then(userCredential => {
168182
// additionally onAuthStateChanged will be triggered as well

docs/auth/oidc-auth.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Before you use `react-native-app-auth` you have to complete the setup in their [
4444
The example below demonstrates how you could setup such a flow within your own application:
4545

4646
```jsx
47-
import auth from '@react-native-firebase/auth';
47+
import { OIDCAuthProvider, getAuth, signInWithCredential } from '@react-native-firebase/auth';
4848
import { authorize } from 'react-native-app-auth';
4949

5050
// using react-native-app-auth to get oauth token from Azure AD
@@ -59,10 +59,10 @@ const config = {
5959
// Log in to get an authentication token
6060
const authState = await authorize(config);
6161

62-
const credential = auth.OIDCAuthProvider.credential(
62+
const credential = OIDCAuthProvider.credential(
6363
'azure_test', // this is the "Provider ID" value from the firebase console
6464
authState.idToken,
6565
);
6666

67-
await auth().signInWithCredential(credential);
67+
await signInWithCredential(getAuth(), credential);
6868
```

docs/auth/phone-auth.md

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ 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';
61-
import auth from '@react-native-firebase/auth';
61+
import { getAuth, onAuthStateChanged, signInWithPhoneNumber } from '@react-native-firebase/auth';
6262

6363
function PhoneSignIn() {
6464
// If null, no SMS has been sent
@@ -68,7 +68,7 @@ function PhoneSignIn() {
6868
const [code, setCode] = useState('');
6969

7070
// Handle login
71-
function onAuthStateChanged(user) {
71+
function handleAuthStateChanged(user) {
7272
if (user) {
7373
// Some Android devices can automatically process the verification code (OTP) message, and the user would NOT need to enter the code.
7474
// Actually, if he/she tries to enter it, he/she will get an error message because the code was already used in the background.
@@ -78,13 +78,13 @@ function PhoneSignIn() {
7878
}
7979

8080
useEffect(() => {
81-
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
81+
const subscriber = onAuthStateChanged(getAuth(), handleAuthStateChanged);
8282
return subscriber; // unsubscribe on unmount
8383
}, []);
8484

8585
// Handle the button press
86-
async function signInWithPhoneNumber(phoneNumber) {
87-
const confirmation = await auth().signInWithPhoneNumber(phoneNumber);
86+
async function handleSignInWithPhoneNumber(phoneNumber) {
87+
const confirmation = await signInWithPhoneNumber(getAuth(), phoneNumber);
8888
setConfirm(confirmation);
8989
}
9090

@@ -100,7 +100,7 @@ function PhoneSignIn() {
100100
return (
101101
<Button
102102
title="Phone Number Sign In"
103-
onPress={() => signInWithPhoneNumber('+1 650-555-3434')}
103+
onPress={() => handleSignInWithPhoneNumber('+1 650-555-3434')}
104104
/>
105105
);
106106
}
@@ -138,7 +138,13 @@ After successfully creating a user with an email and password (see Authenticatio
138138
```jsx
139139
import React, { useState, useEffect } from 'react';
140140
import { Button, TextInput, Text } from 'react-native';
141-
import auth from '@react-native-firebase/auth';
141+
import {
142+
PhoneAuthProvider,
143+
getAuth,
144+
onAuthStateChanged,
145+
createUserWithEmailAndPassword,
146+
verifyPhoneNumber,
147+
} from '@react-native-firebase/auth';
142148

143149
export default function PhoneVerification() {
144150
// Set an initializing state whilst Firebase connects
@@ -151,20 +157,24 @@ export default function PhoneVerification() {
151157
const [code, setCode] = useState('');
152158

153159
// Handle user state changes
154-
function onAuthStateChanged(user) {
160+
function handleAuthStateChanged(user) {
155161
setUser(user);
156162
if (initializing) setInitializing(false);
157163
}
158164

159165
useEffect(() => {
160-
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
166+
const subscriber = onAuthStateChanged(getAuth(), handleAuthStateChanged);
161167
return subscriber; // unsubscribe on unmount
162168
}, []);
163169

164170
// Handle create account button press
165171
async function createAccount() {
166172
try {
167-
await auth().createUserWithEmailAndPassword('jane.doe@example.com', 'SuperSecretPassword!');
173+
await createUserWithEmailAndPassword(
174+
getAuth(),
175+
'jane.doe@example.com',
176+
'SuperSecretPassword!',
177+
);
168178
console.log('User account created & signed in!');
169179
} catch (error) {
170180
if (error.code === 'auth/email-already-in-use') {
@@ -179,16 +189,16 @@ export default function PhoneVerification() {
179189
}
180190

181191
// Handle the verify phone button press
182-
async function verifyPhoneNumber(phoneNumber) {
183-
const confirmation = await auth().verifyPhoneNumber(phoneNumber);
192+
async function handlePhoneNumberVerification(phoneNumber) {
193+
const confirmation = await verifyPhoneNumber(getAuth(), phoneNumber);
184194
setConfirm(confirmation);
185195
}
186196

187197
// Handle confirm code button press
188198
async function confirmCode() {
189199
try {
190-
const credential = auth.PhoneAuthProvider.credential(confirm.verificationId, code);
191-
let userData = await auth().currentUser.linkWithCredential(credential);
200+
const credential = PhoneAuthProvider.credential(confirm.verificationId, code);
201+
let userData = await getAuth().currentUser.linkWithCredential(credential);
192202
setUser(userData.user);
193203
} catch (error) {
194204
if (error.code == 'auth/invalid-verification-code') {
@@ -208,7 +218,9 @@ export default function PhoneVerification() {
208218
return (
209219
<Button
210220
title="Verify Phone Number"
211-
onPress={() => verifyPhoneNumber('ENTER A VALID TESTING OR REAL PHONE NUMBER HERE')}
221+
onPress={() =>
222+
handlePhoneNumberVerification('ENTER A VALID TESTING OR REAL PHONE NUMBER HERE')
223+
}
212224
/>
213225
);
214226
}

0 commit comments

Comments
 (0)