Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27133

Parse - Delete another user as super admin from iOS app

I use back4app for my app, I would like to delete another user (not authorised user on this device).

The app throws this:

[Error]: User cannot be deleted unless they have been authenticated. (Code: 206, Version: 1.19.1)

which make sense to me if I am not a super admin of the product. But in case I have super admin rights I would like to remove a user from the system completely.

Is there any solution for this purpose? I've tried to find some function from Parse.Cloud code.

The idea was to create cloud code and execute it form the iOS device by calling smth like this:

PFCloud.callFunctionInBackground("deleteUserAsSuperAdmin",
        withParameters: user id param here)
        { success, error) -> Void in
 
}

I have not found such a solution and for me it's a bit difficult to write such code in a right way using cloud code, for sure if this is an option.

Upvotes: 0

Views: 184

Answers (1)

Davi Macêdo
Davi Macêdo

Reputation: 2984

You will have to create a cloud code function for that, and use useMasterKey option to delete the user. Something like:

Parse.Cloud.define('deleteUser', async ({ user, params: { userIdToDelete } }) => {
  if (user) {
    const query = new Parse.Query(Parse.Role);
    query.equalTo('name', 'admin');
    query.equalTo('users', user);
    const count = await query.count({ useMasterKey: true });
    if (count === 1) {
      const userToDelete = new Parse.User();
      userToDelete = userIdToDelete;
      return userToDelete.destroy({ useMasterKey: true });
    }
  }
  throw new Error('Not an admin');
});

Upvotes: 0

Related Questions