Skip to content

Commit 59b4047

Browse files
committed
Defers the session creation after DB operation (#1561)
1 parent 54b21c2 commit 59b4047

File tree

2 files changed

+60
-32
lines changed

2 files changed

+60
-32
lines changed

spec/ParseUser.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,31 @@ describe('Parse.User testing', () => {
22722272
}
22732273
});
22742274
});
2275+
2276+
it('should not create extraneous session tokens', (done) => {
2277+
let config = new Config(Parse.applicationId);
2278+
config.database.loadSchema().then((s) => {
2279+
// Lock down the _User class for creation
2280+
return s.addClassIfNotExists('_User', {}, {create: {}})
2281+
}).then((res) => {
2282+
let user = new Parse.User();
2283+
return user.save({'username': 'user', 'password': 'pass'});
2284+
}).then(() => {
2285+
fail('should not be able to save the user');
2286+
}, (err) => {
2287+
return Promise.resolve();
2288+
}).then(() => {
2289+
let q = new Parse.Query('_Session');
2290+
return q.find({useMasterKey: true})
2291+
}).then((res) => {
2292+
// We should have no session created
2293+
expect(res.length).toBe(0);
2294+
done();
2295+
}, (err) => {
2296+
fail('should not fail');
2297+
done();
2298+
});
2299+
});
22752300

22762301
it('should not overwrite username when unlinking facebook user (regression test for #1532)', done => {
22772302
Parse.Object.disableSingleInstance();

src/RestWrite.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ RestWrite.prototype.execute = function() {
7979
return this.expandFilesForExistingObjects();
8080
}).then(() => {
8181
return this.runDatabaseOperation();
82+
}).then(() => {
83+
return this.createSessionTokenIfNeeded();
8284
}).then(() => {
8385
return this.handleFollowup();
8486
}).then(() => {
@@ -316,35 +318,6 @@ RestWrite.prototype.transformUser = function() {
316318

317319
var promise = Promise.resolve();
318320

319-
if (!this.query) {
320-
var token = 'r:' + cryptoUtils.newToken();
321-
this.storage['token'] = token;
322-
promise = promise.then(() => {
323-
var expiresAt = this.config.generateSessionExpiresAt();
324-
var sessionData = {
325-
sessionToken: token,
326-
user: {
327-
__type: 'Pointer',
328-
className: '_User',
329-
objectId: this.objectId()
330-
},
331-
createdWith: {
332-
'action': 'signup',
333-
'authProvider': this.storage['authProvider'] || 'password'
334-
},
335-
restricted: false,
336-
installationId: this.auth.installationId,
337-
expiresAt: Parse._encode(expiresAt)
338-
};
339-
if (this.response && this.response.response) {
340-
this.response.response.sessionToken = token;
341-
}
342-
var create = new RestWrite(this.config, Auth.master(this.config),
343-
'_Session', null, sessionData);
344-
return create.execute();
345-
});
346-
}
347-
348321
// If we're updating a _User object, clear the user cache for the session
349322
if (this.query && this.auth.user && this.auth.user.getSessionToken()) {
350323
cache.users.remove(this.auth.user.getSessionToken());
@@ -412,6 +385,39 @@ RestWrite.prototype.transformUser = function() {
412385
});
413386
};
414387

388+
RestWrite.prototype.createSessionTokenIfNeeded = function() {
389+
if (this.className !== '_User') {
390+
return;
391+
}
392+
if (this.query) {
393+
return;
394+
}
395+
var token = 'r:' + cryptoUtils.newToken();
396+
397+
var expiresAt = this.config.generateSessionExpiresAt();
398+
var sessionData = {
399+
sessionToken: token,
400+
user: {
401+
__type: 'Pointer',
402+
className: '_User',
403+
objectId: this.objectId()
404+
},
405+
createdWith: {
406+
'action': 'signup',
407+
'authProvider': this.storage['authProvider'] || 'password'
408+
},
409+
restricted: false,
410+
installationId: this.auth.installationId,
411+
expiresAt: Parse._encode(expiresAt)
412+
};
413+
if (this.response && this.response.response) {
414+
this.response.response.sessionToken = token;
415+
}
416+
var create = new RestWrite(this.config, Auth.master(this.config),
417+
'_Session', null, sessionData);
418+
return create.execute();
419+
}
420+
415421
// Handles any followup logic
416422
RestWrite.prototype.handleFollowup = function() {
417423

@@ -775,9 +781,6 @@ RestWrite.prototype.runDatabaseOperation = function() {
775781
return memo;
776782
}, resp);
777783
}
778-
if (this.storage['token']) {
779-
resp.sessionToken = this.storage['token'];
780-
}
781784
this.response = {
782785
status: 201,
783786
response: resp,

0 commit comments

Comments
 (0)