Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud Code save role on account creation not working #2710

Closed
gateway opened this issue Sep 13, 2016 · 1 comment
Closed

Cloud Code save role on account creation not working #2710

gateway opened this issue Sep 13, 2016 · 1 comment

Comments

@gateway
Copy link

gateway commented Sep 13, 2016

Please read the following instructions carefully.

Check out #1271 for an ideal bug report.
The closer your issue report is to that one, the more likely we are to be able to help, and the more likely we will be to fix the issue quickly!

Many members of the community use Stack Overflow and Server Fault to ask questions.
Read through the existing questions or ask your own!

For database migration help, please file a bug report at https://parse.com/help#report

Make sure these boxes are checked before submitting your issue -- thanks for reporting issues back to Parse Server!

Issue Description

When a user is created in our iOS app and our cloud code is executed we do not get the user added to the role.

We can clearly see that the user account is created in the parse dashboard, but not in the role.

Steps to reproduce

iOS swift Code

// Create that new user!
let user = User()
user.username = usernameTextfield.text
user.displayName = usernameTextfield.text
user.password = enterPasswordTextfield.text

user.signUpInBackgroundWithBlock({ (success, error) -> Void in

   if (success) {
     // hack to get it to work.
      user.currentBuild = "1"
      user.saveInBackgroundWithBlock({ (success, error) in
            dispatch_async(dispatch_get_main_queue()) {
               self.performSegueWithIdentifier("toOnboarding", sender: self)
            }
      })

   } else {

     // ERROR
   }
})

notice the hack comment, this is to save user in background with block again and this will then trigger the role to be created.

cloud code

Parse.Cloud.afterSave("_User", function(request, response) {
  // add user to NormalUser Role
  query = new Parse.Query(Parse.Role);
  query.equalTo("name", "NormalUser");
  query.first({ useMasterKey: true }).then(function(object) {  
      object.relation("users").add(request.user);
      object.save({ useMasterKey: true });
      response.success("The user has been authorized.");
    }, function(error) {
      response.error("user authorization failed");
    });
});

Environment Setup

  • Server
    • parse-server version (Be specific! Don't say 'latest'.) : 2.2.19
    • Operating System: Heroku
    • Hardware: 2 Heroku Standard XL
    • Localhost or remote server? Heroku
  • Database
    • MongoDB version: 3.2.x
    • Storage engine: WiredTiger
    • Hardware: M1 Cluster
    • Localhost or remote server? Mlab.

Logs/Trace

Heroku Logs

First time user is created w/o the hack part of the code.

2016-09-13T23:33:33.173405+00:00 app[web.1]: in here
2016-09-13T23:33:33.178617+00:00 app[web.1]: info: afterSave triggered for _User for user undefined:
2016-09-13T23:33:33.178620+00:00 app[web.1]:   Input: {"username":"cccccvvv","displayName":"cccccvvv","ACL":{"*":{"read":true,"write":false},"goOoxf25sU":{"read":true,"write":true}},"createdAt":"2016-09-13T23:33:32.974Z","sessionToken":"r:a8af92fd09055b3f43dbe146d05dbb42","updatedAt":"2016-09-13T23:33:32.974Z","objectId":"goOoxf25sU"} className=_User, triggerType=afterSave, **user=undefined**

When we enable the hack we see this above first happen, aka the first time cloud code is run.. not sure why user - undefined, notice the object is is in the json package.

Once we do an additional save we now get the user in the role..

2016-09-13T23:34:10.560202+00:00 app[web.1]: in here
2016-09-13T23:34:10.570458+00:00 app[web.1]: info: afterSave triggered for _User for user goOoxf25sU:
2016-09-13T23:34:10.570462+00:00 app[web.1]:   Input: {"username":"cccccvvv","displayName":"cccccvvv","createdAt":"2016-09-13T23:33:32.974Z","updatedAt":"2016-09-13T23:34:10.526Z","ACL":{"*":{"read":true},"goOoxf25sU":{"read":true,"write":true}},"currentBuild":"1","objectId":"goOoxf25sU"} className=_User, triggerType=afterSave, user=goOoxf25sU
2016-09-13T23:34:10.851163+00:00 heroku[router]: at=info method=POST path="/parse/classes/_Role" host=aaa-stage.com request_id=1421c63c-8ada-4af8-9792-ba254f0def29 fwd="1.1.1.1.1" dyno=web.1 connect=1ms service=257ms status=200 bytes=817
2016-09-13T23:34:10.870533+00:00 app[web.1]: authed
2016-09-13T23:34:10.872880+00:00 app[web.1]: info: afterSave triggered for _User for user goOoxf25sU:
2016-09-13T23:34:10.872882+00:00 app[web.1]:   Input: {"username":"cccccvvv","displayName":"cccccvvv","createdAt":"2016-09-13T23:33:32.974Z","updatedAt":"2016-09-13T23:34:10.526Z","ACL":{"*":{"read":true},"goOoxf25sU":{"read":true,"write":true}},"currentBuild":"1","objectId":"goOoxf25sU"}
2016-09-13T23:34:10.872883+00:00 app[web.1]:   Result: {} className=_User, triggerType=afterSave, user=goOoxf25sU
@gateway gateway changed the title Cloud Code Save Role on Account Creation Not working Cloud Code save role on account creation not working Sep 13, 2016
@flovilmart
Copy link
Contributor

You should probably use request.object as the user is not set as not authenticated when the first save is done. It can't be authenticated as there'S no session token on the client to authenticate it.

On subsequent saves, your request.user is set, as the call is authenticated by the session token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants