Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase error when deleting user account "This operation is sensitive and requires recent authentication. Log in again before retrying this request."

When I want to delete a firebase user account in my application, the operation passes normally if the user has recently logged but after a period of time if I try to delete the user I get this error "This operation is sensitive and requires recent authentication. Log in again before retrying this request." Normally the firebase refresh the user session automatically but I didn't find why he want the user to log again and even the value of Auth.auth().currentUser is not nil. Thank you for your help ! this is my code to delete the user account :

@objc func deleteAccountAction(){
self.showProgressView()
let user = Auth.auth().currentUser
let id=Auth.auth().currentUser?.uid
self.refProducts.child(id!).removeValue { error, _ in
    if(error != nil){
        print("firebase remove error")
        print(error?.localizedDescription.description ?? nil)
        self.dismissHUD(isAnimated: true)
    }
    else{
        self.refUsers.child(id!).removeValue { error, _ in
            if(error != nil){
                print("firebase remove error")
                print("error while deleting user from firebase: "+error!.localizedDescription)
                self.dismissHUD(isAnimated: true)    
            }
            else {

                user?.delete { error in
                    if  error != nil {
                        print("error while deleting user:" + error!.localizedDescription)
                        self.dismissHUD(isAnimated: true)
                    } else {

                       UserDefaults.standard.removeObject(forKey: "USER_UID")
                                                                               UserDefaults.standard.synchronize
                                    self.dismissHUD(isAnimated: true)
                                    let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "StartingViewController") as! StartingViewController
                                    nextVC.isAccoundDeleted=true
                                    GlobalVar.user=nil
                                    self.navigationController?.pushViewController(nextVC, animated: true)
                        }
                    }
                }
            }
        }
    }
}
like image 213
mark Avatar asked Jun 16 '19 09:06

mark


2 Answers

For certain sensitive operations (such as changing the user's password, or deleting the user account), Firebase requires that the user has recently signed in. If the user hasn't signed in recently when you try to perform such an operation, Firebase throws the exception you get.

When you get this exception, you should ask the user to re-enter their credentials, and retry the operation.

From the documentation on handling errors:

[Deleting a user account] is a security sensitive operation that requires a recent login from the user. This error indicates the user has not signed in recently enough. To resolve, reauthenticate the user by invoking reauthenticateWithCredential:completion: on FIRUser.

like image 145
Frank van Puffelen Avatar answered Sep 18 '22 10:09

Frank van Puffelen


In addition to Frank van's answer, time span for that is 5 minutes. After 5 minutes of login you cannot do such operations.

you can refer FIRAuthErrorCode (check out error code 17014 : FIRAuthErrorCodeRequiresRecentLogin = 17014)

like image 34
Deep Gandhi Avatar answered Sep 20 '22 10:09

Deep Gandhi