parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

'unauthorized' error using 2.0.8

Open RadianSmile opened this issue 10 years ago • 23 comments

I run Parse server @2.0.8

then happened a strange thing :

I ran the sample code in node :

...
Parse = require ('parse/node');
var obj = new Parse.Object('GameScore');
obj.set('score',1337);
obj.save().then(function(obj) {
  console.log(obj.toJSON());
  var query = new Parse.Query('GameScore');
  query.get(obj.id).then(function(objAgain) {
    console.log(objAgain.toJSON());
  }, function(err) {console.log(err); });
}, function(err) { console.log(err); });
...

then my server log showed :

2016-02-14T11:54:01.166014+00:00 app[web.1]: { score: 1337,
2016-02-14T11:54:01.166021+00:00 app[web.1]:   createdAt: '2016-02-14T11:54:00.537Z',
2016-02-14T11:54:01.166022+00:00 app[web.1]:   updatedAt: '2016-02-14T11:54:00.537Z',
2016-02-14T11:54:01.166023+00:00 app[web.1]:   objectId: 'rzuNqFVFY0' }
2016-02-14T11:54:01.201652+00:00 app[web.1]: ParseError { code: undefined, message: 'unauthorized' }

that means it could create object , but not able to query.

that's so wired. So I test it with [email protected]

then this code runs smoothly :

2016-02-14T12:14:01.634098+00:00 app[web.1]: { score: 1337,
2016-02-14T12:14:01.634103+00:00 app[web.1]:   createdAt: '2016-02-14T12:14:00.587Z',
2016-02-14T12:14:01.634104+00:00 app[web.1]:   updatedAt: '2016-02-14T12:14:00.587Z',
2016-02-14T12:14:01.634106+00:00 app[web.1]:   objectId: '2MbFbROSAP' }
2016-02-14T12:14:01.685361+00:00 app[web.1]: { score: 1337,
2016-02-14T12:14:01.685366+00:00 app[web.1]:   updatedAt: '2016-02-14T12:14:00.587Z',
2016-02-14T12:14:01.685367+00:00 app[web.1]:   createdAt: '2016-02-14T12:14:00.587Z',
2016-02-14T12:14:01.685367+00:00 app[web.1]:   objectId: '2MbFbROSAP' }

Please check! Thanks.

RadianSmile avatar Feb 14 '16 12:02 RadianSmile

what's version of your mongo server?

henry40408 avatar Feb 14 '16 13:02 henry40408

I use heroku mongolab addon with sandbox plan : http://docs.mongolab.com/ops/#version-mgmt version is 3.0.x

RadianSmile avatar Feb 14 '16 14:02 RadianSmile

Have you tried initialising Parse with the JS key?

davide-scalzo avatar Feb 14 '16 16:02 davide-scalzo

Hm. You shouldn't have to use a JS key, as long as you don't create your parse server instance with one as option. Check your DB and look at the documents in your Schema collection. Is there anything strange under metadata.find property?

jmk2142 avatar Feb 14 '16 20:02 jmk2142

Where is this code running (cloud code or a separate node script) ? Was Parse initialized, was the serverURL set? If it's in cloud code, you should remove the require on Parse, as it is already a global.

gfosco avatar Feb 14 '16 20:02 gfosco

