Skip to content

Fix/3451 duplicate session upon login #4337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions spec/ParseUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3453,4 +3453,34 @@ describe('Parse.User testing', () => {
done();
});
});

it('does not duplicate session when logging in multiple times #3451', (done) => {
const user = new Parse.User();
user.signUp({
username: 'yolo',
password: 'yolo',
email: '[email protected]'
}).then(() => {
const promises = [];
while(promises.length != 5) {
Parse.User.logIn('yolo', 'yolo')
promises.push(Parse.User.logIn('yolo', 'yolo').then((res) => {
// ensure a new session token is generated at each login
expect(res.getSessionToken()).not.toBe(user.getSessionToken());
}));
}
return Promise.all(promises);
}).then(() => {
// wait because session destruction is not synchronous
return new Promise((resolve) => {
setTimeout(resolve, 100);
});
}).then(() => {
const query = new Parse.Query('_Session');
return query.find({ useMasterKey: true });
}).then((results) => {
// only one session in the end
expect(results.length).toBe(1);
}).then(done, done.fail);
});
});
32 changes: 24 additions & 8 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ RestWrite.prototype.execute = function() {
return this.transformUser();
}).then(() => {
return this.expandFilesForExistingObjects();
}).then(() => {
return this.destroyDuplicatedSessions();
}).then(() => {
return this.runDatabaseOperation();
}).then(() => {
Expand Down Expand Up @@ -588,17 +590,31 @@ RestWrite.prototype.createSessionToken = function() {
this.response.response.sessionToken = token;
}

return new RestWrite(this.config, Auth.master(this.config), '_Session', null, sessionData).execute();
}

RestWrite.prototype.destroyDuplicatedSessions = function() {
// Only for _Session, and at creation time
if (this.className != '_Session' || this.query) {
return;
}
// Destroy the sessions in 'Background'
const {
user,
installationId,
sessionToken,
} = this.data;
if (!user || !installationId) {
return;
}
if (!user.objectId) {
return;
}
this.config.database.destroy('_Session', {
user: {
__type: 'Pointer',
className: '_User',
objectId: this.objectId()
},
installationId: this.auth.installationId,
sessionToken: { '$ne': token },
user,
installationId,
sessionToken: { '$ne': sessionToken },
});
return new RestWrite(this.config, Auth.master(this.config), '_Session', null, sessionData).execute();
}

// Handles any followup logic
Expand Down