For my iOS app, i carried out Auth.auth().addStateDidChangeListener to get the newest person credentials and if none discovered then register mechanically anonymously so the person can connect with database. My intent is to permit nameless customers, and if they need then there’s e mail/password to hyperlink account. I observed with the Auth.auth().addStateDidChangeListener all the time observing auth state, generally the state come again NIL (throughout app use, after auth already discovered on launch) and with my code a brand new anon person is created. This appears nearly random. With thousand of customers that is changing into a nuisance.
-
Is it frequent observe to take away Auth.auth().addStateDidChangeListener as soon as auth is discovered for a session?
-
In that case, then what occurs when the person is misplaced (because the listener is telling me it’s). I’ve seen this alone gadget a couple of times, throughout a session after auth was beforehand discovered, however can’t recreate purposefully. App is closely used, and it appears to be a reasonably uncommon prevalence, however is creating havoc.
My suspicion is that is as a result of auth token refresh not being fairly seamless, and state is misplaced momentarily so my code creates a brand new person within the meantime.
class For_AuthChange {
static let shared = For_AuthChange()
personal init () {}
personal var deal with: AuthStateDidChangeListenerHandle? //this one is an Auth Listener, NOT a database deal with. <<<<<
func StackOverFlow_StopObserver_forAuthState() {
if let deal with = deal with {
Auth.auth().removeStateDidChangeListener(deal with)
}
deal with = nil
}
func StackOverFlow_forAuthState_GetOnAppLaunch() {
guard deal with == nil else { return } // Guarantee observer is just set as soon as
deal with = Auth.auth().addStateDidChangeListener({ (auth, person) in
if let person = person {
if person.isAnonymous == true {
print(" 🔥 - person is nameless ⬜️ (person.uid)")
} else if person.isEmailVerified == true {
print(" 🔥 - person is e mail verified ✅ (person.uid)")
} else {
print(" 🔥 - person wants e mail verification 🟧 (person.uid)")
}
} else {
print(" 🔥 - person not discovered > Sign up Anonymously")
Auth.auth().signInAnonymously() { (authResult, error) in
if let error = error {
print(error.localizedDescription)
return
} else {
guard let person = authResult?.person else {
return
}
print("✅ nameless uid: (person.uid)")
return
}
}
}
//replace UI
NotificationCenter.default.publish(title: .auth_change, object: nil)
})
}
}