Search code examples
iosparse-platformparse-cloud-codepfuser

Create new PFUser using code without logging in as this new PFUser


I am creating a new PFUser to be a "sub user" of the currently logged in user.

For example Master is logged in and decides to create a Junior User.

The Junior user will have a different PFRole to the Master.

I can create a new User via

    var newUser = PFUser()
    newUser.email = "someemail@gmail.com"
    newUser.username = "Demo Subuser"
    newUser.password = "12341234"


    newUser.signUpInBackgroundWithBlock { (newUser, error) -> Void in

        println(error)

    }

The user is created, but the problem is I am logged out of the master account and logged in as the Junior user.

How can I create a new PFUser without it logging into that account.

Ideally, I want to send an email to the new subuser stating, welcome here is your username and details.

Maybe I should be using CloudCode ?

Thanks


Solution

  • Create a Cloudcode function and upload it to Parse

    Parse.Cloud.define("createNewUser", function(request, response) {
    
        // extract passed in details 
        var username = request.params.username
        var pw = request.params.password
        var email = request.params.email
    
        // cloud local calls
        var user = new Parse.User();
        user.set("username", username);
        user.set("password", pw);
        user.set("email", email);
    
        user.signUp(null, {
            success: function(user) {       
            //response.error("working");
            // do other stuff here 
            // like set ACL
            // create relationships
            // and then save again!! using user.save
            // you will need to use Parse.Cloud.useMasterKey(); 
    
        },
        error: function(user, error) {
            //response.error("Sorry! " + error.message);
        } });
    
    });
    

    Call the cloud code function from within your app via. Pass in details to the clod function using a Dictionary, ["username":"", "password":""] etc

    PFCloud.callFunctionInBackground