Can I delete FIRAuth users using their User UID?
The client-side SDKs for Firebase can only delete the currently authenticated user. They do not allow deleting any other user, or deleting a user identified by their UID.
This means that you cannot implement administrative functionality like deleting an arbitrary user in Swift.
Instead you will have to use the Firebase Admin SDK to delete an arbitrary user or allow your iOS users to delete their own account. Given the actions the Admin SDK allows its user to perform, it should only be used in trusted environments, such as a server you control or Cloud Functions for Firebase.
Delete Google or Facebook User from Firebase
To delete google and facebook user from firebase, you must have to Re-authenticate user. Deleting an account require that the user has recently signed in. If you perform one of these actions, and the user signed in too long ago, the action fails with the FIRAuthErrorCodeCredentialTooOld
error. When this happens, re-authenticate the user by getting new sign-in credentials from the user and passing the credentials to reauthenticate
.
let user = Auth.auth().currentUser
var credential: AuthCredential
// Prompt the user to re-provide their sign-in credentials
user?.reauthenticate(with: credential) { error in
if let error = error {
// An error happened.
} else {
// User re-authenticated.
}
}
Now you can delete a user account with the deleteWithCompletion
method.
let user = Auth.auth().currentUser
user?.delete { error in
if let error = error {
// An error happened.
} else {
// Account deleted.
}
}
iOS SWIFT: Unable to delete user from Firebase Database
I'm having the same issue. You are not able to make use of your function deleteCurrentFirebaseDBUser()
because the Firebase delete function (if successful) removes the user auth object.
As a result user is not authenticated anymore at the time you want to delete user's data in database with deleteCurrentFirebaseDBUser()
.
Currently I delete user's data in database before Firebase delete function which is not the ideal solution.
Getting a FIRUser from their uid
In order to get someone by their Uid, you would need to store The data that you want in the database. You can use child["user"] with child["Uid"] to store things like a user's name or photoURL. Check out this series of videos. He shows you how to do a signup flow with FIRAuth and FIRDatabase. The next video in the series shows how you query the data if you don't already know how to do that.
Write Firebase Auth User UID to database
databaseRef.child("users").child(FIRAuth.auth()!.currentUser!.uid).setValue(user)
How can I detect if the user was deleted from Firebase auth?
You can achieve it by doing this inside appDelegate
:
//Check if user does exists
func checkUserAgainstDatabase(completion: @escaping (_ success: Bool, _ error: NSError?) -> Void) {
guard let currentUser = Auth.auth()?.currentUser else { return }
currentUser.getTokenForcingRefresh(true) { (idToken, error) in
if let error = error {
completion(false, error as NSError?)
print(error.localizedDescription)
} else {
completion(true, nil)
}
}
}
And you can do something like this after checking with the above function in didFinishLaunchingWithOptions
:
If user does exist:
self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "CustomTabBarViewController")
else:
self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "WelcomeViewController")
And to test if it did work simply remove user manually from the Auth manager in the Firebase Console. So like this it should just show the welcome screen if user has been deleted.
Displaying firebase child from user.uid to username in Swift 3
Here's some stuff to consider - a little, some or all may get you headed in the right direction. Also, you can probably remove all of the DispatchQueue calls as Firebase does most of the heavy lifting for you, and with proper code structure, they are not needed.
1) A Swifty user class
class UserClass {
var usersname = ""
var email = ""
var password = ""
var photoUrl = ""
var uid = ""
init(withSnapshot: FIRDataSnapshot) {
let dict = withSnapshot.value as! [String:AnyObject]
uid = withSnapshot.key
usersname = dict["usersname"] as! String
email = dict["email"] as! String
password = dict["password"] as! String
photoUrl = dict["photoUrl"] as! String
}
}
note that we are using the var uid of each user to identify them (their 'key')
The structure that matches that class
users
uid_0
email: "bill@email.com"
password: "myPassword"
photoUrl: "http://www.url.com"
usersname: "Bill"
uid_1
email: "leroy@email.com"
password: "aPassword"
photoUrl: "http://www.anotherUrl.com"
usersname: "Leroy"
Notice again the users and their associated info are stored within the /users node in each child node that has that users uid as the key.
And some code that reads in uid_0, prints the uid and name. This code is a one-shot so it reads uid_0, but does NOT leave an observer attached to the node.
let userRef = rootRef.child("users/uid_0")
userRef.observeSingleEvent(of: .value, with: { snapshot in
let aUser = UserClass(withSnapshot: snapshot)
print("uid \(aUser.uid) has name \(aUser.usersname)")
})
Now the Geofire node would like something like this
user_locations
uid_0
//geofire data
uid_1
//geofire data
So now there is a direct correlation between the users node and their location.
In general, it's a good idea to disassociate node names (keys, which are static data) from the data they contain, which is dynamic.
With the structure in the initial question, imagine if 'europeanjunkie' changed his name to 'europeanjunkieDude'. Every place you reference 'europeanjunkie' would then have to be changed - and if it's used as a key, the entire node would have to be read in, deleted, updated, and re-written.
Using child keys created by Firebase, uid's and childByAutoId(), removes that issue.
Hope that helps!
Related Topics
iOS 8 Swift Audio Playback Execute Method on Completion
Linking Error When Building Parse in Xcode 7
Moving an Object Across the Screen at a Certain Speed.(Sprite Kit)
Tabview, Tabitem: Running Code on Selection or Adding an Ontapgesture
How to Stop an Infinitely Rotating Image? and How Does One Implement Removeallanimations
Composing Video and Audio - Video's Audio Is Gone
Empty Class in Swift Playground Gives _Lldb_Expr_ Error
Including Zeros in Front of an Integer
After Getting Image from Uiimagepickercontroller, Uiimageview Rotates Image for iPhone 5
Is There Difference Between Using a Constructor and Using .Init
How to Run App in Simulator Xcode 6
How to Display Mtkview with Rgba16Float Mtlpixelformat
Swift Package Manager with Resources Compile Errors
Differences Between Ways of Initializing a Dictionary
How to Find a Maximum Value in a Swift Dictionary
Performing a Completion Handler Before App Launches
Check If an Int Value Is Greater or Equal to Another Int? Value