Skip to content

Commit

Permalink
feat: include phoneNumber from PhoneMultiFactorInfo
Browse files Browse the repository at this point in the history
this is useful for indicating to users which phone number they have
enrolled, and distinguishing between factors if there are multiple
phone factors enrolled.
  • Loading branch information
mnahkies authored and mikehardy committed May 20, 2024
1 parent a6805bc commit 5acdfb3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,10 @@ private WritableMap multiFactorInfoToMap(MultiFactorInfo hint) {
hintMap.putString("factorId", hint.getFactorId());
hintMap.putString("uid", hint.getUid());

if (hint.getFactorId().equals(PhoneMultiFactorGenerator.FACTOR_ID)) {
hintMap.putString("phoneNumber", ((PhoneMultiFactorInfo) hint).getPhoneNumber());
}

return hintMap;
}

Expand Down
7 changes: 6 additions & 1 deletion packages/auth/e2e/multiFactor.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ describe('multi-factor modular', function () {
this.skip();
}
const { phoneNumber, email, password } = await createUserWithMultiFactor();
const maskedNumber = '+********' + phoneNumber.substring(phoneNumber.length - 4);

const { signInWithEmailAndPassword, getAuth, getMultiFactorResolver } = authModular;

Expand All @@ -562,6 +563,9 @@ describe('multi-factor modular', function () {
multiFactorResolver.should.be.an.Object();
multiFactorResolver.hints.should.be.an.Array();
multiFactorResolver.hints.length.should.equal(1);
multiFactorResolver.hints[0].factorId.should.equal('phone');
multiFactorResolver.hints[0].phoneNumber.should.equal(maskedNumber);

multiFactorResolver.session.should.be.a.String();

const verificationId = await new firebase.auth.PhoneAuthProvider(
Expand All @@ -575,7 +579,6 @@ describe('multi-factor modular', function () {
let verificationCode = await getLastSmsCode(phoneNumber);
if (verificationCode == null) {
// iOS simulator uses a masked phone number
const maskedNumber = '+********' + phoneNumber.substring(phoneNumber.length - 4);
verificationCode = await getLastSmsCode(maskedNumber);
}
const phoneAuthCredential = new firebase.auth.PhoneAuthProvider.credential(
Expand All @@ -591,6 +594,8 @@ describe('multi-factor modular', function () {
user.email.should.equal('[email protected]');
user.multiFactor.should.be.an.Object();
user.multiFactor.enrolledFactors.length.should.equal(1);
user.multiFactor.enrolledFactors[0].factorId.should.equal('phone');
user.multiFactor.enrolledFactors[0].phoneNumber.should.equal(phoneNumber);
return Promise.resolve();
})
.catch(e => {
Expand Down
2 changes: 2 additions & 0 deletions packages/auth/ios/RNFBAuth/RNFBAuthModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,8 @@ - (NSDictionary *)firebaseUserToDict:(FIRUser *)user {
@"enrollmentTime" : enrollmentTime,
// @deprecated enrollmentDate kept for backwards compatibility, please use enrollmentTime
@"enrollmentDate" : enrollmentTime,
// phoneNumber only present on FIRPhoneMultiFactorInfo
@"phoneNumber" : hint.phoneNumber == nil ? [NSNull null] : hint.phoneNumber,
}];
}
return enrolledFactors;
Expand Down
20 changes: 15 additions & 5 deletions packages/auth/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,21 @@ export namespace FirebaseAuthTypes {
/**
* Contains information about a second factor.
*/
export interface MultiFactorInfo {
export type MultiFactorInfo = PhoneMultiFactorInfo | TotpMultiFactorInfo;

export interface PhoneMultiFactorInfo extends MultiFactorInfoCommon {
factorId: 'phone';
/**
* The phone number used for this factor.
*/
phoneNumber: string;
}

export interface TotpMultiFactorInfo extends MultiFactorInfoCommon {
factorId: 'totp';
}

export interface MultiFactorInfoCommon {
/**
* User friendly name for this factor.
*/
Expand All @@ -481,10 +495,6 @@ export namespace FirebaseAuthTypes {
* Time the second factor was enrolled, in UTC.
*/
enrollmentTime: string;
/**
* Type of factor.
*/
factorId: FactorId;
/**
* Unique id for this factor.
*/
Expand Down

0 comments on commit 5acdfb3

Please sign in to comment.