2

I've looked up questions answers, but not found correct solution.

Here is the issue: I've already signed up with email/password, then I signed out. Then I tried sign in via facebook that has same email address, then it returned this error message.

"An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address."

What I want is merge(link) the facebook account with the existing one. In Firebase documentation, it requires the previous account pre logged in to link with the new one.

user.link(with: credential) { (user, error) in
  // ...
}

But in the login page, there is no current user logged in.

Please suggest me the best way to solve this issue.

Daniel
  • 507
  • 6
  • 22
  • The user will have to be signed in to their existing account already, before they can link the Facebook account. To know what providers an email address is already registered with, use `fetchProvidersForEmail:`: https://firebase.google.com/docs/reference/ios/firebaseauth/api/reference/Classes/FIRAuth#-fetchprovidersforemailcompletion – Frank van Puffelen Jun 12 '17 at 13:35
  • If you look at the Authentication Example, they have this scenario https://github.com/firebase/quickstart-ios/tree/master/authentication – Ryan Heitner Jun 12 '17 at 13:37
  • The code you want is func firebaseLogin(_ credential: AuthCredential) { if let user = Auth.auth().currentUser { // [START link_credential] user.link(with: credential) { (user, error) in – Ryan Heitner Jun 12 '17 at 13:40
  • @FrankvanPuffelen Currently this code is on sign in page and not logged in with any credentials. I wanted to provide users more convenient way they can log in either ways providing them same user information. – Daniel Jun 12 '17 at 14:37
  • @PaulStein As I said: the user will have to be signed in to the existing provider already in order to allow the accounts to be linked. We cannot "magically" sign them into the email/password provider. The flow could be: 1) have them enter their email address, 2) find that an account already exists for that address, 3) find the provider for that account (with the method I have), 4) ask the user if they want to link the two providers to that account, 5) sign them into email+password, 6) sign them into Facebook, 7) link the providers to the account. – Frank van Puffelen Jun 12 '17 at 15:36

2 Answers2

1

What you do is check your current user when you (login/link)

If you are logged in you link the user

if let user = Auth.auth().currentUser
            user.link(with: credential) { (user, error) in

If you are not logged in just a regular first time sign in

Auth.auth().signIn(with: credential) { (user, error) in

All of this and more can be seen in the sample app

https://github.com/firebase/quickstart-ios/tree/master/authentication

Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
  • As I understand according to https://stackoverflow.com/a/41843039/580621 if user 1) `.signIn` with Facebook; 2) after some time `.signIn` with Google => then firebase delete Facebook from providers (and add Google instead). So next time calling `.signIn` for Facebook will gives an error `"An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address."`. It seems we need to check for each email that Google is in providers and Facebook is not, if so use `.link` for Facebook. Is it correct? Any other solutions? – pupadupa Jun 12 '17 at 22:04
1

As described here Authentication using Facebook at first and then Google causes an error in Firebase for Android There is a problem wit Facebook login if user previously login with google.

For example,

Case 1:

  • User sign in with Google
  • User logout from app
  • User can't sign in using Facebook

Case 2:

  • User sign in with Facebook
  • User logout from app
  • User sign in with Google => in this point Firebase delete Facebook as Provider
  • User logout from app
  • User can't sign in using Facebook

If user try to sign in with Facebook, but user with given email already exist (with Google provider) and this errors occures:

"An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address."

So, just ask user to loging using Google (and after it silently link Facebook to existing account)

Facebook and Google Sign In logics using firebase

pupadupa
  • 1,530
  • 2
  • 17
  • 29