简体   繁体   中英

How can I create a new user in Parse, without changing the current user?

I am currently making a friend request module. The user clicks "Approve" the program adds the friend to the _User class on Parse Server. I believed the code below would do it, and it does, but the problem is that is changes the current user to the new friend that has been added. So if the current user is "Bob", and Bob adds "Mike" the new current user is Mike.

I've been experimenting with signUp() , signUpInBackground() , but neither seem to work.

ParseUser newFriend = new ParseUser();
newFriend.setUsername(friendRequestFrom); //friendRequestFrom is a String that carries a name
newFriend.setPassword("12345");
newFriend.signUpInBackground(new SignUpCallback() {
    @Override
    public void done(ParseException e) {
        if(e == null){
            Log.i("Parse Result","Succesful!");
            Log.i("Current User",ParseUser.getCurrentUser().getUsername());
        }
        else{
            Log.i("Parse Result","Failed " + e.toString());
        }
    }
});

In order to create a new Parse user in Parse using client side code, you have to use a cloud code function hosted in your app backend.

In fact, for security reasons, client librararies are not permitted to directly add users.

In your server side, create a file (main.js) containing the following code:

Parse.Cloud.define("createNewUser", function(request, response) {
var User = Parse.Object.extend("User");
var us = new User();

us.set("username", request.params.username);
us.set("name", request.params.name);
us.set("email", request.params.email);
us.set("password", request.params.password);
us.save(null, {
    useMasterKey: true,
    success: function(obj) {
        response.success("user added");
    },
    error:function(err){
       response.error(err);         
    }
});       
});

Then, you can test this function in your javascript client code as following:

var params =  { username: "userEmailAddress@yahoo.fr",
          email: "userEmailAddress@yahoo.fr",
          name:"Here write the user name",
          password:"TheUserPasswordHere"};

Parse.Cloud.run("createNewUser", params).then(function(response){console.log("response: "+response);}).catch(function (err) {
     console.log("error: "+err);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM