Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27133

Back4App create user using authenticated permission for PFUser class

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.

Upvotes: 0

Views: 523

Answers (2)

gilbriatore
gilbriatore

Reputation: 678

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

pfUser.sessionToken = PFUser.current().sessionToken;
pfUser.signUpInBackground {...}

Upvotes: 0

Tanzim Chowdhury
Tanzim Chowdhury

Reputation: 3090

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.

Upvotes: 0

Related Questions