firebase / FirebaseUI-iOS

iOS UI bindings for Firebase.
Apache License 2.0
1.52k stars 478 forks source link

Github OAuth provider won't link with Google and Apple #1130

Open drekka opened 1 year ago

drekka commented 1 year ago

Step 3: Describe the problem:

In my Firebase console authentication providers I have setup Google, Apple and Github as described in the Firebase UI doco. I also have also checked and enabled the automatic account linking option.

In my code (SwiftUI app) I have setup FirebaseAuthUI like this:

let authUI = FUIAuth.defaultAuthUI()!
authUI.delegate = self

authUI.providers = [
    FUIOAuth.appleAuthProvider(withAuthUI: authUI),
    FUIOAuth.githubAuthProvider(withAuthUI: authUI),
    FUIGoogleAuth(authUI: authUI),
]

So as to provide Apple, Google and Github sign on.

It appears that the GitHib provider does not want to play nice with other providers.

Can you tell me if I'm doing anything wrong?

drekka commented 1 year ago

I've now managed to solve one problem. Specifically the problem where the GitHub provider won't sign on if there is an Apple or Google provider already registered. I did it with this code:

extension Security: FUIAuthDelegate {

    func authUI(_: FUIAuth, didSignInWith user: User?, error: Error?) {
        if let error {
            let nsError = error as NSError
            if nsError.code == AuthErrorCode.accountExistsWithDifferentCredential.rawValue {
                pendingAuth = nsError.userInfo[AuthErrorUserInfoUpdatedCredentialKey] as? AuthCredential
            }

            signOnViewModel.error = error
            return
        }

        if let user {
            if let pendingAuth {
                Auth.auth().currentUser?.link(with: pendingAuth) { result, error in
                    self.pendingAuth = nil
                }
            }
        }
    }

Essentially this is a hack of the code I found on the Firebase site for dealing with AuthErrorCode.accountExistsWithDifferentCredentials. I found that if there is already a registration with the same email but a different provide, then when I attempt to sign in with the GitHub provider it throws this error.

In my code I then stash the GitHub credentials and ask the user to sign in with a different provider. Once they do that, it then uses the stashed GitHub credentials to link the GitHub provider.

it works in that I end up with one registration with both GitHub and Apple/Google providers attached.

However it's not a great user experience.

Nor does it solve the issues of registering Appel/Google after GitHub removing Github as a provider.

I've tried poking around in Auth.auth() and FUIAuth.defaultAuthUI() but I've not been able to find anything that will tell me what has already been registered for a specific email. I've called Auth.auth().fetchSignInMethods(forEmail:...) but it always returns an empty array.

Any advice????