Skip to content

Commit 839a117

Browse files
authored
Fixes #2885 duplicate sessions (#4143)
* Adds test to repro the issue * Improved test * Destroy duplicate sessions for User/Installation-id pair - Sessions will also be created with action login instead of signup when using 3rd party auth
1 parent bc3cef2 commit 839a117

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

spec/ParseUser.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,36 @@ describe('Parse.User testing', () => {
11681168
});
11691169
});
11701170

1171+
it('only creates a single session for an installation / user pair (#2885)', done => {
1172+
Parse.Object.disableSingleInstance();
1173+
const provider = getMockFacebookProvider();
1174+
Parse.User._registerAuthenticationProvider(provider);
1175+
Parse.User.logInWith('facebook', {
1176+
success: () => {
1177+
return Parse.User.logInWith('facebook', {
1178+
success: () => {
1179+
return Parse.User.logInWith('facebook', {
1180+
success: (user) => {
1181+
const sessionToken = user.getSessionToken();
1182+
const query = new Parse.Query('_Session');
1183+
return query.find({ useMasterKey: true })
1184+
.then((results) => {
1185+
expect(results.length).toBe(1);
1186+
expect(results[0].get('sessionToken')).toBe(sessionToken);
1187+
expect(results[0].get('createdWith')).toEqual({
1188+
action: 'login',
1189+
authProvider: 'facebook'
1190+
});
1191+
done();
1192+
}).catch(done.fail);
1193+
}
1194+
});
1195+
}
1196+
});
1197+
}
1198+
});
1199+
});
1200+
11711201
it('log in with provider with files', done => {
11721202
const provider = getMockFacebookProvider();
11731203
Parse.User._registerAuthenticationProvider(provider);

src/RestWrite.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ RestWrite.prototype.createSessionToken = function() {
568568
objectId: this.objectId()
569569
},
570570
createdWith: {
571-
'action': 'signup',
571+
'action': this.storage['authProvider'] ? 'login' : 'signup',
572572
'authProvider': this.storage['authProvider'] || 'password'
573573
},
574574
restricted: false,
@@ -578,8 +578,18 @@ RestWrite.prototype.createSessionToken = function() {
578578
if (this.response && this.response.response) {
579579
this.response.response.sessionToken = token;
580580
}
581-
var create = new RestWrite(this.config, Auth.master(this.config), '_Session', null, sessionData);
582-
return create.execute();
581+
582+
// Destroy the sessions in 'Background'
583+
this.config.database.destroy('_Session', {
584+
user: {
585+
__type: 'Pointer',
586+
className: '_User',
587+
objectId: this.objectId()
588+
},
589+
installationId: this.auth.installationId,
590+
sessionToken: { '$ne': token },
591+
});
592+
return new RestWrite(this.config, Auth.master(this.config), '_Session', null, sessionData).execute();
583593
}
584594

585595
// Handles any followup logic

0 commit comments

Comments
 (0)