Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): include phoneNumber from PhoneMultiFactorInfo #7565

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@
final Promise promise) {
final MultiFactorSession multiFactorSession = mMultiFactorSessions.get(sessionKey);
if (multiFactorSession == null) {
rejectPromiseWithCodeAndMessage(promise, "unknown", "can't find session for provided key");
rejectPromiseWithCodeAndMessage(promise, "invalid-multi-factor-session", "can't find session for provided key");

Check warning on line 1191 in packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java

View check run for this annotation

Codecov / codecov/patch

packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java#L1191

Added line #L1191 was not covered by tests
return;
}
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
Expand Down Expand Up @@ -2553,6 +2553,10 @@
hintMap.putString("factorId", hint.getFactorId());
hintMap.putString("uid", hint.getUid());

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

Check warning on line 2557 in packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java

View check run for this annotation

Codecov / codecov/patch

packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java#L2557

Added line #L2557 was not covered by tests
}

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('verified@example.com');
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
61 changes: 41 additions & 20 deletions packages/auth/ios/RNFBAuth/RNFBAuthModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,9 @@ - (void)invalidate {
@"has expired or is not currently supported.",
}];
}
DLog(@"using app SignInWithCredential: %@", firebaseApp.name)[[FIRAuth authWithApp:firebaseApp]
DLog(@"using app SignInWithCredential: %@", firebaseApp.name);

[[FIRAuth authWithApp:firebaseApp]
signInWithCredential:credential
completion:^(FIRAuthDataResult *authResult, NSError *error) {
if (error) {
Expand Down Expand Up @@ -820,8 +822,9 @@ - (void)invalidate {
: (NSString *)phoneNumber
: (RCTPromiseResolveBlock)resolve
: (RCTPromiseRejectBlock)reject) {
DLog(@"SignInWthPhoneNumber instance: %@",
firebaseApp.name)[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
DLog(@"SignInWthPhoneNumber instance: %@", firebaseApp.name);

[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
verifyPhoneNumber:phoneNumber
UIDelegate:nil
completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
Expand Down Expand Up @@ -862,8 +865,9 @@ - (void)invalidate {
}];
return;
}
DLog(@"using instance verifyPhoneNumberWithMultiFactorInfo: %@",
firebaseApp.name)[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
DLog(@"using instance verifyPhoneNumberWithMultiFactorInfo: %@", firebaseApp.name);

[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
verifyPhoneNumberWithMultiFactorInfo:hint
UIDelegate:nil
multiFactorSession:session
Expand All @@ -884,9 +888,19 @@ - (void)invalidate {
: (NSString *)sessionId
: (RCTPromiseResolveBlock)resolve
: (RCTPromiseRejectBlock)reject) {
DLog(@"verifyPhoneNumberForMultifactor using app: %@", firebaseApp.name);
DLog(@"verifyPhoneNumberForMultifactor phoneNumber: %@", phoneNumber);
DLog(@"verifyPhoneNumberForMultifactor sessionId: %@", sessionId);
FIRMultiFactorSession *session = cachedSessions[sessionId];
DLog(@"using instance VerifyPhoneNumberForMultifactor: %@",
firebaseApp.name)[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
if (session == nil) {
[RNFBSharedUtils rejectPromiseWithUserInfo:reject
userInfo:(NSMutableDictionary *)@{
@"code" : @"invalid-multi-factor-session",
@"message" : @"can't find session for provided key"
}];
return;
}
[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
verifyPhoneNumber:phoneNumber
UIDelegate:nil
multiFactorSession:session
Expand All @@ -907,12 +921,15 @@ - (void)invalidate {
: (NSString *)verificationCode
: (RCTPromiseResolveBlock)resolve
: (RCTPromiseRejectBlock)reject) {
DLog(@"using instance resolve MultiFactorSignIn: %@", firebaseApp.name)
FIRPhoneAuthCredential *credential =
[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
credentialWithVerificationID:verificationId
verificationCode:verificationCode];
DLog(@"credential: %@", credential) FIRMultiFactorAssertion *assertion =
DLog(@"using instance resolve MultiFactorSignIn: %@", firebaseApp.name);

FIRPhoneAuthCredential *credential =
[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
credentialWithVerificationID:verificationId
verificationCode:verificationCode];
DLog(@"credential: %@", credential);

FIRMultiFactorAssertion *assertion =
[FIRPhoneMultiFactorGenerator assertionWithCredential:credential];

[cachedResolver[sessionKey] resolveSignInWithAssertion:assertion
Expand Down Expand Up @@ -955,11 +972,12 @@ - (void)invalidate {
: (NSString *_Nullable)displayName
: (RCTPromiseResolveBlock)resolve
: (RCTPromiseRejectBlock)reject) {
DLog(@"using instance finalizeMultifactorEnrollment: %@", firebaseApp.name)
FIRPhoneAuthCredential *credential =
[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
credentialWithVerificationID:verificationId
verificationCode:verificationCode];
DLog(@"using instance finalizeMultifactorEnrollment: %@", firebaseApp.name);

FIRPhoneAuthCredential *credential =
[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
credentialWithVerificationID:verificationId
verificationCode:verificationCode];
FIRMultiFactorAssertion *assertion =
[FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
FIRUser *user = [FIRAuth authWithApp:firebaseApp].currentUser;
Expand All @@ -980,8 +998,9 @@ - (void)invalidate {
: (FIRApp *)firebaseApp
: (NSString *)phoneNumber
: (NSString *)requestKey) {
DLog(@"using instance verifyPhoneNumber: %@",
firebaseApp.name)[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
DLog(@"using instance verifyPhoneNumber: %@", firebaseApp.name);

[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]]
verifyPhoneNumber:phoneNumber
UIDelegate:nil
completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) {
Expand Down Expand Up @@ -1695,6 +1714,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;
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
}

export interface TotpMultiFactorInfo extends MultiFactorInfoCommon {
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
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
Loading