0

As administrator I want to create another users from the iOS app. In my system I would like users to be created via my admin panel.

I have switched public permission to authenticated for User class: enter image description here

When just public checkmark were selected I was able to create new user in database with this code:

func createUser(with params: [String : Any], completion: @escaping (Result<User?, NSError>) -> ()) {
        let pfUser = PFUser()
        pfUser.username = params["username"] as? String
        pfUser.password = params["password"] as? String
        pfUser.email = params["email"] as? String
        
        pfUser["firstname"] = params["firstname"] as? String
        pfUser["lastname"] = params["lastname"] as? String
                    
        let imageFile = PFFileObject(name:"image.jpg", data:params["avatar"] as! Data)
                    
        pfUser["avatar"] = imageFile
        
        pfUser.signUpInBackground { (succeeded, error) in
            if let error = error {
                print(error)
                completion(.failure(error as NSError))
            } else {
                print("User created successfully")
                let user = User()
                user.setPFUser(user: pfUser)
                completion(.success(user))
            }
        }
    }

But now I am getting error for authenticated class level permission Permission denied, user needs to be authenticated.:

enter image description here

Is there any solution to do it? Maybe I can inject auth token or smth like this which I user for current admin user which creates another one at the moment.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

2 Answers2

0

First of all, as an Admin you should have an admin user in your user table.

And as you mentioned that you have an "admin user", simply login to your admin panel as that user and create new users.

Tanzim Chowdhury
  • 3,020
  • 2
  • 9
  • 21
  • thanks for reply! you mean some admin user by default, or where can I find this user? is this some specific setting? – Matrosov Oleksandr Mar 24 '21 at 14:58
  • or you mean I need to add some column to specify that user is admin, anyway form my question you can see that this looks like will not work for not public permissions, right? – Matrosov Oleksandr Mar 24 '21 at 15:00
  • You can simply have an admin user by default. Public (non authenticated users) can read your data from the user class but not write to it. – Tanzim Chowdhury Mar 26 '21 at 10:40
0

When saving pfUser you will need to send the current sessionToken... (something like this)

pfUser.sessionToken = PFUser.current().sessionToken;
pfUser.signUpInBackground {...}
gilbriatore
  • 658
  • 7
  • 12