Skip to content

Commit e1ffd61

Browse files
committed
docs(auth): update examples to use modular API
1 parent 2308183 commit e1ffd61

File tree

5 files changed

+116
-77
lines changed

5 files changed

+116
-77
lines changed

Diff for: docs/auth/multi-factor-auth.md

+34-22
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ 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 authModule, { PhoneAuthProvider, getAuth, multiFactor } from '@react-native-firebase/auth';
25+
26+
const auth = getAuth();
27+
const multiFactorUser = await multiFactor(auth.currentUser);
2628
```
2729

2830
Request the session identifier and use the phone number obtained from the user
@@ -36,15 +38,15 @@ const phoneOptions = {
3638
};
3739

3840
// Sends a text message to the user
39-
const verificationId = await auth().verifyPhoneNumberForMultiFactor(phoneOptions);
41+
const verificationId = await auth.verifyPhoneNumberForMultiFactor(phoneOptions);
4042
```
4143

4244
Once the user has provided the verification code received by text message, you
4345
can complete the process:
4446

4547
```js
46-
const cred = auth.PhoneAuthProvider.credential(verificationId, verificationCode);
47-
const multiFactorAssertion = auth.PhoneMultiFactorGenerator.assertion(cred);
48+
const cred = PhoneAuthProvider.credential(verificationId, verificationCode);
49+
const multiFactorAssertion = authModule.PhoneMultiFactorGenerator.assertion(cred);
4850
await multiFactorUser.enroll(multiFactorAssertion, 'Optional display name for the user');
4951
```
5052

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

6062
```js
61-
import auth from '@react-native-firebase/auth';
63+
import authModule, {
64+
PhoneAuthProvider,
65+
getAuth,
66+
signInWithEmailAndPassword,
67+
getMultiFactorResolver,
68+
} from '@react-native-firebase/auth';
69+
70+
const auth = getAuth();
6271

63-
auth()
64-
.signInWithEmailAndPassword(email, password)
72+
signInWithEmailAndPassword(auth, email, password)
6573
.then(() => {
6674
// User has not enrolled a second factor
6775
})
@@ -81,7 +89,7 @@ Using the error object you can obtain a
8189
continue the flow:
8290

8391
```js
84-
const resolver = auth().getMultiFactorResolver(error);
92+
const resolver = getMultiFactorResolver(auth, error);
8593
```
8694

8795
The resolver object has all the required information to prompt the user for a
@@ -93,7 +101,7 @@ if (resolver.hints.length > 1) {
93101
}
94102

95103
// Currently only phone based factors are supported
96-
if (resolver.hints[0].factorId === auth.PhoneMultiFactorGenerator.FACTOR_ID) {
104+
if (resolver.hints[0].factorId === authModule.PhoneMultiFactorGenerator.FACTOR_ID) {
97105
// Continue with the sign-in flow
98106
}
99107
```
@@ -105,7 +113,7 @@ verification code to the user:
105113
const hint = resolver.hints[0];
106114
const sessionId = resolver.session;
107115

108-
auth()
116+
auth
109117
.verifyPhoneNumberWithMultiFactorInfo(hint, sessionId) // triggers the message to the user
110118
.then(verificationId => setVerificationId(verificationId));
111119
```
@@ -114,9 +122,9 @@ Once the user has entered the verification code you can create a multi-factor
114122
assertion and finish the flow:
115123

116124
```js
117-
const credential = auth.PhoneAuthProvider.credential(verificationId, verificationCode);
125+
const credential = PhoneAuthProvider.credential(verificationId, verificationCode);
118126

119-
const multiFactorAssertion = auth.PhoneMultiFactorGenerator.assertion(credential);
127+
const multiFactorAssertion = authModule.PhoneMultiFactorGenerator.assertion(credential);
120128

