Skip to content

Commit ca8bcd9

Browse files
committed
Cleaner implementation of getting auth
1 parent 055c768 commit ca8bcd9

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/LiveQuery/ParseLiveQueryServer.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SchemaController from '../Controllers/SchemaController';
1111
import _ from 'lodash';
1212
import uuid from 'uuid';
1313
import { runLiveQueryEventHandlers } from '../triggers';
14-
import { getAuthForSessionToken } from '../Auth';
14+
import { getAuthForSessionToken, Auth } from '../Auth';
1515
import { getCacheController } from '../Controllers';
1616

1717
class ParseLiveQueryServer {
@@ -333,11 +333,12 @@ class ParseLiveQueryServer {
333333
return matchesQuery(parseObject, subscription.query);
334334
}
335335

336-
async getUserId(sessionToken: ?string): ?string {
336+
async getAuthForSessionToken(sessionToken: ?string): { auth: ?Auth, userId: ?string } {
337337
try {
338338
const auth = await getAuthForSessionToken({ cacheController: this.cacheController, sessionToken: sessionToken });
339-
return auth && auth.user && auth.user.id; // return the ID of the found user
339+
return { auth, userId: auth && auth.user && auth.user.id }// return the ID of the found user
340340
} catch(e) { /* ignore errors */ }
341+
return {};
341342
}
342343

343344
async _matchesCLP(classLevelPermissions: ?any, object: any, client: any, requestId: number, op: string): any {
@@ -348,7 +349,7 @@ class ParseLiveQueryServer {
348349
}
349350
const subscriptionSessionToken = subscriptionInfo.sessionToken;
350351
const aclGroup = ['*'];
351-
const userId = await this.getUserId(subscriptionSessionToken);
352+
const { userId } = await this.getAuthForSessionToken(subscriptionSessionToken);
352353
if (userId) {
353354
aclGroup.push(userId);
354355
}
@@ -391,7 +392,7 @@ class ParseLiveQueryServer {
391392

392393
const subscriptionSessionToken = subscriptionInfo.sessionToken;
393394
// TODO: get auth there and de-duplicate code below to work with the same Auth obj.
394-
const userId = await this.getUserId(subscriptionSessionToken);
395+
const { auth, userId } = await this.getAuthForSessionToken(subscriptionSessionToken);
395396
const isSubscriptionSessionTokenMatched = acl.getReadAccess(userId);
396397
if (isSubscriptionSessionTokenMatched) {
397398
return Promise.resolve(true);
@@ -406,7 +407,6 @@ class ParseLiveQueryServer {
406407
return false;
407408
}
408409

409-
const auth = await getAuthForSessionToken({ cacheController: this.cacheController, sessionToken: subscriptionSessionToken });
410410
const roleNames = await auth.getUserRoles();
411411
// Finally, see if any of the user's roles allow them read access
412412
for (const role of roleNames) {
@@ -425,7 +425,7 @@ class ParseLiveQueryServer {
425425
// Check client sessionToken matches ACL
426426
const clientSessionToken = client.sessionToken;
427427
if (clientSessionToken) {
428-
const userId = await this.getUserId(clientSessionToken);
428+
const { userId } = await this.getAuthForSessionToken(clientSessionToken);
429429
return acl.getReadAccess(userId);
430430
} else {
431431
return isRoleMatched;

0 commit comments

Comments
 (0)