Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix beforeLogin trigger when user has a file #6001

Merged
merged 2 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1546,9 +1546,7 @@ describe('Cloud Code', () => {

request({
method: 'POST',
url: `http://${Parse.applicationId}:${
Parse.masterKey
}@localhost:8378/1/jobs/myJob`,
url: `http://${Parse.applicationId}:${Parse.masterKey}@localhost:8378/1/jobs/myJob`,
}).then(
() => {},
err => {
Expand Down Expand Up @@ -2383,6 +2381,31 @@ describe('beforeLogin hook', () => {
done();
});

it('should be able to block login if an error is thrown even if the user has a attached file', async done => {
let hit = 0;
Parse.Cloud.beforeLogin(req => {
hit++;
if (req.object.get('isBanned')) {
throw new Error('banned account');
}
});

const user = await Parse.User.signUp('tupac', 'shakur');
const base64 = 'V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=';
const file = new Parse.File('myfile.txt', { base64 });
await file.save();
await user.save({ isBanned: true, file });

try {
await Parse.User.logIn('tupac', 'shakur');
throw new Error('should not have been logged in.');
} catch (e) {
expect(e.message).toBe('banned account');
}
expect(hit).toBe(1);
done();
});

it('should not run beforeLogin with incorrect credentials', async done => {
let hit = 0;
Parse.Cloud.beforeLogin(req => {
Expand Down
4 changes: 2 additions & 2 deletions src/Routers/UsersRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ export class UsersRouter extends ClassesRouter {
// Remove hidden properties.
UsersRouter.removeHiddenProperties(user);

req.config.filesController.expandFilesInObject(req.config, user);

// Before login trigger; throws if failure
await maybeRunTrigger(
TriggerTypes.beforeLogin,
Expand All @@ -261,8 +263,6 @@ export class UsersRouter extends ClassesRouter {

user.sessionToken = sessionData.sessionToken;

req.config.filesController.expandFilesInObject(req.config, user);

await createSession();
return { response: user };
}
Expand Down