代码之家  ›  专栏  ›  技术社区  ›  iKK

Apple登录导致FIRAuthErrorUserInfoNameKey=错误\u电子邮件\u已在使用中(代码=17007)

  •  0
  • iKK  · 技术社区  · 5 年前

    我的Firebase电子邮件/密码登录页面应扩展为其他登录方式,如苹果登录(最终为谷歌登录、Facebook登录等)。

    我的步骤:

    1. 使用电子邮件/密码登录Firebase
    2. 注销
    3. 使用“使用Apple登录”登录
    Error Domain=FIRAuthErrorDomain Code=17007
    
    "The email address is already in use by another account."
    
    UserInfo={NSLocalizedDescription=The email address is already in use by another account.,
    
    FIRAuthErrorUserInfoNameKey=ERROR_EMAIL_ALREADY_IN_USE}
    

    我打算做的是将现有的 Email/Password-Firebase -向 Sign in with Apple -账户(如上所述) here here ).

    要不是这样做,我需要错误 FIRAuthErrorUserInfoUpdatedCredentialKey

    在我的情况下,我得到错误的电子邮件已经在使用中,这不会导致任何老用户被链接。

    这是我的密码:

    let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce)
    
    Auth.auth().signIn(with: credential) { (authResult, error) in
    
        if (error != nil) {
            print(error?.localizedDescription as Any)
            return
        }
        print("signed in with Apple...")
        
        do {
            // if user did log in with Email/Password previously
            if let email = try THKeychain.getEmail(),
               let password = try THKeychain.getPassword() {
    
                let credential = EmailAuthProvider.credential(withEmail: email, password: password)
    
                if let user = authResult?.user {
    
                    // here I am trying to link the existing Firebase-Email/Password account to the just signed-in with Apple account
                    user.link(with: credential) { (result, linkError) in
                        
                        print(linkError)  // this is where I get FIRAuthErrorUserInfoNameKey=ERROR_EMAIL_ALREADY_IN_USE
                        
                        // unfortunately, the two accounts are not linked as expected due to this error !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
                        // What is missing ??????????????????
                        
                        loginStatus = true
                    }
                }
            } else {
                loginStatus = true
            }
        } catch {
            print(error.localizedDescription)
        }
    }
    

    在Firebase文档中,它说:

    Sign in with Apple will not allow you to reuse an auth credential to link to an existing account. If you want to link a Sign in with Apple credential to another account, you must first attempt to link the accounts using the old Sign in with Apple credential and then examine the error returned to find a new credential. The new credential will be located in the error's userInfo dictionary and can be accessed via the FIRAuthErrorUserInfoUpdatedCredentialKey key.

    “如果您想使用Apple credential将登录链接到另一个帐户,您必须首先尝试使用旧的Apple credential登录链接帐户…”这部分的确切含义是什么?问题是什么 old Sign in with Apple credential ????????

    我该怎么做呢?

    事实上,在连接电话中,我实际上期待着某种 linkError.userInfo 使用更新的用户登录。但是我的例子中的linkError只给了我 ERROR_EMAIL_ALREADY_IN_USE

    作为 Peter Friese mentions in his Blog ,我应该能找到一个 AuthErrorUserInfoUpdatedCredentialKey

    以下是彼得例子的摘录:(由于未知原因,同样不适用于我的情况)

    currentUser.link(with: credential) { (result, error) in // (1)
        if let error = error, (error as NSError).code == AuthErrorCode.credentialAlreadyInUse.rawValue { // (2)
            print("The user you're signing in with has already been linked, signing in to the new user and migrating the anonymous users [\(currentUser.uid)] tasks.")
            if let updatedCredential = (error as NSError).userInfo[AuthErrorUserInfoUpdatedCredentialKey] as? OAuthCredential {
                print("Signing in using the updated credentials")
                Auth.auth().signIn(with: updatedCredential) { (result, error) in
                    if let user = result?.user {
                        // TODO: handle data migration
                        self.doSignIn(appleIDCredential: appleIDCredential, user: user) // (3)
                    }
                }
            }
        }
    }
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   iKK    5 年前

    如果我按下按钮 Sign in with Apple AuthErrorUserInfoUpdatedCredentialKey 在链接回调中。

    在那里,我检索 updatedCredential

    请参阅下面的代码。

    这个数据迁移步骤是如何工作的???

    user.link(with: appleCredentials) { ... } 做这个工作?

    let appleCredentials = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce)
    
    do {
        // if user did log in with Email/Password anytime before
        if let email = try THKeychain.getEmail(),
           let password = try THKeychain.getPassword() {
            
            let firebaseEmailCredentials = EmailAuthProvider.credential(withEmail: email, password: password)
            
            Auth.auth().signIn(with: firebaseEmailCredentials) { (authResult, error) in
                
                if let user = authResult?.user {
                    
                    user.link(with: appleCredentials) { (result, linkError) in
                        
                        if let linkError = linkError, (linkError as NSError).code == AuthErrorCode.credentialAlreadyInUse.rawValue {
                            
                            print("The user you're signing in with has been linked.")
                            print("Signing in to Apple and migrating the email/pw-firebase-users [\(user.uid)]` data.")
                            if let updatedCredential = (linkError as NSError).userInfo[AuthErrorUserInfoUpdatedCredentialKey] as? OAuthCredential {
                                
                                print("Signing in using the updated credentials")
                                Auth.auth().signIn(with: updatedCredential) { (result, error) in
                                    if let _ = result?.user {
                                        print("signed in with Apple...")
                                        // TODO: handle data migration
                                        print("Data-migration takes place now...")
                                        
                                        loginStatus = true
                                    }
                                }
                            }
                        }
                        else if let error = error {
                            print("Error trying to link user: \(error.localizedDescription)")
                        }
                        else {
                            if let _ = result?.user {
                                loginStatus = true
                            }
                        }
                    }
                }
            }
        } else {
            // case where user never logged in with firebase-Email/Password before
            Auth.auth().signIn(with: appleCredentials) { (result, error) in
                if let _ = result?.user {
                    print("signed in with Apple...")
                    loginStatus = true
                }
            }
        }
    } catch {
        print(error.localizedDescription)
    }