Skip to content

Commit 1b8e613

Browse files
committed
Clean authData of null values on _User update
Adds a step to the RestWrite#execute chain: it cleans the response authData object of null values. For example, this: {"authData": {"anonymous": null}, "updatedAt", ...} will be transformed to this: {"updatedAt", ...} And this: {"authData": {"anonymous": null, "twitter": ...}, "updatedAt", ...} will be transformed to this: {"authData": {"twitter": ...}, "updatedAt", ...} Fixing this issue will fix anonymous user upgrades from the Android SDK.
1 parent f259a3b commit 1b8e613

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

spec/ParseUser.spec.js

+59
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,65 @@ describe('Parse.User testing', () => {
20612061
})
20622062
});
20632063

2064+
// https://github.com/ParsePlatform/parse-server/issues/1198
2065+
it('should cleanup null authData keys ParseUser update', (done) => {
2066+
Parse.Cloud.beforeSave('_User', (req, res) => {
2067+
req.object.set('foo', 'bar');
2068+
res.success();
2069+
});
2070+
2071+
// Simulate anonymous user save
2072+
new Promise((resolve, reject) => {
2073+
request.post({
2074+
url: 'http://localhost:8378/1/classes/_User',
2075+
headers: {
2076+
'X-Parse-Application-Id': Parse.applicationId,
2077+
'X-Parse-REST-API-Key': 'rest',
2078+
},
2079+
json: {authData: {anonymous: {id: '00000000-0000-0000-0000-000000000001'}}}
2080+
}, (err, res, body) => {
2081+
if (err) {
2082+
reject(err);
2083+
} else {
2084+
resolve(body);
2085+
}
2086+
});
2087+
}).then((user) => {
2088+
// Simulate registration
2089+
return new Promise((resolve, reject) => {
2090+
request.put({
2091+
url: 'http://localhost:8378/1/classes/_User/' + user.objectId,
2092+
headers: {
2093+
'X-Parse-Application-Id': Parse.applicationId,
2094+
'X-Parse-Session-Token': user.sessionToken,
2095+
'X-Parse-REST-API-Key': 'rest',
2096+
},
2097+
json: {
2098+
authData: {anonymous: null},
2099+
user: 'user',
2100+
password: 'password',
2101+
}
2102+
}, (err, res, body) => {
2103+
if (err) {
2104+
reject(err);
2105+
} else {
2106+
resolve(body);
2107+
}
2108+
});
2109+
});
2110+
}).then((user) => {
2111+
expect(typeof user).toEqual('object');
2112+
expect(user.authData).toBeUndefined();
2113+
Parse.Cloud._removeHook('Triggers', 'beforeSave', '_User');
2114+
done();
2115+
}).catch((err) => {
2116+
fail('no request should fail: ' + JSON.stringify(err));
2117+
Parse.Cloud._removeHook('Triggers', 'beforeSave', '_User');
2118+
done();
2119+
});
2120+
});
2121+
2122+
20642123
it('should aftersave with full object', (done) => {
20652124
var hit = 0;
20662125
Parse.Cloud.afterSave('_User', (req, res) => {

src/RestWrite.js

+18
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ RestWrite.prototype.execute = function() {
8383
return this.handleFollowup();
8484
}).then(() => {
8585
return this.runAfterTrigger();
86+
}).then(() => {
87+
return this.cleanUserAuthData();
8688
}).then(() => {
8789
return this.response;
8890
});
@@ -824,5 +826,21 @@ RestWrite.prototype.sanitizedData = function() {
824826
return Parse._decode(undefined, data);
825827
}
826828

829+
RestWrite.prototype.cleanUserAuthData = function() {
830+
if (this.response && this.response.response && this.className === '_User') {
831+
let user = this.response.response;
832+
if (user.authData) {
833+
Object.keys(user.authData).forEach((provider) => {
834+
if (user.authData[provider] === null) {
835+
delete user.authData[provider];
836+
}
837+
});
838+
if (Object.keys(user.authData).length == 0) {
839+
delete user.authData;
840+
}
841+
}
842+
}
843+
};
844+
827845
export default RestWrite;
828846
module.exports = RestWrite;

0 commit comments

Comments
 (0)