kenwen
kenwen

Reputation: 834

Firebase auth linking error - User can only be linked to one identity for the given provider

When users first run my app it creates an anonymous account for them, which if they decide to use higher level features, will be converted to an email authenticated account

Code for anon sign in is straightforward (taken directly from the docs)

// No user is signed in.
Auth.auth().signInAnonymously() { (usr, err) in
    if (err == nil) {
        print("Anon User ID \(usr!.uid)")
    } else {
        print("Anon auth error \(String(describing: err!.localizedDescription))")
    }
}

When registering, I create a new account with email credentials and link to the anonymous account like so

Auth.auth().createUser(withEmail: email, password: password) { (user, err) in
    if (err != nil) {
        print("Error registering \(String(describing: err!.localizedDescription))")
    } else {
        let credential = EmailAuthProvider.credential(withEmail: email, password: password)
        Auth.auth().currentUser!.link(with: credential, completion: {(user, err) in
            if (err != nil) {
                // Error
            } else {
                // Linked
            }
        })
    })

Problem is every single time I get the "User can only be linked to one identity for the given provider"

and I have no accounts registered in Firebase at all

Accounts, anonymous and email auth are successfully created but just cannot link

Upvotes: 1

Views: 3822

Answers (1)

bojeil
bojeil

Reputation: 30798

You can't link 2 existing Firebase users. You can only link a new credential to an existing user. When you are registering the user, you should just currentUser.linkWithCredential on the anonymous user using the email/password credential without calling createUser first. This is basically upgrading the anonymous user to an email/password user.

Upvotes: 2

Related Questions