Skip to content

Commit 9a1a7d8

Browse files
committed
Factor sessionDestruction as part of Session creation flow in RestWrite
1 parent 93623c5 commit 9a1a7d8

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/RestWrite.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ RestWrite.prototype.execute = function() {
8181
return this.transformUser();
8282
}).then(() => {
8383
return this.expandFilesForExistingObjects();
84+
}).then(() => {
85+
return this.destroyDuplicatedSessions();
8486
}).then(() => {
8587
return this.runDatabaseOperation();
8688
}).then(() => {
@@ -588,17 +590,31 @@ RestWrite.prototype.createSessionToken = function() {
588590
this.response.response.sessionToken = token;
589591
}
590592

593+
return new RestWrite(this.config, Auth.master(this.config), '_Session', null, sessionData).execute();
594+
}
595+
596+
RestWrite.prototype.destroyDuplicatedSessions = function() {
597+
// Only for _Session, and at creation time
598+
if (this.className != '_Session' || this.query) {
599+
return;
600+
}
591601
// Destroy the sessions in 'Background'
602+
const {
603+
user,
604+
installationId,
605+
sessionToken,
606+
} = this.data;
607+
if (!user || !installationId) {
608+
return;
609+
}
610+
if (!user.objectId) {
611+
return;
612+
}
592613
this.config.database.destroy('_Session', {
593-
user: {
594-
__type: 'Pointer',
595-
className: '_User',
596-
objectId: this.objectId()
597-
},
598-
installationId: this.auth.installationId,
599-
sessionToken: { '$ne': token },
614+
user,
615+
installationId,
616+
sessionToken: { '$ne': sessionToken },
600617
});
601-
return new RestWrite(this.config, Auth.master(this.config), '_Session', null, sessionData).execute();
602618
}
603619

604620
// Handles any followup logic

0 commit comments

Comments
 (0)