firebase / FirebaseUI-iOS

iOS UI bindings for Firebase.
Apache License 2.0
1.51k stars 475 forks source link

Not possible to link existing credentials #744

Open sipersso opened 5 years ago

sipersso commented 5 years ago

Accordning to the documentation, to link credentials I have to

1: Sign in the user using any authentication provider or method. 2: Complete the sign-in flow for the new authentication provider up to, but not including, calling one of the FIRAuth.signInWith methods. For example, get the user's Google ID token, Facebook access token, or email and password. 3: Get a FIRAuthCredential for the new authentication provider:

But I don't see how I can do this with FirebaseUI since it seems to always complete the sign in with no way to abort before the signInWith methods.

Step 2: Describe your environment

Step 3: Describe the problem:

I use a custom authentication token in conjunction with iCloud tokens to provide automatic accounts on iOS. I want to enable users to add more ways to sign in (Google Account or Email), so they can log in on their Android devices and Web.

Steps to reproduce:

  1. Sign in with a user using custom auth token
  2. Use AuthUI to get a new credential.
  3. Link the credentials

I don't see how to get credentials from FireBaseUI without having it call FIRAuth.signInWith. Is there a way to do this?

func authUI(_ authUI: FUIAuth, didSignInWith authDataResult: AuthDataResult?, error: Error?) {
       //User is already signed in here so linking will fail.
        oldUser.link(with:authCredential, completion: { (authDataResult, error) in

            //Error Code=17025 "This credential is already associated with a different user
        })
}
morganchen12 commented 5 years ago

FirebaseUI provides sign-in and auto-upgrade of anonymous users. It does not provide any UI or facilities for linking together two non-anonymous users. The error you're getting is because the provider you're trying to link with is already associated with a separate account. See this doc.

sipersso commented 5 years ago

I wasn’t expecting auto upgrade. Account likning is listed in this doc https://firebase.google.com/docs/auth/ios/firebaseui. I think this is very misleading. If I understand you correctly I would have to implement my own way of retreiving credentials for this and there is no support for account likning except for anonymous users?

Why isn’t it possible to just get credentials without Sign in?

sipersso commented 5 years ago

As I understand it I can’t get the credentials from the logged in user so to be able to use the doc you are referring to I have to skip using FirebaseUI altogether?

morganchen12 commented 5 years ago

The document you linked should say Anonymous account linking instead of Account linking. You can use FirebaseUI to obtain an AuthDataResult that you can use to link to another provider. It's not possible to get credentials without signing in because without authenticating you don't have access to the credential; anyone could link to your account if it wasn't gated by some auth method.

sipersso commented 5 years ago

This doc https://firebase.google.com/docs/auth/ios/account-linking says specifically not to call signInwith after getting credentials if you want to link accounts. I suspect that signInWith creates a user which results in an error message. Am I understanding this correctly?

I don’t see how I could use the AuthDataResult since it is too late. The user is already created

morganchen12 commented 5 years ago

You're right. We could probably add this to FirebaseUI reusing the existing UI and just returning an AuthDataResult, but for the meantime you'll have to use Auth directly.

sipersso commented 5 years ago

Thanks for the answer. I built my own UI for likning based on the Firebase QuickStart samples and will move away from Firebase UI since this work pretty much included all I need for account creation as well.

morganchen12 commented 5 years ago

I'll take a look at exposing an AuthDataResult callback.

willbattel commented 4 years ago

@morganchen12 is there any update on this? We'd like to use the FirebaseUI bindings but we require the ability to link multiple providers, which does not seem possible at this time.

yo1995 commented 4 years ago

Same needs here! ✋ It would be great if we can use the existing FUI to link multiple providers.