I used to be questioning what could be a stable movement to forestall a number of Firebase nameless customers from being created on a single machine.
We at the moment use the next API to create an nameless consumer:
Auth.auth().signInAnonymously
And the next code to signal out:
Auth.auth().signOut
To hyperlink an nameless consumer with an Apple account, we use:
consumer.hyperlink(with: oAuthCredential)
Under is our present movement, which leads to a number of nameless customers being created for a single machine.
-
On the sign-in web page, the consumer faucets "Proceed as visitor" -> the primary nameless consumer is created.
-
On the primary app web page, the consumer faucets "Proceed with Apple" -> the nameless consumer is linked to the Apple account.
-
The consumer faucets “Signal out”.
-
On the sign-in web page, the consumer faucets "Proceed as visitor" once more -> a second nameless consumer is created.
-
On the primary app web page, the consumer faucets "Proceed with Apple". Because the Apple account is already linked to the primary consumer, Firebase indicators the consumer again in as the primary consumer.
-
Consequently, the second nameless consumer turns into a “zombie” consumer.
If steps 3-5 are repeated, extra "zombie" nameless customers will proceed to be created, as proven within the screenshot.

My query is: what’s a stable and advisable movement to forestall this example?
func updateBasedOnLoginStatus() {
if let consumer = Auth.auth().currentUser, consumer.isAnonymous {
// Present Apple join button, disguise signal out button.
appleSignUpButton.isHidden = false
signOutButton.isHidden = true
} else {
// Conceal Apple join button, present signal out button.
appleView.isHidden = true
signOutButton.isHidden = false
}
}
// https://stackoverflow.com/questions/79615957/firebase-auth-link-anonymous-user-to-apple
personal func handleOAuthCredentialAsync(_ oAuthCredential: OAuthCredential) {
Activity {
defer {
updateBasedOnLoginStatus()
}
if let consumer = Auth.auth().currentUser, consumer.isAnonymous {
do {
_ = strive await consumer.hyperlink(with: oAuthCredential)
} catch let linkError as NSError {
if linkError.code == AuthErrorCode.credentialAlreadyInUse.rawValue {
if let newCredential = linkError.userInfo[AuthErrorUserInfoUpdatedCredentialKey] as? OAuthCredential {
do {
_ = strive await Auth.auth().signIn(with: newCredential)
} catch {
Utils.showErrorAlert(viewController: self, message: error.localizedDescription)
}
}
}
}
} else {
// We should not attain right here. This web page is dealing with nameless consumer to login consumer.
do {
_ = strive await Auth.auth().signIn(with: oAuthCredential)
} catch {
Utils.showErrorAlert(viewController: self, message: error.localizedDescription)
}
}
}
}
