Skip to content

Commit 2e1ba66

Browse files
authored
Fix/user update issue (#4123)
* Adds failing test, the _User object is not updated as soon as you pass some authData part of the PUT * Do not run the DB call when updating the user with new auth data, just part of the rest
1 parent a103871 commit 2e1ba66

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

spec/ParseUser.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,4 +3116,58 @@ describe('Parse.User testing', () => {
31163116
});
31173117
});
31183118
});
3119+
3120+
it('should be able to update user with authData passed', (done) => {
3121+
let objectId;
3122+
let sessionToken;
3123+
3124+
function validate(block) {
3125+
return rp.get({
3126+
url: `http://localhost:8378/1/classes/_User/${objectId}`,
3127+
headers: {
3128+
'X-Parse-Application-Id': Parse.applicationId,
3129+
'X-Parse-REST-API-Key': 'rest',
3130+
'X-Parse-Session-Token': sessionToken
3131+
},
3132+
json: true,
3133+
}).then(block);
3134+
}
3135+
3136+
rp.post({
3137+
url: 'http://localhost:8378/1/classes/_User',
3138+
headers: {
3139+
'X-Parse-Application-Id': Parse.applicationId,
3140+
'X-Parse-REST-API-Key': 'rest',
3141+
},
3142+
json: { key: "value", authData: {anonymous: {id: '00000000-0000-0000-0000-000000000001'}}}
3143+
}).then((body) => {
3144+
objectId = body.objectId;
3145+
sessionToken = body.sessionToken;
3146+
expect(sessionToken).toBeDefined();
3147+
expect(objectId).toBeDefined();
3148+
return validate((user) => { // validate that keys are set on creation
3149+
expect(user.key).toBe("value");
3150+
});
3151+
}).then(() => {
3152+
// update the user
3153+
const options = {
3154+
url: `http://localhost:8378/1/classes/_User/${objectId}`,
3155+
headers: {
3156+
'X-Parse-Application-Id': Parse.applicationId,
3157+
'X-Parse-REST-API-Key': 'rest',
3158+
'X-Parse-Session-Token': sessionToken
3159+
},
3160+
json: { key: "otherValue", authData: {anonymous: {id: '00000000-0000-0000-0000-000000000001'}}}
3161+
}
3162+
return rp.put(options);
3163+
}).then(() => {
3164+
return validate((user) => { // validate that keys are set on update
3165+
expect(user.key).toBe("otherValue");
3166+
});
3167+
}).then(() => {
3168+
done();
3169+
})
3170+
.then(done)
3171+
.catch(done.fail);
3172+
});
31193173
});

src/RestWrite.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,12 @@ RestWrite.prototype.handleAuthData = function(authData) {
311311
// need to set the objectId first otherwise location has trailing undefined
312312
this.data.objectId = userResult.objectId;
313313

314-
// Determine if authData was updated
315-
316-
this.response = {
317-
response: userResult,
318-
location: this.location()
319-
};
320-
314+
if (!this.query || !this.query.objectId) { // this a login call, no userId passed
315+
this.response = {
316+
response: userResult,
317+
location: this.location()
318+
};
319+
}
321320
// If we didn't change the auth data, just keep going
322321
if (!hasMutatedAuthData) {
323322
return;
@@ -327,13 +326,20 @@ RestWrite.prototype.handleAuthData = function(authData) {
327326
// We should update the token and let the user in
328327
// We should only check the mutated keys
329328
return this.handleAuthDataValidation(mutatedAuthData).then(() => {
330-
// Assign the new authData in the response
331-
Object.keys(mutatedAuthData).forEach((provider) => {
332-
this.response.response.authData[provider] = mutatedAuthData[provider];
333-
});
334-
// Run the DB update directly, as 'master'
335-
// Just update the authData part
336-
return this.config.database.update(this.className, {objectId: this.data.objectId}, {authData: mutatedAuthData}, {});
329+
// IF we have a response, we'll skip the database operation / beforeSave / afterSave etc...
330+
// we need to set it up there.
331+
// We are supposed to have a response only on LOGIN with authData, so we skip those
332+
// If we're not logging in, but just updating the current user, we can safely skip that part
333+
if (this.response) {
334+
// Assign the new authData in the response
335+
Object.keys(mutatedAuthData).forEach((provider) => {
336+
this.response.response.authData[provider] = mutatedAuthData[provider];
337+
});
338+
// Run the DB update directly, as 'master'
339+
// Just update the authData part
340+
// Then we're good for the user, early exit of sorts
341+
return this.config.database.update(this.className, {objectId: this.data.objectId}, {authData: mutatedAuthData}, {});
342+
}
337343
});
338344
} else if (userId) {
339345
// Trying to update auth data but users

0 commit comments

Comments
 (0)