Skip to content

Commit a086924

Browse files
committed
fixed Auth-RestWrite circular dependency
1 parent ca22444 commit a086924

File tree

4 files changed

+38
-39
lines changed

4 files changed

+38
-39
lines changed

src/Auth.js

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -299,45 +299,11 @@ Auth.prototype._getAllRolesNamesForRoleIds = function (roleIDs, names = [], quer
299299
});
300300
};
301301

302-
const createSession = function (
303-
config,
304-
{ userId, createdWith, installationId, additionalSessionData }
305-
) {
306-
const token = 'r:' + cryptoUtils.newToken();
307-
const expiresAt = config.generateSessionExpiresAt();
308-
const sessionData = {
309-
sessionToken: token,
310-
user: {
311-
__type: 'Pointer',
312-
className: '_User',
313-
objectId: userId,
314-
},
315-
createdWith,
316-
restricted: false,
317-
expiresAt: Parse._encode(expiresAt),
318-
};
319-
320-
if (installationId) {
321-
sessionData.installationId = installationId;
322-
}
323-
324-
Object.assign(sessionData, additionalSessionData);
325-
// We need to import RestWrite at this point for the cyclic dependency it has to it
326-
const RestWrite = require('./RestWrite');
327-
328-
return {
329-
sessionData,
330-
createSession: () =>
331-
new RestWrite(config, master(config), '_Session', null, sessionData).execute(),
332-
};
333-
};
334-
335302
module.exports = {
336303
Auth,
337304
master,
338305
nobody,
339306
readOnly,
340307
getAuthForSessionToken,
341308
getAuthForLegacySessionToken,
342-
createSession,
343309
};

src/RestWrite.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ RestWrite.prototype.createSessionToken = async function () {
857857
return;
858858
}
859859

860-
const { sessionData, createSession } = Auth.createSession(this.config, {
860+
const { sessionData, createSession } = RestWrite.createSession(this.config, {
861861
userId: this.objectId(),
862862
createdWith: {
863863
action: this.storage['authProvider'] ? 'login' : 'signup',
@@ -873,6 +873,37 @@ RestWrite.prototype.createSessionToken = async function () {
873873
return createSession();
874874
};
875875

876+
RestWrite.createSession = function (
877+
config,
878+
{ userId, createdWith, installationId, additionalSessionData }
879+
) {
880+
const token = 'r:' + cryptoUtils.newToken();
881+
const expiresAt = config.generateSessionExpiresAt();
882+
const sessionData = {
883+
sessionToken: token,
884+
user: {
885+
__type: 'Pointer',
886+
className: '_User',
887+
objectId: userId,
888+
},
889+
createdWith,
890+
restricted: false,
891+
expiresAt: Parse._encode(expiresAt),
892+
};
893+
894+
if (installationId) {
895+
sessionData.installationId = installationId;
896+
}
897+
898+
Object.assign(sessionData, additionalSessionData);
899+
900+
return {
901+
sessionData,
902+
createSession: () =>
903+
new RestWrite(config, Auth.master(config), '_Session', null, sessionData).execute(),
904+
};
905+
};
906+
876907
// Delete email reset tokens if user is changing password or email.
877908
RestWrite.prototype.deleteEmailResetTokenIfNeeded = function () {
878909
if (this.className !== '_User' || this.query === null) {
@@ -978,7 +1009,7 @@ RestWrite.prototype.handleSession = function () {
9781009
additionalSessionData[key] = this.data[key];
9791010
}
9801011

981-
const { sessionData, createSession } = Auth.createSession(this.config, {
1012+
const { sessionData, createSession } = RestWrite.createSession(this.config, {
9821013
userId: this.auth.user.id,
9831014
createdWith: {
9841015
action: 'create',
@@ -1258,7 +1289,7 @@ RestWrite.prototype.handleInstallation = function () {
12581289
return promise;
12591290
};
12601291

1261-
// If we short-circuted the object response - then we need to make sure we expand all the files,
1292+
// If we short-circuited the object response - then we need to make sure we expand all the files,
12621293
// since this might not have a query, meaning it won't return the full result back.
12631294
// TODO: (nlutsenko) This should die when we move to per-class based controllers on _Session/_User
12641295
RestWrite.prototype.expandFilesForExistingObjects = function () {

src/Routers/SessionsRouter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ClassesRouter from './ClassesRouter';
22
import Parse from 'parse/node';
33
import rest from '../rest';
44
import Auth from '../Auth';
5+
import RestWrite from '../RestWrite';
56

67
export class SessionsRouter extends ClassesRouter {
78
className() {
@@ -41,7 +42,7 @@ export class SessionsRouter extends ClassesRouter {
4142
if (!user) {
4243
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'invalid session');
4344
}
44-
const { sessionData, createSession } = Auth.createSession(config, {
45+
const { sessionData, createSession } = RestWrite.createSession(config, {
4546
userId: user.id,
4647
createdWith: {
4748
action: 'upgrade',

src/Routers/UsersRouter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Auth from '../Auth';
99
import passwordCrypto from '../password';
1010
import { maybeRunTrigger, Types as TriggerTypes } from '../triggers';
1111
import { promiseEnsureIdempotency } from '../middlewares';
12+
import RestWrite from '../RestWrite';
1213

1314
export class UsersRouter extends ClassesRouter {
1415
className() {
@@ -218,7 +219,7 @@ export class UsersRouter extends ClassesRouter {
218219
req.config
219220
);
220221

221-
const { sessionData, createSession } = Auth.createSession(req.config, {
222+
const { sessionData, createSession } = RestWrite.createSession(req.config, {
222223
userId: user.objectId,
223224
createdWith: {
224225
action: 'login',

0 commit comments

Comments
 (0)