I have the same problem. And it is solved (at least for me) by including { useMasterKey: true } in each query. like this : var query = new Parse.Query(Parse.User); query.equalTo('objectId', request.user.id); query.first({ useMasterKey: true }).then(function(user) { .....

This is indicated here https://github.com/ParsePlatform/parse-server in the last paragraph under *Not Supported *

Parse.User.current() or Parse.Cloud.useMasterKey() in cloud code. Instead of Parse.User.current() use request.user and instead of Parse.Cloud.useMasterKey() pass useMasterKey: true to each query. To make queries and writes as a specific user within Cloud Code, you need the user's session token, which is available in request.user.getSessionToken().

But as far as i know this is not a solution at all. Since some Queries do not require the use of MasterKey.

Also, i was not able to Fetch the user even with a master key. The only way to retrieve the User was to query Parse.User with an ObjectId constraint. This did not work for me currentUser.fetch({ useMasterKey: true }).then(function(currentUser) { ...

polo2244 avatar Feb 14 '16 20:02 polo2244

@davodesign84 no js key @jmk2142 I use mongolab.com, sorry that I cant get what you said. where to find metadata.find property @gfosco please see the following details :

my Parse server config :

//# app.js 

// [email protected]

ParseServer = require('parse-server').ParseServer;
app.use(PARSE_MOUNT, new ParseServer({  // mount on '/1'
  databaseURI: DATABASE_URI,
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: 'id',
  masterKey: 'mkey'
}));

I ran these in node server , not parse cloud.

//#  app.js 

// this is my initialization
Parse = require ('parse/node');
Parse.initialize("id","");
Parse.serverURL = PARSE_SERVER_URL; 

// do request and then get unauthorized when query.
Parse = require ('parse/node'); // parse js sdk @1.6.14
var obj = new Parse.Object('GameScore');
obj.set('score',1337);
obj.save().then(function(obj) {
  console.log(obj.toJSON());
  var query = new Parse.Query('GameScore');
  query.get(obj.id).then(function(objAgain) {
    console.log(objAgain.toJSON());
  }, function(err) {console.log(err); });
}, function(err) { console.log(err); });

Another thing. I don't know is this related.

PARSE_SERVER_URL in the parse server example repo is https://xxx.herokuapp.com/ it works for me before(not sure). but I now would see this wrong result :

2016-02-14T12:04:48.823496+00:00 heroku[router]: at=info method=POST path="/classes/GameScore" host=nccu-ctld.herokuapp.com request_id=5c2626cd-143c-4082-b3de-c43cb4866a28 fwd="174.129.148.88" dyno=web.1 connect=2ms service=316ms status=404 bytes=1670
2016-02-14T12:04:48.827611+00:00 app[web.1]: POST /classes/GameScore 404 307.577 ms - 1433
2016-02-14T12:04:48.834256+00:00 app[web.1]: ParseError {
2016-02-14T12:04:48.834258+00:00 app[web.1]:   code: 107,
2016-02-14T12:04:48.834259+00:00 app[web.1]:   message: 'Received an error with invalid JSON from Parse: <!DOCTYPE html><html><head><title></title><link rel="stylesheet ... / whatever, it's 404 page.

The request went to '/' to do requests. Then I set parse_server_url to https://xxx.herokuapp.com/1 and get the right result.

RadianSmile avatar Feb 15 '16 05:02 RadianSmile

the following info maybe useful but strange too :

2016-02-14T11:54:01.160595+00:00 heroku[router]: at=info method=POST path="/1/classes/GameScore" host=xxx.herokuapp.com request_id=cc699c78-c645-43b8-b187-68d0d3717ff1 fwd="xx.x.x.xx" dyno=web.1 connect=1ms service=657ms status=201 bytes=468
2016-02-14T11:54:01.166014+00:00 app[web.1]: { score: 1337,
2016-02-14T11:54:01.166021+00:00 app[web.1]:   createdAt: '2016-02-14T11:54:00.537Z',
2016-02-14T11:54:01.166022+00:00 app[web.1]:   updatedAt: '2016-02-14T11:54:00.537Z',
2016-02-14T11:54:01.166023+00:00 app[web.1]:   objectId: 'rzuNqFVFY0' }
2016-02-14T11:54:01.201652+00:00 app[web.1]: ParseError { code: undefined, message: 'unauthorized' }

there should be two router log showing two request reaches/1/classes/GameScore , but there is only one router log (I have double checked). So it means ParseError happend before the request reach /1/classes/GameScore?! the problem seems is in request side (parse-js-sdk) but not server-side (parse-server). but my problem solved when server go back to 2.0.0 and Parse-js-sdk is always1.6.14.... why?

RadianSmile avatar Feb 15 '16 05:02 RadianSmile

@RadianSmile i have a question about using parse server version Parse server @2.0.8 I installed parse-server-example on Heroku using the Deploy button at (https://github.com/ParsePlatform/parse-server-example) and resulted with the first build 1.0.0

How did you update to the latest build 2.0.8 ?

It was pointed out to me to change the version here: `{ "name": "parse-server-example", "version": "1.0.0",.... ' inside package.jon to 2.0.8 and it should work. But i don't think this is the way to do it. Is there another way ?

polo2244 avatar Feb 15 '16 18:02 polo2244

@polo2244 It seems that you mistaken project version as module version. You should look at "dependencies" property in package.json. There goes "parse-server": "~2.0".

You should read the package.json as:

Your projcet "parse-server-example" is now 1.0.0 version (the version can be defined by yourself) and depend on parse-server Approximately at version 2.0

RadianSmile avatar Feb 15 '16 22:02 RadianSmile

@Radiansmile there should be a "table/collection" in MongoLab's data browser called schema. You'll find objects representing your other classes you produced. Basically you can set class permissions and such there including query permissions. Dig around a little and make sure you don't have anything weird in there. If you have a new app, with all class permissions open it'll basically be a bunch of empty objects. From the rest of this thread it seems this is probably not the culprit but check just to be sure. If something is funny there it will probably pop out at you.

jmk2142 avatar Feb 16 '16 22:02 jmk2142

@RadianSmile What serverURL are you using in the parse cloud (for the parse server)? You didn't include any in the config above, but if you are using heroku it should be something like https://heroku-app-id.herokuapp.com/parse.

simonbengtsson avatar Feb 17 '16 12:02 simonbengtsson

Make sure you use latest version of parse sdk.

eatentree avatar Feb 22 '16 11:02 eatentree

I am performing this but its not working

Parse.Cloud.afterSave("MyClass", function (request) {
    console.log("AFTER SAVE EXECUTED");
    var myToken = request.object;
    console.log(myToken)
    myToken.set("house", 852);
    myToken.save({
        useMasterKey: true,
        success: function(res){
            console.log(res)
        },
        error: function(err){
            console.log(err);
        }
    });
});

It prints the console log but it fails to change the object which have been triggered this afterSave().

Help needed ASAP.

mahabubakram avatar Feb 23 '16 14:02 mahabubakram

You should not save the same object as triggered the aftersave as that will create an infinite loop. You main problem is that the firat parameter in the save method is attributes, not options. Use myToken.save(null, {__options__});

simonbengtsson avatar Feb 23 '16 14:02 simonbengtsson

"null" added but still not working. Its giving this error:

it gives error status=201

mahabubakram avatar Feb 23 '16 14:02 mahabubakram

I haven't tried but I would imagine that saving the same object that triggered the afterSave (as you do above) would create an infinite loop. If you need to add something to an object, do it in beforeSave instead.

simonbengtsson avatar Feb 23 '16 15:02 simonbengtsson

But I used to do change the same object in the afterSave in my previous parse cloud. But right now the parse server is running in heroku and its creating this problem.

mahabubakram avatar Feb 23 '16 15:02 mahabubakram

@RadianSmile Do you still get the unauthorized error with version 2.1.3?

simonbengtsson avatar Feb 26 '16 00:02 simonbengtsson

@mahabubakram If you're having an issue with the latest version, please open your own issue and provide as much detail as possible, including code. Thanks.

gfosco avatar Feb 26 '16 15:02 gfosco