danr
danr

Reputation: 91

parse-server: How to differentiate between signUp and regular ParseUser.save in User class BeforeSave?

This is an issue where parse-server behaves differently from Parse.com. I have anonymous users in my app, and when using ParseUser.signUp (Android SDK) there's an important difference between parse-server and Parse.com. Parse.com would first check for username conflicts, and if there is one, the beforeSave function wouldn't even be called. This allowed me to make some assumptions in the beforeSave code.

Now in parse-server, the beforeSave is always called, and only after I response.success() does it fail to save it. The problem is that there is some code that shouldn't happen if the internal signUp fails (e.g. duplicate username) but happens anyway since I assume that if beforeSave is called, the username is unique. My solution was to do the check myself via a ParseQuery on the username, but now there's another issue - how do I differentiate between an anonymous user and a new user?

In my app, every new user is automatically saved as an anonymous user. For an anonymous user, there's some things you can't do (change username for example). Now after a while, when he wants to sign up, he enters a username, and in the beforeSave I can't tell if he is trying to change only the username, or is he signing up? If he is trying to sign up, I should allow him to set the username, but if he is just trying to change his username, then I'm suppose to reject the change.

So to summarize: How can I tell in the User class beforeSave if signUp was called or a regular save?

Upvotes: 3

Views: 705

Answers (2)

Julian Vogels
Julian Vogels

Reputation: 688

I couldn't find anything obvious in the documentation, but after analysing the request object of the beforeSave hook, I found a solution:

  1. Verifying if the authData object contains the anonymous key tells us if an anonymous user is saved. Once signed up, authData is undefined:

    var authData = request.object.get("authData"); if (authData != undefined && authData.anonymous != undefined) { console.log("#### User is anonymous"); response.success(); // save and return return; }

To figure out if the user has just signed up, you'd compare if the email address was set (not changed but that it was undefined before). You do that by asking request.original for the email:

// object now has an email for the first time
if (request.object.get("email") != undefined &&
    request.original.get("email") == undefined) {
    console.log("#### User signed up");
    …
}

I'm not sure what the recommended way is, but this seems to work to distinguish the anonymous user from the real, signed up user.

Upvotes: 0

mogile_oli
mogile_oli

Reputation: 2188

Can you try using the .existed() function in your beforeSave?

Parse.Cloud.afterSave(Parse.User, function(request) {
    if(!(request.object.existed())){
      //Not sure what anonymous user would return here...
    }
});

Upvotes: 0

Related Questions