Skip to content

Commit 18ea2bd

Browse files
committed
removes key transformation for authData from restWrite, ensures authData is set in hooks
1 parent bcffcba commit 18ea2bd

File tree

3 files changed

+74
-14
lines changed

3 files changed

+74
-14
lines changed

spec/ParseUser.spec.js

+37
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,43 @@ describe('Parse.User testing', () => {
14361436
}
14371437
});
14381438
});
1439+
1440+
it('should have authData in beforeSave and afterSave', (done) => {
1441+
1442+
Parse.Cloud.beforeSave('_User', (request, response) => {
1443+
let authData = request.object.get('authData');
1444+
expect(authData).not.toBeUndefined();
1445+
if (authData) {
1446+
expect(authData.facebook.id).toEqual('8675309');
1447+
expect(authData.facebook.access_token).toEqual('jenny');
1448+
} else {
1449+
fail('authData should be set');
1450+
}
1451+
response.success();
1452+
});
1453+
1454+
Parse.Cloud.afterSave('_User', (request, response) => {
1455+
let authData = request.object.get('authData');
1456+
expect(authData).not.toBeUndefined();
1457+
if (authData) {
1458+
expect(authData.facebook.id).toEqual('8675309');
1459+
expect(authData.facebook.access_token).toEqual('jenny');
1460+
} else {
1461+
fail('authData should be set');
1462+
}
1463+
response.success();
1464+
});
1465+
1466+
var provider = getMockFacebookProvider();
1467+
Parse.User._registerAuthenticationProvider(provider);
1468+
Parse.User._logInWith("facebook", {
1469+
success: function(model) {
1470+
Parse.Cloud._removeHook('Triggers', 'beforeSave', Parse.User.className);
1471+
Parse.Cloud._removeHook('Triggers', 'afterSave', Parse.User.className);
1472+
done();
1473+
}
1474+
});
1475+
});
14391476

14401477
it('set password then change password', (done) => {
14411478
Parse.User.signUp('bob', 'barker').then((bob) => {

src/RestWrite.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,9 @@ RestWrite.prototype.handleAuthData = function(authData) {
274274
throw new Parse.Error(Parse.Error.ACCOUNT_ALREADY_LINKED,
275275
'this auth is already used');
276276
}
277-
// set the proper keys
278-
Object.keys(authData).forEach((provider) => {
279-
this.data[`_auth_data_${provider}`] = authData[provider];
280-
});
281-
277+
278+
this.storage['authProvider'] = Object.keys(authData).join(',');
279+
282280
if (results.length == 0) {
283281
this.data.username = cryptoUtils.newToken();
284282
} else if (!this.query) {
@@ -294,9 +292,6 @@ RestWrite.prototype.handleAuthData = function(authData) {
294292
// Trying to update auth data but users
295293
// are different
296294
if (results[0].objectId !== this.query.objectId) {
297-
Object.keys(authData).forEach((provider) => {
298-
delete this.data[`_auth_data_${provider}`];
299-
});
300295
throw new Parse.Error(Parse.Error.ACCOUNT_ALREADY_LINKED,
301296
'this auth is already used');
302297
}
@@ -708,10 +703,6 @@ RestWrite.prototype.runDatabaseOperation = function() {
708703
if (this.data.ACL && this.data.ACL['*unresolved']) {
709704
throw new Parse.Error(Parse.Error.INVALID_ACL, 'Invalid ACL.');
710705
}
711-
712-
if (this.className === '_User') {
713-
delete this.data.authData;
714-
}
715706

716707
if (this.query) {
717708
// Run an update
@@ -777,8 +768,23 @@ RestWrite.prototype.runAfterTrigger = function() {
777768
// Build the inflated object, different from beforeSave, originalData is not empty
778769
// since developers can change data in the beforeSave.
779770
let updatedObject = triggers.inflate(extraData, this.originalData);
780-
updatedObject.set(Parse._decode(undefined, this.data));
771+
let decodedData = Parse._decode(undefined, this.data);
772+
773+
// de-transform the authData
774+
decodedData = Object.keys(decodedData).reduce((data, key) => {
775+
let result = key.match(/^_auth_data_([a-zA-Z0-9_]+)$/);
776+
if (result) {
777+
let provider = result[1];
778+
data.authData = data.authData || {};
779+
data.authData[provider] = data[key];
780+
delete data[key];
781+
}
782+
return data;
783+
}, decodedData);
784+
785+
updatedObject.set(decodedData);
781786
updatedObject._handleSaveResponse(this.response.response, this.response.status || 200);
787+
782788

783789
triggers.maybeRunTrigger(triggers.Types.afterSave, this.auth, updatedObject, originalObject, this.config.applicationId);
784790
};

src/transform.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function transformKeyValue(schema, className, restKey, restValue, options
8787
return transformWhere(schema, className, s);
8888
});
8989
return {key: '$and', value: mongoSubqueries};
90-
default:
90+
default:
9191
// Other auth data
9292
var authDataMatch = key.match(/^authData\.([a-zA-Z0-9_]+)\.id$/);
9393
if (authDataMatch) {
@@ -203,6 +203,9 @@ function transformWhere(schema, className, restWhere) {
203203
// restCreate is the "create" clause in REST API form.
204204
// Returns the mongo form of the object.
205205
function transformCreate(schema, className, restCreate) {
206+
if (className == '_User') {
207+
restCreate = transformAuthData(restCreate);
208+
}
206209
var mongoCreate = transformACL(restCreate);
207210
for (var restKey in restCreate) {
208211
var out = transformKeyValue(schema, className, restKey, restCreate[restKey]);
@@ -218,6 +221,10 @@ function transformUpdate(schema, className, restUpdate) {
218221
if (!restUpdate) {
219222
throw 'got empty restUpdate';
220223
}
224+
if (className == '_User') {
225+
restUpdate = transformAuthData(restUpdate);
226+
}
227+
221228
var mongoUpdate = {};
222229
var acl = transformACL(restUpdate);
223230
if (acl._rperm || acl._wperm) {
@@ -250,6 +257,16 @@ function transformUpdate(schema, className, restUpdate) {
250257
return mongoUpdate;
251258
}
252259

260+
function transformAuthData(restObject) {
261+
if (restObject.authData) {
262+
Object.keys(restObject.authData).forEach((provider) => {
263+
restObject[`_auth_data_${provider}`] = restObject.authData[provider];
264+
});
265+
delete restObject.authData;
266+
}
267+
return restObject;
268+
}
269+
253270
// Transforms a REST API formatted ACL object to our two-field mongo format.
254271
// This mutates the restObject passed in to remove the ACL key.
255272
function transformACL(restObject) {

0 commit comments

Comments
 (0)