我的Firebase电子邮件/密码登录页面应扩展为其他登录方式,如苹果登录(最终为谷歌登录、Facebook登录等)。
我的步骤:
-
使用电子邮件/密码登录Firebase
-
注销
-
使用“使用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)
}
}
}
}
}