Skip to content

Commit

Permalink
fix(auth, multifactor): put multiFactor and getMultiFactorResolver on…
Browse files Browse the repository at this point in the history
… auth()

this resolves some fidelity issues with the firebase-js-sdk API

this is non-breaking, the previous style from original implementation is still valid,
but it will emit a warning now. Types and docs are updated

Co-authored-by: Ferenc Züllich <fzuellich@users.noreply.github.com>
  • Loading branch information
mikehardy and fzuellich committed Nov 14, 2022
1 parent c4b2502 commit 357094e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 23 deletions.
6 changes: 3 additions & 3 deletions docs/auth/multi-factor-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ operations:

```js
import auth from '@react-native-firebase/auth';
const multiFactorUser = await auth.multiFactor(auth());
const multiFactorUser = await auth().multiFactor(auth().currentUser);
```

Request the session identifier and use the phone number obtained from the user
Expand Down Expand Up @@ -81,7 +81,7 @@ Using the error object you can obtain a
continue the flow:

```js
const resolver = auth.getMultiFactorResolver(auth(), error);
const resolver = auth().getMultiFactorResolver(error);
```

The resolver object has all the required information to prompt the user for a
Expand Down Expand Up @@ -143,7 +143,7 @@ authInstance
const { code } = error;
// Make sure to check if multi factor authentication is required
if (code !== 'auth/multi-factor-auth-required') {
const resolver = auth.getMultiFactorResolver(authInstance, error);
const resolver = auth.getMultiFactorResolver(error);
if (resolver.hints.length > 1) {
// Use resolver.hints to display a list of second factors to the user
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ exports.createUserWithMultiFactor = async function createUserWithMultiFactor() {
const email = 'verified@example.com';
const password = 'test123';
await createVerifiedUser(email, password);
const multiFactorUser = await firebase.auth.multiFactor(firebase.auth());
const multiFactorUser = await firebase.auth().multiFactor(firebase.auth().currentUser);
const session = await multiFactorUser.getSession();
const phoneNumber = getRandomPhoneNumber();
const verificationId = await firebase
Expand Down Expand Up @@ -242,7 +242,7 @@ exports.signInUserWithMultiFactor = async function signInUserWithMultiFactor(
e.message.should.equal(
'[auth/multi-factor-auth-required] Please complete a second factor challenge to finish signing into this account.',
);
const resolver = firebase.auth.getMultiFactorResolver(firebase.auth(), e);
const resolver = firebase.auth().getMultiFactorResolver(e);
let verificationId = await firebase
.auth()
.verifyPhoneNumberWithMultiFactorInfo(resolver.hints[0], resolver.session);
Expand Down
28 changes: 14 additions & 14 deletions packages/auth/e2e/multiFactor.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('multi-factor', function () {
e.message.should.equal(
'[auth/multi-factor-auth-required] Please complete a second factor challenge to finish signing into this account.',
);
const resolver = firebase.auth.getMultiFactorResolver(firebase.auth(), e);
const resolver = firebase.auth().getMultiFactorResolver(e);
resolver.should.be.an.Object();
resolver.hints.should.be.an.Array();
resolver.hints.length.should.equal(1);
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('multi-factor', function () {
e.message.should.equal(
'[auth/multi-factor-auth-required] Please complete a second factor challenge to finish signing into this account.',
);
const resolver = firebase.auth.getMultiFactorResolver(firebase.auth(), e);
const resolver = firebase.auth().getMultiFactorResolver(e);
const verificationId = await firebase
.auth()
.verifyPhoneNumberWithMultiFactorInfo(resolver.hints[0], resolver.session);
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('multi-factor', function () {
e.message.should.equal(
'[auth/multi-factor-auth-required] Please complete a second factor challenge to finish signing into this account.',
);
const resolver = firebase.auth.getMultiFactorResolver(firebase.auth(), e);
const resolver = firebase.auth().getMultiFactorResolver(e);
await firebase
.auth()
.verifyPhoneNumberWithMultiFactorInfo(resolver.hints[0], resolver.session);
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('multi-factor', function () {
e.message.should.equal(
'[auth/multi-factor-auth-required] Please complete a second factor challenge to finish signing into this account.',
);
const resolver = firebase.auth.getMultiFactorResolver(firebase.auth(), e);
const resolver = firebase.auth().getMultiFactorResolver(e);
const unknownFactor = {
uid: 'notknown',
};
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('multi-factor', function () {
await firebase.auth().signInWithEmailAndPassword(TEST_EMAIL, TEST_PASS);

try {
const multiFactorUser = await firebase.auth.multiFactor(firebase.auth());
const multiFactorUser = await firebase.auth().multiFactor(firebase.auth().currentUser);
const session = await multiFactorUser.getSession();
await firebase
.auth()
Expand All @@ -223,7 +223,7 @@ describe('multi-factor', function () {
const phoneNumber = getRandomPhoneNumber();

should.deepEqual(firebase.auth().currentUser.multiFactor.enrolledFactors, []);
const multiFactorUser = await firebase.auth.multiFactor(firebase.auth());
const multiFactorUser = await firebase.auth().multiFactor(firebase.auth().currentUser);

const session = await multiFactorUser.getSession();

Expand Down Expand Up @@ -256,7 +256,7 @@ describe('multi-factor', function () {
const phoneNumber = getRandomPhoneNumber();

should.deepEqual(firebase.auth().currentUser.multiFactor.enrolledFactors, []);
const multiFactorUser = await firebase.auth.multiFactor(firebase.auth());
const multiFactorUser = await firebase.auth().multiFactor(firebase.auth().currentUser);

const session = await multiFactorUser.getSession();

Expand Down Expand Up @@ -285,7 +285,7 @@ describe('multi-factor', function () {
await signInUserWithMultiFactor(email, password, phoneNumber);

const anotherNumber = getRandomPhoneNumber();
const multiFactorUser = await firebase.auth.multiFactor(firebase.auth());
const multiFactorUser = await firebase.auth().multiFactor(firebase.auth().currentUser);

const session = await multiFactorUser.getSession();
const verificationId = await firebase
Expand Down Expand Up @@ -318,7 +318,7 @@ describe('multi-factor', function () {
await clearAllUsers();
const { email, password, phoneNumber } = await createUserWithMultiFactor();
await signInUserWithMultiFactor(email, password, phoneNumber);
const multiFactorUser = await firebase.auth.multiFactor(firebase.auth());
const multiFactorUser = await firebase.auth().multiFactor(firebase.auth().currentUser);
const session = await multiFactorUser.getSession();
const verificationId = await firebase
Expand Down Expand Up @@ -355,7 +355,7 @@ describe('multi-factor', function () {
e.message.should.equal(
'[auth/multi-factor-auth-required] Please complete a second factor challenge to finish signing into this account.',
);
resolver = firebase.auth.getMultiFactorResolver(firebase.auth(), e);
resolver = firebase.auth().getMultiFactorResolver(e);
}
await firebase
.auth()
Expand Down Expand Up @@ -392,7 +392,7 @@ describe('multi-factor', function () {
e.message.should.equal(
'[auth/multi-factor-auth-required] Please complete a second factor challenge to finish signing into this account.',
);
resolver = firebase.auth.getMultiFactorResolver(firebase.auth(), e);
resolver = firebase.auth().getMultiFactorResolver(e);
}

try {
Expand Down Expand Up @@ -420,7 +420,7 @@ describe('multi-factor', function () {
e.message.should.equal(
'[auth/multi-factor-auth-required] Please complete a second factor challenge to finish signing into this account.',
);
resolver = firebase.auth.getMultiFactorResolver(firebase.auth(), e);
resolver = firebase.auth().getMultiFactorResolver(e);
}
const verificationId = await firebase
.auth()
Expand Down Expand Up @@ -461,7 +461,7 @@ describe('multi-factor', function () {
await confirmResult.confirm(lastSmsCode);

// WHEN they attempt to enroll a second factor
const multiFactorUser = await firebase.auth.multiFactor(firebase.auth());
const multiFactorUser = await firebase.auth().multiFactor(firebase.auth().currentUser);
const session = await multiFactorUser.getSession();
try {
await firebase.auth().verifyPhoneNumberForMultiFactor({ phoneNumber: '+1123123', session });
Expand All @@ -477,7 +477,7 @@ describe('multi-factor', function () {
});
it('can not enroll when phone number is missing + sign', async function () {
await createVerifiedUser('verified@example.com', 'test123');
const multiFactorUser = firebase.auth.multiFactor(firebase.auth());
const multiFactorUser = firebase.auth().multiFactor(firebase.auth().currentUser);
const session = await multiFactorUser.getSession();
try {
await firebase.auth().verifyPhoneNumberForMultiFactor({ phoneNumber: '491575', session });
Expand Down
13 changes: 13 additions & 0 deletions packages/auth/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,19 @@ export namespace FirebaseAuthTypes {
* @param url: emulator URL, must have host and port (eg, 'http://localhost:9099')
*/
useEmulator(url: string): void;
/**
* Provides a MultiFactorResolver suitable for completion of a multi-factor flow.
*
* @param error: The MultiFactorError raised during a sign-in, or reauthentication operation.
*/
getMultiFactorResolver(error: MultiFactorError): MultiFactorResolver;
/**
* The MultiFactorUser corresponding to the user.
*
* This is used to access all multi-factor properties and operations related to the user.
* @param user The user.
*/
multiFactor(user: User): MultiFactorUser;
}
}

Expand Down
13 changes: 12 additions & 1 deletion packages/auth/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import Settings from './Settings';
import User from './User';
import version from './version';
import { getMultiFactorResolver } from './getMultiFactorResolver';
import { multiFactor } from './multiFactor';
import { multiFactor, MultiFactorUser } from './multiFactor';

const statics = {
AppleAuthProvider,
Expand Down Expand Up @@ -414,6 +414,17 @@ class FirebaseAuthModule extends FirebaseModule {
this.native.useEmulator(host, port);
return [host, port]; // undocumented return, useful for unit testing
}

getMultiFactorResolver(error) {
return getMultiFactorResolver(this, error);
}

multiFactor(user) {
if (user.userId !== this.currentUser.userId) {
throw new Error('firebase.auth().multiFactor() only operates on currentUser');
}
return new MultiFactorUser(this, user);
}
}

// import { SDK_VERSION } from '@react-native-firebase/auth';
Expand Down
11 changes: 8 additions & 3 deletions packages/auth/lib/multiFactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
* Return a MultiFactorUser instance the gateway to multi-factor operations.
*/
export function multiFactor(auth) {
// eslint-disable-next-line no-console
console.warn('This method is deprecated. Please use auth().multiFactor(user) instead');
return new MultiFactorUser(auth);
}

export class MultiFactorUser {
constructor(auth) {
constructor(auth, user) {
this._auth = auth;
this._user = auth.currentUser;
this.enrolledFactor = auth.currentUser.multiFactor.enrolledFactors;
if (user === undefined) {
user = auth.currentUser;
}
this._user = user;
this.enrolledFactor = user.multiFactor.enrolledFactors;
}

getSession() {
Expand Down

1 comment on commit 357094e

@vercel
Copy link

@vercel vercel bot commented on 357094e Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.