Skip to content

Commit 099ace3

Browse files
feat(mfa): Refactor MFA handling by introducing MfaBridge for better organization
1 parent 3ce9438 commit 099ace3

14 files changed

Lines changed: 481 additions & 473 deletions

File tree

EXAMPLES-WEB.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,12 @@ import { View, Button, TextInput, Text } from 'react-native';
170170
import { useAuth0, MfaError, MfaErrorCodes } from 'react-native-auth0';
171171

172172
function MfaScreen({ mfaToken }: { mfaToken: string }) {
173-
const { mfaGetAuthenticators, mfaEnroll, mfaChallenge, mfaVerify } =
174-
useAuth0();
173+
const { mfa } = useAuth0();
175174
const [otp, setOtp] = useState('');
176175

177176
const listAuthenticators = async () => {
178177
try {
179-
const authenticators = await mfaGetAuthenticators({ mfaToken });
178+
const authenticators = await mfa.getAuthenticators({ mfaToken });
180179
console.log('Authenticators:', authenticators);
181180
} catch (error) {
182181
if (error instanceof MfaError) {
@@ -187,7 +186,7 @@ function MfaScreen({ mfaToken }: { mfaToken: string }) {
187186

188187
const enrollTotp = async () => {
189188
try {
190-
const challenge = await mfaEnroll({ mfaToken, type: 'otp' });
189+
const challenge = await mfa.enroll({ mfaToken, type: 'otp' });
191190
if (challenge.type === 'totp') {
192191
console.log('Scan QR:', challenge.barcodeUri);
193192
console.log('Secret:', challenge.secret);
@@ -201,7 +200,7 @@ function MfaScreen({ mfaToken }: { mfaToken: string }) {
201200

202201
const verifyOtp = async () => {
203202
try {
204-
const credentials = await mfaVerify({ mfaToken, otp });
203+
const credentials = await mfa.verify({ mfaToken, otp });
205204
console.log('Authenticated!', credentials.accessToken);
206205
} catch (error) {
207206
if (error instanceof MfaError) {
@@ -241,33 +240,31 @@ const auth0 = new Auth0({
241240
clientId: 'YOUR_AUTH0_CLIENT_ID',
242241
});
243242

244-
const mfaClient = auth0.mfa();
245-
246243
// List authenticators
247-
const authenticators = await mfaClient.getAuthenticators({
244+
const authenticators = await auth0.mfa.getAuthenticators({
248245
mfaToken: 'mfa_token',
249246
});
250247

251248
// Enroll TOTP
252-
const challenge = await mfaClient.enroll({
249+
const challenge = await auth0.mfa.enroll({
253250
mfaToken: 'mfa_token',
254251
type: 'otp',
255252
});
256253

257254
// Enroll SMS
258-
const smsChallenge = await mfaClient.enroll({
255+
const smsChallenge = await auth0.mfa.enroll({
259256
mfaToken: 'mfa_token',
260257
phoneNumber: '+12025550135',
261258
});
262259

263260
// Challenge an authenticator
264-
const challengeResult = await mfaClient.challenge({
261+
const challengeResult = await auth0.mfa.challenge({
265262
mfaToken: 'mfa_token',
266263
authenticatorId: 'sms|dev_123',
267264
});
268265

269266
// Verify OTP
270-
const credentials = await mfaClient.verify({
267+
const credentials = await auth0.mfa.verify({
271268
mfaToken: 'mfa_token',
272269
otp: '123456',
273270
});

EXAMPLES.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,14 +1261,13 @@ import { View, Button, TextInput, Text, Alert } from 'react-native';
12611261
import { useAuth0, MfaError, MfaErrorCodes } from 'react-native-auth0';
12621262

12631263
function MfaScreen({ mfaToken }: { mfaToken: string }) {
1264-
const { mfaGetAuthenticators, mfaEnroll, mfaChallenge, mfaVerify } =
1265-
useAuth0();
1264+
const { mfa } = useAuth0();
12661265
const [otp, setOtp] = useState('');
12671266

12681267
// List enrolled authenticators
12691268
const listAuthenticators = async () => {
12701269
try {
1271-
const authenticators = await mfaGetAuthenticators({ mfaToken });
1270+
const authenticators = await mfa.getAuthenticators({ mfaToken });
12721271
console.log('Enrolled authenticators:', authenticators);
12731272
} catch (error) {
12741273
if (error instanceof MfaError) {
@@ -1280,7 +1279,7 @@ function MfaScreen({ mfaToken }: { mfaToken: string }) {
12801279
// Enroll a new TOTP authenticator
12811280
const enrollTotp = async () => {
12821281
try {
1283-
const challenge = await mfaEnroll({ mfaToken, type: 'otp' });
1282+
const challenge = await mfa.enroll({ mfaToken, type: 'otp' });
12841283
if (challenge.type === 'totp') {
12851284
console.log('Scan this barcode:', challenge.barcodeUri);
12861285
console.log('Or enter this secret:', challenge.secret);
@@ -1302,7 +1301,7 @@ function MfaScreen({ mfaToken }: { mfaToken: string }) {
13021301
// Enroll an SMS factor
13031302
const enrollSms = async () => {
13041303
try {
1305-
const challenge = await mfaEnroll({
1304+
const challenge = await mfa.enroll({
13061305
mfaToken,
13071306
phoneNumber: '+12025550135',
13081307
});
@@ -1320,7 +1319,7 @@ function MfaScreen({ mfaToken }: { mfaToken: string }) {
13201319
// Trigger a challenge for an existing authenticator
13211320
const triggerChallenge = async (authenticatorId: string) => {
13221321
try {
1323-
const result = await mfaChallenge({ mfaToken, authenticatorId });
1322+
const result = await mfa.challenge({ mfaToken, authenticatorId });
13241323
console.log('Challenge type:', result.challengeType);
13251324
console.log('OOB code:', result.oobCode);
13261325
} catch (error) {
@@ -1333,7 +1332,7 @@ function MfaScreen({ mfaToken }: { mfaToken: string }) {
13331332
// Verify an OTP code - this completes authentication
13341333
const verifyOtp = async () => {
13351334
try {
1336-
const credentials = await mfaVerify({ mfaToken, otp });
1335+
const credentials = await mfa.verify({ mfaToken, otp });
13371336
console.log('Authentication complete!', credentials.accessToken);
13381337
// User is now logged in - state is automatically updated
13391338
} catch (error) {
@@ -1380,16 +1379,14 @@ const auth0 = new Auth0({
13801379
clientId: 'YOUR_AUTH0_CLIENT_ID',
13811380
});
13821381

1383-
const mfaClient = auth0.mfa();
1384-
13851382
// List enrolled authenticators
1386-
const authenticators = await mfaClient.getAuthenticators({
1383+
const authenticators = await auth0.mfa.getAuthenticators({
13871384
mfaToken: 'mfa_token_from_login',
13881385
factorsAllowed: ['otp', 'oob'], // Optional: filter by factor type
13891386
});
13901387

13911388
// Enroll a TOTP authenticator
1392-
const totpChallenge = await mfaClient.enroll({
1389+
const totpChallenge = await auth0.mfa.enroll({
13931390
mfaToken: 'mfa_token',
13941391
type: 'otp',
13951392
});
@@ -1398,45 +1395,45 @@ const totpChallenge = await mfaClient.enroll({
13981395
// totpChallenge.secret - Manual entry secret
13991396

14001397
// Enroll via SMS
1401-
const smsChallenge = await mfaClient.enroll({
1398+
const smsChallenge = await auth0.mfa.enroll({
14021399
mfaToken: 'mfa_token',
14031400
phoneNumber: '+12025550135',
14041401
});
14051402

14061403
// Enroll via email
1407-
const emailChallenge = await mfaClient.enroll({
1404+
const emailChallenge = await auth0.mfa.enroll({
14081405
mfaToken: 'mfa_token',
14091406
email: 'user@example.com',
14101407
});
14111408

14121409
// Enroll via voice call
1413-
const voiceChallenge = await mfaClient.enroll({
1410+
const voiceChallenge = await auth0.mfa.enroll({
14141411
mfaToken: 'mfa_token',
14151412
phoneNumber: '+12025550135',
14161413
voice: true,
14171414
});
14181415

14191416
// Trigger an OOB challenge
1420-
const challenge = await mfaClient.challenge({
1417+
const challenge = await auth0.mfa.challenge({
14211418
mfaToken: 'mfa_token',
14221419
authenticatorId: 'sms|dev_123',
14231420
});
14241421

14251422
// Verify with OTP
1426-
const credentials = await mfaClient.verify({
1423+
const credentials = await auth0.mfa.verify({
14271424
mfaToken: 'mfa_token',
14281425
otp: '123456',
14291426
});
14301427

14311428
// Verify with OOB code
1432-
const credentialsOob = await mfaClient.verify({
1429+
const credentialsOob = await auth0.mfa.verify({
14331430
mfaToken: 'mfa_token',
14341431
oobCode: 'oob_code_from_challenge',
14351432
bindingCode: '654321', // Optional, for SMS/email OOB
14361433
});
14371434

14381435
// Verify with recovery code
1439-
const credentialsRecovery = await mfaClient.verify({
1436+
const credentialsRecovery = await auth0.mfa.verify({
14401437
mfaToken: 'mfa_token',
14411438
recoveryCode: 'ABCDEF123456',
14421439
});
@@ -1450,7 +1447,7 @@ All MFA operations throw `MfaError` with a normalized, platform-agnostic `type`
14501447
import { MfaError, MfaErrorCodes } from 'react-native-auth0';
14511448

14521449
try {
1453-
await auth0.mfa().verify({ mfaToken, otp: '123456' });
1450+
await auth0.mfa.verify({ mfaToken, otp: '123456' });
14541451
} catch (error) {
14551452
if (error instanceof MfaError) {
14561453
switch (error.type) {

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -714,13 +714,13 @@ try {
714714

715715
### MFA errors
716716

717-
All MFA operations (via `auth0.mfa()` or the `mfaGetAuthenticators`, `mfaEnroll`, `mfaChallenge`, `mfaVerify` hooks) throw `MfaError` with a normalized `type` property:
717+
All MFA operations (via `auth0.mfa` or the `mfa` property from `useAuth0()`) throw `MfaError` with a normalized `type` property:
718718

719719
```js
720720
import { MfaError, MfaErrorCodes } from 'react-native-auth0';
721721

722722
try {
723-
const credentials = await auth0.mfa().verify({ mfaToken, otp: '123456' });
723+
const credentials = await auth0.mfa.verify({ mfaToken, otp: '123456' });
724724
} catch (error) {
725725
if (error instanceof MfaError) {
726726
switch (error.type) {
@@ -878,10 +878,10 @@ This library provides a unified API across Native (iOS/Android) and Web platform
878878
| `auth.resetPassword()` ||| Calls the `/dbconnections/change_password` endpoint. Works on both platforms. |
879879
| `users(token).patchUser()` ||| Calls the Management API. Works on any platform with a valid token, but use with caution in the browser. |
880880
| **MFA Flexible Factors Grant** | | | --- |
881-
| `mfa().getAuthenticators()` ||| Lists enrolled MFA authenticators for the user. |
882-
| `mfa().enroll()` ||| Enrolls a new MFA factor (OTP, SMS, email, voice, push). |
883-
| `mfa().challenge()` ||| Triggers an MFA challenge for an enrolled authenticator. |
884-
| `mfa().verify()` ||| Verifies an MFA code (OTP, OOB, recovery) and returns credentials. |
881+
| `mfa.getAuthenticators()` ||| Lists enrolled MFA authenticators for the user. |
882+
| `mfa.enroll()` ||| Enrolls a new MFA factor (OTP, SMS, email, voice, push). |
883+
| `mfa.challenge()` ||| Triggers an MFA challenge for an enrolled authenticator. |
884+
| `mfa.verify()` ||| Verifies an MFA code (OTP, OOB, recovery) and returns credentials. |
885885

886886
## Troubleshooting
887887

0 commit comments

Comments
 (0)