Firebase sign out not working in Swift
2020 ...
do { try Auth.auth().signOut() }
catch { print("already logged out") }
Typically, on your "base" screen, something like ...
func logoutUser() {
// call from any screen
do { try Auth.auth().signOut() }
catch { print("already logged out") }
navigationController?.popToRootViewController(animated: true)
}
Sign Out of Firebase in Swift 4
This always works for me:
do {
try Auth.auth().signOut()
} catch let error {
print("Error: ", error.localizedDescription)
}
If this isn't working for you, provide us with some more context. What do you mean by "it's not working"? If you use this code provided, it will tell what error you're encountering.
Firebase user can not be logged out - Swift
I had this exact same problem. Since this happens when you relaunch, try putting this code in your app.delegate.
FIRAuth.auth()?.addAuthStateDidChangeListener { auth, user in
if let user = user {
// User is signed in.
} else {
// No user is signed in.
}
}
Writing to Firestore after logging out the Firebase authenticated user
Is there a way to authenticate and start the Firestore write and logout the user in the meantime, not having to wait for the write to finish?*
Unforetunately no. Once you call the signOut() method, it invalidates your auth token. If the write operation reaches successfully before it can invalidate the token, then it may succeed. Otherwise it will fail.
Is there a way to somehow cache this Firestore write until the phone is online again, even if the app has been closed in between?*
Firestore supports offline persistence by default, meaning all writes you perform (except for Transactions) will be cached and will get executed once you are connected to the internet.
It is generally a better practice anyways to do the cleanup in the back-end since the Firebase admin SDK bypasses security rules, you will be able to cleanup data that are otherwise inaccessible to your mobile clients.
Related Topics
How to Hide the Navigationbar When Embedding Swiftui in Uikit
Expression Implicitly Coerced from 'String' to Any
Detailed Instruction on Use of Nsopenpanel
Failed to Get Descriptors for Extensionbundleid
Difference Between? and ! in Swift Language
How Is a Return Value of Anyobject! Different from Anyobject
Swift - 'Bool' Is Not a Subtype of 'Void'
Swift: Does Closure Have References to Constants or Variables
Handle Swiftui and Corelocation with Mvvm-Pattern
How to Get Next Case of Enum(I.E. Write a Circulating Method) in Swift 4.2
How to Check Object Is Nil or Not in Swift
How to Use Unsafemutablerawpointer to Fill an Array
Swift 3 Get Start Index (As Int) of Substring
Round Top Corners of a Uiview in Swift