121129
resolver.resolveSignIn(multiFactorAssertion).then(userCredential => {
122130
// additionally onAuthStateChanged will be triggered as well
@@ -130,39 +138,43 @@ will trigger with the new authentication state of the user.
130138
To put the example together:
131139

132140
```js
133-
import auth from '@react-native-firebase/auth';
141+
import authModule, {
142+
PhoneAuthProvider,
143+
getAuth,
144+
signInWithEmailAndPassword,
145+
getMultiFactorResolver,
146+
} from '@react-native-firebase/auth';
134147

135-
const authInstance = auth();
148+
const auth = getAuth();
136149

137-
authInstance
138-
.signInWithEmailAndPassword(email, password)
150+
signInWithEmailAndPassword(auth, email, password)
139151
.then(() => {
140152
// User has not enrolled a second factor
141153
})
142154
.catch(error => {
143155
const { code } = error;
144156
// Make sure to check if multi factor authentication is required
145157
if (code === 'auth/multi-factor-auth-required') {
146-
const resolver = auth.getMultiFactorResolver(error);
158+
const resolver = getMultiFactorResolver(error);
147159

148160
if (resolver.hints.length > 1) {
149161
// Use resolver.hints to display a list of second factors to the user
150162
}
151163

152164
// Currently only phone based factors are supported
153-
if (resolver.hints[0].factorId === auth.PhoneMultiFactorGenerator.FACTOR_ID) {
165+
if (resolver.hints[0].factorId === authModule.PhoneMultiFactorGenerator.FACTOR_ID) {
154166
const hint = resolver.hints[0];
155167
const sessionId = resolver.session;
156168

157-
authInstance
169+
auth
158170
.verifyPhoneNumberWithMultiFactorInfo(hint, sessionId) // triggers the message to the user
159171
.then(verificationId => setVerificationId(verificationId));
160172

161173
// Request verificationCode from user
162174

163-
const credential = auth.PhoneAuthProvider.credential(verificationId, verificationCode);
175+
const credential = PhoneAuthProvider.credential(verificationId, verificationCode);
164176

165-
const multiFactorAssertion = auth.PhoneMultiFactorGenerator.assertion(credential);
177+
const multiFactorAssertion = authModule.PhoneMultiFactorGenerator.assertion(credential);
166178

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

Diff for: docs/auth/oidc-auth.md

+4-3
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,11 @@ 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+
const auth = getAuth();
68+
await signInWithCredential(auth, credential);
6869
```

Diff for: docs/auth/phone-auth.md

+27-15
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ The example below demonstrates how you could setup such a flow within your own a
5858
```jsx
5959
import React, { 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';
62+
63+
const auth = getAuth();
6264

6365
function PhoneSignIn() {
6466
// If null, no SMS has been sent
@@ -68,7 +70,7 @@ function PhoneSignIn() {
6870
const [code, setCode] = useState('');
6971

7072
// Handle login
71-
function onAuthStateChanged(user) {
73+
function handleAuthStateChanged(user) {
7274
if (user) {
7375
// Some Android devices can automatically process the verification code (OTP) message, and the user would NOT need to enter the code.
7476
// 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 +80,13 @@ function PhoneSignIn() {
7880
}
7981

8082
useEffect(() => {
81-
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
83+
const subscriber = onAuthStateChanged(auth, handleAuthStateChanged);
8284
return subscriber; // unsubscribe on unmount
8385
}, []);
8486

8587
// Handle the button press
86-
async function signInWithPhoneNumber(phoneNumber) {
87-
const confirmation = await auth().signInWithPhoneNumber(phoneNumber);
88+
async function handleSignInWithPhoneNumber(phoneNumber) {
89+
const confirmation = await signInWithPhoneNumber(auth, phoneNumber);
8890
setConfirm(confirmation);
8991
}
9092

@@ -100,7 +102,7 @@ function PhoneSignIn() {
100102
return (
101103
<Button
102104
title="Phone Number Sign In"
103-
onPress={() => signInWithPhoneNumber('+1 650-555-3434')}
105+
onPress={() => handleSignInWithPhoneNumber('+1 650-555-3434')}
104106
/>
105107
);
106108
}
@@ -138,7 +140,15 @@ After successfully creating a user with an email and password (see Authenticatio
138140
```jsx
139141
import React, { useState, useEffect } from 'react';
140142
import { Button, TextInput, Text } from 'react-native';
141-
import auth from '@react-native-firebase/auth';
143+
import {
144+
PhoneAuthProvider,
145+
getAuth,
146+
onAuthStateChanged,
147+
createUserWithEmailAndPassword,
148+
verifyPhoneNumber,
149+
} from '@react-native-firebase/auth';
150+
151+
const auth = getAuth();
142152

143153
export default function PhoneVerification() {
144154
// Set an initializing state whilst Firebase connects
@@ -151,20 +161,20 @@ export default function PhoneVerification() {
151161
const [code, setCode] = useState('');
152162

153163
// Handle user state changes
154-
function onAuthStateChanged(user) {
164+
function handleAuthStateChanged(user) {
155165
setUser(user);
156166
if (initializing) setInitializing(false);
157167
}
158168

159169
useEffect(() => {
160-
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
170+
const subscriber = onAuthStateChanged(auth, handleAuthStateChanged);
161171
return subscriber; // unsubscribe on unmount
162172
}, []);
163173

164174
// Handle create account button press
165175
async function createAccount() {
166176
try {
167-
await auth().createUserWithEmailAndPassword('[email protected]', 'SuperSecretPassword!');
177+
await createUserWithEmailAndPassword(auth, '[email protected]', 'SuperSecretPassword!');
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(auth, 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 auth.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)