parse-community / parse-community/parse-server
Error occurring using beforeLogin for users logging in with AuthData
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 21.4k
- Forks
- 4.8k
- Avg merge
- 7h 45m
- Merged PRs (30d)
- 11
Description
Issue Description
When logging in with AuthData, an error (similar to #5998) occurs if that user has one or more files associated.
If no files associated, beforeLogin can be executed without error.
Steps to reproduce
- Define a
beforeLogintrigger like the following:
Parse.Cloud.beforeLogin(async request => {
const { object: user } = request;
if(user.get('isBanned')) {
throw new Error('Access denied, you have been banned.')
}
});
- Log in with an Auth provider. No matter if the user has
isBannedset to true, the following error occurs.
UnhandledPromiseRejectionWarning: Error: Tried to encode an unsaved file.
at encode (C:\Users\kevin\Documents\GitHub\maveregistry-parse-server\node_modules\parse\lib\node\encode.js:73:13)
at _default (C:\Users\kevin\Documents\GitHub\maveregistry-parse-server\node_modules\parse\lib\node\encode.js:126:10)
at ParseUser.toJSON (C:\Users\kevin\Documents\GitHub\maveregistry-parse-server\node_modules\parse\lib\node\ParseObject.js:604:42)
at C:\Users\kevin\Documents\GitHub\maveregistry-parse-server\node_modules\parse-server\lib\triggers.js:598:81
at error (C:\Users\kevin\Documents\GitHub\maveregistry-parse-server\node_modules\parse-server\lib\triggers.js:379:9)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
Actual Outcome
I spent some time investigating this problem and found that this error was triggered by this block of code:
https://github.com/parse-community/parse-server/blob/44015c3e351fc7cf0ac3e99bc64738d35ddc3cba/src/triggers.js#L749-L764
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
);
Environment
Server
- Parse Server version:
4.3.0 - Operating system:
Windows - Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc):
local
Client
- SDK (iOS, Android, JavaScript, PHP, Unity, etc):
JavaScript - SDK version:
2.15.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/RestWrite.js at the beforeLogin flow and compare the trigger handling in src/triggers.js; reproduce with AuthData for a user that has an associated file. Done means beforeLogin runs without the unsaved-file encoding error while preserving access to the user data.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100