Skip to content

Commit 6abfac3

Browse files
authored
Fix beforeLogin trigger when user has a file (parse-community#6001)
* Fix beforeLogin trigger when user has a file * Add test case
1 parent 3d2d6d1 commit 6abfac3

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

spec/CloudCode.spec.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,9 +1546,7 @@ describe('Cloud Code', () => {
15461546

15471547
request({
15481548
method: 'POST',
1549-
url: `http://${Parse.applicationId}:${
1550-
Parse.masterKey
1551-
}@localhost:8378/1/jobs/myJob`,
1549+
url: `http://${Parse.applicationId}:${Parse.masterKey}@localhost:8378/1/jobs/myJob`,
15521550
}).then(
15531551
() => {},
15541552
err => {
@@ -2383,6 +2381,31 @@ describe('beforeLogin hook', () => {
23832381
done();
23842382
});
23852383

2384+
it('should be able to block login if an error is thrown even if the user has a attached file', async done => {
2385+
let hit = 0;
2386+
Parse.Cloud.beforeLogin(req => {
2387+
hit++;
2388+
if (req.object.get('isBanned')) {
2389+
throw new Error('banned account');
2390+
}
2391+
});
2392+
2393+
const user = await Parse.User.signUp('tupac', 'shakur');
2394+
const base64 = 'V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=';
2395+
const file = new Parse.File('myfile.txt', { base64 });
2396+
await file.save();
2397+
await user.save({ isBanned: true, file });
2398+
2399+
try {
2400+
await Parse.User.logIn('tupac', 'shakur');
2401+
throw new Error('should not have been logged in.');
2402+
} catch (e) {
2403+
expect(e.message).toBe('banned account');
2404+
}
2405+
expect(hit).toBe(1);
2406+
done();
2407+
});
2408+
23862409
it('should not run beforeLogin with incorrect credentials', async done => {
23872410
let hit = 0;
23882411
Parse.Cloud.beforeLogin(req => {

src/Routers/UsersRouter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ export class UsersRouter extends ClassesRouter {
241241
// Remove hidden properties.
242242
UsersRouter.removeHiddenProperties(user);
243243

244+
req.config.filesController.expandFilesInObject(req.config, user);
245+
244246
// Before login trigger; throws if failure
245247
await maybeRunTrigger(
246248
TriggerTypes.beforeLogin,
@@ -261,8 +263,6 @@ export class UsersRouter extends ClassesRouter {
261263

262264
user.sessionToken = sessionData.sessionToken;
263265

264-
req.config.filesController.expandFilesInObject(req.config, user);
265-
266266
await createSession();
267267
return { response: user };
268268
}

0 commit comments

Comments
 (0)