diff --git a/firebase.android.js b/firebase.android.js index 60e627bf..e8ae9b8e 100755 --- a/firebase.android.js +++ b/firebase.android.js @@ -819,7 +819,17 @@ firebase.login = function (arg) { if (!arg.email || !arg.password) { reject("Auth type emailandpassword requires an email and password argument"); } else { - firebaseAuth.signInWithEmailAndPassword(arg.email, arg.password).addOnCompleteListener(onCompleteListener); + var user = com.google.firebase.auth.FirebaseAuth.getInstance().getCurrentUser(); + if (user) { + if (firebase._alreadyLinkedToAuthProvider(user, "password")) { + firebaseAuth.signInWithEmailAndPassword(arg.email, arg.password).addOnCompleteListener(onCompleteListener); + } else { + var authCredential = com.google.firebase.auth.EmailAuthProvider.getCredential(arg.email, arg.password); + user.linkWithCredential(authCredential).addOnCompleteListener(onCompleteListener); + } + } else { + firebaseAuth.signInWithEmailAndPassword(arg.email, arg.password).addOnCompleteListener(onCompleteListener); + } } } else if (arg.type === firebase.LoginType.CUSTOM) { diff --git a/firebase.ios.js b/firebase.ios.js index b9908380..764f007a 100755 --- a/firebase.ios.js +++ b/firebase.ios.js @@ -931,7 +931,23 @@ firebase.login = function (arg) { if (!arg.email || !arg.password) { reject("Auth type emailandpassword requires an email and password argument"); } else { - fAuth.signInWithEmailPasswordCompletion(arg.email, arg.password, onCompletion); + var fIRAuthCredential = FIREmailPasswordAuthProvider.credentialWithEmailPassword(arg.email, arg.password) + if (fAuth.currentUser) { + // link credential, note that you only want to do this if this user doesn't already use fb as an auth provider + var onCompletionLink = function (user, error) { + if (error) { + // ignore, as this one was probably already linked, so just return the user + log("--- linking error: " + error.localizedDescription); + fAuth.signInWithCredentialCompletion(fIRAuthCredential, onCompletion); + } else { + onCompletion(user); + } + }; + fAuth.currentUser.linkWithCredentialCompletion(fIRAuthCredential, onCompletionLink); + + } else { + fAuth.signInWithEmailPasswordCompletion(arg.email, arg.password, onCompletion); + } } } else if (arg.type === firebase.LoginType.CUSTOM) {