Skip to content

Error Occurring Using Parse.Cloud.beforeLogin #5998

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

Closed
KAIOAVILA opened this issue Aug 29, 2019 · 4 comments · Fixed by #6001
Closed

Error Occurring Using Parse.Cloud.beforeLogin #5998

KAIOAVILA opened this issue Aug 29, 2019 · 4 comments · Fixed by #6001
Labels
type:bug Impaired feature or lacking behavior that is likely assumed

Comments

@KAIOAVILA
Copy link

KAIOAVILA commented Aug 29, 2019

Issue Description

I'm implementing a check to make sure the user can sign in to the app, but an error occurs while trying to log in to the app.

For the error to occur, the class User must have a column with file and the record must have some file, if null works normally

Steps to reproduce

Cloud implementation code snippet

Parse.Cloud.beforeLogin(async request => {
    const { object: user }  = request;
    if(user.get('deletedAt')) {
        throw 'Access denied, you have been banned.';
    }
    
});

3|AP | Error: Tried to encode an unsaved file. 3|AP | at encode (/home/user/api/parse-api/node_modules/parse/lib/node/encode.js:73:13) 3|AP | at _default (/home/user/api/parse-api/node_modules/parse/lib/node/encode.js:126:10) 3| AP | at ParseUser.toJSON (/home/user/api/parse-api/node_modules/parse/lib/node/ParseObject.js:578:42) 3| AP | at object (/home/user/api/parse-api/node_modules/parse-server/src/triggers.js:540:23) 3| AP | at success (/home/user/api/parse-api/node_modules/parse-server/src/triggers.js:273:14) 3|AP | at <anonymous>

I was checking and an error is occurring on the user who has a file and when doing the code he can't pass this point

Captura de Tela 2019-08-29 às 15 28 12

@davimacedo davimacedo added type:bug Impaired feature or lacking behavior that is likely assumed pr submitted labels Aug 30, 2019
@davimacedo
Copy link
Member

Thanks for reporting. This PR should solve this issue: https://github.com/parse-community/parse-server/pull/6001/files

@kvnkuang
Copy link
Contributor

kvnkuang commented Aug 20, 2020

Just want to report another issue surrounding this.

When logging in with an Auth provider, the same error occurs if that user has one or more files associated.

I spent some time investigating this problem and found that this error was triggered by this block of code:

logTriggerSuccessBeforeHook(
triggerType,
'Parse.File',
{ ...fileObject.file.toJSON(), fileSize: fileObject.fileSize },
result,
auth
);
return result || fileObject;
} catch (error) {
logTriggerErrorBeforeHook(
triggerType,
'Parse.File',
{ ...fileObject.file.toJSON(), fileSize: fileObject.fileSize },
auth,
error
);

Specifically, when encoding a file object with toJSON(), because the object was not expanded, it has no url property and hence cause this block of code to throw an error:

  if (value instanceof ParseFile) {
    if (!value.url()) {
      throw new Error('Tried to encode an unsaved file.');
    }
    return value.toJSON();
  }

Similar to the fix introduced previously (#6001), all we need is to expand file objects before passing them to the trigger runBeforeLoginTrigger in RestWrite.js:

  // Cloud code gets a bit of extra data for its objects
  const extraData = { className: this.className };

  // PROPOSED FIX: expand file objects
  this.config.filesController.expandFilesInObject(this.config, userData)
  
  const user = triggers.inflate(extraData, userData);

  // no need to return a response
  await triggers.maybeRunTrigger(
    triggers.Types.beforeLogin,
    this.auth,
    user,
    null,
    this.config,
    this.context
  );

I will prepare a pull request with the proposed fix.

@dplewis
Copy link
Member

dplewis commented Aug 20, 2020

@kvnkuang Can you open a new issue? We don't want it to get lost.

@kvnkuang
Copy link
Contributor

Thanks! Issue created #6871.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Impaired feature or lacking behavior that is likely assumed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants