iOS SWIFT:无法从Firebase数据库中删除用户

时间:2017-01-22 05:55:04

标签: ios swift firebase firebase-realtime-database firebase-authentication

我想从Firebase中删除当前用户。已验证的用户被删除但是,我无法删除数据库中该用户的数据。我做错了什么?

这是我的删除用户方法....

#include <stdio.h>

#define MAX_ELEMENT_LENGTH 10

struct data {
    int numbers[MAX_ELEMENT_LENGTH];
    int length;
};

void insert(struct data *x, int newNumber){
    int i, k;

    if(x->length == 0){
        x->numbers[x->length++] = newNumber;
    } else if(x->length < MAX_ELEMENT_LENGTH){//need this check
        //x->length = x->length + 1;//Do not increment before the loop. because x->numbers[i] occurs out of bounds

        for(i = 0; i < x->length; i++){   //descending order
            if(x->numbers[i] < newNumber){//descending order
                //insert position is i
                for(k = x->length-1; k >= i; k--){//k > i --> k >= i
                    x->numbers[k+1] = x->numbers[k];
                }
                break;//exit loop
            }
        }
        x->numbers[i] = newNumber;
        x->length++;
    }
}

int main(void){
    //DEMO
    struct data x = { {0}, 0 };

    insert(&x, 5);
    for(int i = 1; i < 11; ++i)
        insert(&x, i);

    for(int i = 0; i < x.length; ++i)
        printf("%d ", x.numbers[i]);
    puts("");
    return 0;
}

Firebase规则:

FIRAuth.auth()?.signIn(withEmail: (emailTextField?.text)! , password: (passwordTextField?.text)!, completion: { (user, error) in
            if error == nil {
                print("User Authenticate!!!")
                let user = FIRAuth.auth()?.currentUser

                user?.delete(completion: { (error) in
                    if error != nil {
                        print("Error unable to delete user")

                    } else {

                        DataService.ds.deleteCurrentFirebaseDBUser()
                        KeychainWrapper.standard.removeObject(forKey: KEY_UID)
                        self.performSegue(withIdentifier: "goToLogin", sender: nil)
                    }
                })

            } else {
                //Password was wrong, unable to authenicate user. Data is not updated
                print("!!!ALERT!!! Unable to authenticate user")
                let alert = UIAlertController(title: "Incorrect Password", message: "Please re-enter your password", preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
                self.present(alert, animated: true, completion: nil)
            }

        })

数据库:

{
"rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

错误:

2017-01-21 21:33:10.321704 APP [11582:4102711] [FirebaseDatabase] setValue:或removeValue:at / users / 4erkjkl543jfe46 failed:permission_denied

可选(错误域= com.firebase代码= 1&#34;权限被拒绝&#34; UserInfo = {NSLocalizedDescription =权限被拒绝})

3 个答案:

答案 0 :(得分:4)

我遇到了同样的问题。您无法使用功能deleteCurrentFirebaseDBUser(),因为Firebase删除功能(如果成功)会删除用户身份验证对象。

因此,当您想要使用deleteCurrentFirebaseDBUser()删除数据库中的用户数据时,用户不再进行身份验证。

目前我在Firebase删除功能之前删除了数据库中的用户数据,这不是理想的解决方案。

答案 1 :(得分:0)

我们可以从双方身份验证和数据库中删除用户。但在此之前我们需要首先重新验证用户,然后我们获取最新令牌以删除用户。

这是漂亮的代码:

  let user = Auth.auth().currentUser
            user?.reauthenticate(with:credential) { error in
                if let error = error {
                    // An error happened.
                    showAlertWithErrorMessage(message: error.localizedDescription)
                } else {
                    // User re-authenticated.
                    user?.delete { error in
                        if let error = error {
                            // An error happened.
                            showAlertWithErrorMessage(message: error.localizedDescription)
                        } else {
                            // Account deleted.
                            let userID = HelperFunction.helper.FetchFromUserDefault(name: kUID)
                            Database.database().reference(fromURL: kFirebaseLink).child(kUser).child(userID).removeValue()

                            try!  Auth.auth().signOut()
                             showAlertWithErrorMessage(message: "Your account deleted successfully...")
                           return
                        }
                    }

                }
            }

100%在我的项目中工作且经过充分测试

答案 2 :(得分:0)

仅从Firebase中删除子项使用“removeValue()”

events: [
{
  id: 2,
  start: '2018-05-02 10:00:00',
  end: '2018-05-02 11:00:00',
  color: 'blue',
  rendering: 'inverse-background'
},
{
  id: 2,
  start: '2018-05-02 14:00:00',
  end: '2018-05-02 15:00:00',
  color: 'green',
  rendering: 'inverse-background'
}