Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 948f4b3

Browse files
Merge pull request #484 from witi83/patch-2
Fix phone authentication
2 parents f35a16c + 4e42066 commit 948f4b3

File tree

1 file changed

+51
-55
lines changed

1 file changed

+51
-55
lines changed

firebase.android.js

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -999,67 +999,63 @@ firebase.login = function (arg) {
999999
}
10001000

10011001
var user = com.google.firebase.auth.FirebaseAuth.getInstance().getCurrentUser();
1002-
// TODO, no hurry
1003-
if (false && user) {
1004-
if (firebase._alreadyLinkedToAuthProvider(user, "phone")) {
1005-
// TODO .. you don't want to sign in again I guess...
1006-
// firebaseAuth.signInWithEmailAndPassword(arg.email, arg.password).addOnCompleteListener(onCompleteListener);
1007-
} else {
1008-
// TODO get auth credential.. prolly need to move this
1009-
user.linkWithCredential(authCredential).addOnCompleteListener(onCompleteListener);
1010-
}
1011-
} else {
10121002

1013-
var OnVerificationStateChangedCallbacks = com.google.firebase.auth.PhoneAuthProvider.OnVerificationStateChangedCallbacks.extend({
1014-
onVerificationCompleted: function (phoneAuthCredential) {
1015-
console.log("phone number verification completed");
1016-
firebase._verifyPhoneNumberInProgress = false;
1003+
if (user && firebase._alreadyLinkedToAuthProvider(user, "phone")) {
1004+
// skip sending an SMS if user is already linked to the phone-provider
1005+
resolve(toLoginResult(user));
1006+
return;
1007+
}
1008+
1009+
var OnVerificationStateChangedCallbacks = com.google.firebase.auth.PhoneAuthProvider.OnVerificationStateChangedCallbacks.extend({
1010+
onVerificationCompleted: function (phoneAuthCredential) {
1011+
firebase._verifyPhoneNumberInProgress = false;
1012+
var user = com.google.firebase.auth.FirebaseAuth.getInstance().getCurrentUser();
1013+
1014+
if (!user || firebase._alreadyLinkedToAuthProvider(user, "phone")) {
10171015
// the user previously authenticated with phone (or no prompt was required), so sign in and complete
10181016
firebaseAuth.signInWithCredential(phoneAuthCredential).addOnCompleteListener(onCompleteListener);
1019-
},
1020-
onVerificationFailed: function (firebaseException) {
1021-
console.log("onVerificationStateChangedCallbacks.onVerificationFailed: " + firebaseException);
1022-
firebase._verifyPhoneNumberInProgress = false;
1023-
var errorMessage = firebaseException.getMessage();
1024-
if (errorMessage.indexOf("INVALID_APP_CREDENTIAL") > -1) {
1025-
reject("Please upload the SHA1 fingerprint of your debug and release keystores to the Firebase console, see https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/AUTHENTICATION.md#phone-verification");
1026-
} else {
1027-
reject(errorMessage);
1028-
}
1029-
},
1030-
onCodeSent: function (verificationId, forceResendingToken) {
1031-
// If the device has a SIM card auto-verification may occur in the background (eventually calling onVerificationCompleted)
1032-
// .. so the prompt would be redundant, but it's recommended by Google not to wait to long before showing the prompt
1033-
setTimeout(function () {
1034-
if (firebase._verifyPhoneNumberInProgress) {
1035-
firebase._verifyPhoneNumberInProgress = false;
1036-
firebase.requestPhoneAuthVerificationCode(function (userResponse) {
1037-
var authCredential = com.google.firebase.auth.PhoneAuthProvider.getCredential(verificationId, userResponse);
1038-
var user = com.google.firebase.auth.FirebaseAuth.getInstance().getCurrentUser();
1039-
if (user) {
1040-
if (firebase._alreadyLinkedToAuthProvider(user, "phone")) {
1041-
firebaseAuth.signInWithCredential(authCredential).addOnCompleteListener(onCompleteListener);
1042-
} else {
1043-
user.linkWithCredential(authCredential).addOnCompleteListener(onCompleteListener);
1044-
}
1045-
} else {
1046-
firebaseAuth.signInWithCredential(authCredential).addOnCompleteListener(onCompleteListener);
1047-
}
1048-
}, arg.phoneOptions.verificationPrompt);
1049-
}
1050-
}, 3000);
1017+
} else {
1018+
user.linkWithCredential(phoneAuthCredential).addOnCompleteListener(onCompleteListener);
10511019
}
1052-
});
1020+
},
1021+
onVerificationFailed: function (firebaseException) {
1022+
firebase._verifyPhoneNumberInProgress = false;
1023+
var errorMessage = firebaseException.getMessage();
1024+
if (errorMessage.indexOf("INVALID_APP_CREDENTIAL") > -1) {
1025+
reject("Please upload the SHA1 fingerprint of your debug and release keystores to the Firebase console, see https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/AUTHENTICATION.md#phone-verification");
1026+
} else {
1027+
reject(errorMessage);
1028+
}
1029+
},
1030+
onCodeSent: function (verificationId, forceResendingToken) {
1031+
// If the device has a SIM card auto-verification may occur in the background (eventually calling onVerificationCompleted)
1032+
// .. so the prompt would be redundant, but it's recommended by Google not to wait to long before showing the prompt
1033+
setTimeout(function () {
1034+
if (firebase._verifyPhoneNumberInProgress) {
1035+
firebase._verifyPhoneNumberInProgress = false;
1036+
firebase.requestPhoneAuthVerificationCode(function (userResponse) {
1037+
var authCredential = com.google.firebase.auth.PhoneAuthProvider.getCredential(verificationId, userResponse);
1038+
var user = com.google.firebase.auth.FirebaseAuth.getInstance().getCurrentUser();
1039+
1040+
if (!user || firebase._alreadyLinkedToAuthProvider(user, "phone")) {
1041+
firebaseAuth.signInWithCredential(authCredential).addOnCompleteListener(onCompleteListener);
1042+
} else {
1043+
user.linkWithCredential(authCredential).addOnCompleteListener(onCompleteListener);
1044+
}
1045+
}, arg.phoneOptions.verificationPrompt);
1046+
}
1047+
}, 3000);
1048+
}
1049+
});
10531050

1054-
firebase._verifyPhoneNumberInProgress = true;
1051+
firebase._verifyPhoneNumberInProgress = true;
10551052

1056-
com.google.firebase.auth.PhoneAuthProvider.getInstance().verifyPhoneNumber(
1057-
arg.phoneOptions.phoneNumber,
1058-
60, // timeout (in seconds, because of the next argument)
1059-
java.util.concurrent.TimeUnit.SECONDS,
1060-
appModule.android.foregroundActivity, // or utils.ad.getApplicationContext()
1061-
new OnVerificationStateChangedCallbacks());
1062-
}
1053+
com.google.firebase.auth.PhoneAuthProvider.getInstance().verifyPhoneNumber(
1054+
arg.phoneOptions.phoneNumber,
1055+
60, // timeout (in seconds, because of the next argument)
1056+
java.util.concurrent.TimeUnit.SECONDS,
1057+
appModule.android.foregroundActivity, // or utils.ad.getApplicationContext()
1058+
new OnVerificationStateChangedCallbacks());
10631059

10641060
} else if (arg.type === firebase.LoginType.CUSTOM) {
10651061
if (!arg.customOptions || (!arg.customOptions.token && !arg.customOptions.tokenProviderFn)) {

0 commit comments

Comments
 (0)