-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Request.user undefined and no information in request #2104
Comments
This is because always before call a cloud function you must log in first, then user or session token can be used in your cloud function |
Did you set the header correct ? You should have a header set :
Once you set this header you will have access to req.user in your cloud code . |
How do you set the header with Swift? You should have a header set : X-Parse-Session-Token : <SESSION_TOKEN> Once you set this header you will have access to req.user in your cloud code . — |
Make sure you have logged in your user before you make any cloud calls. Parse SDK add your header by default once it has a user logged in . |
I believe this is related to an intermittent issue I am having where I start to get access denied type of errors for any queries to private objects, that is, objects that have a certain ACL such as the user settings which only the current user should be able to access. The session becomes invalid or something. |
I got same problem accessing
|
It seems that if I use Parse.Cloud.define("fixTodoItem", function (request, response) {
console.log('*** Cloud function\'s request = ', request);
});
Parse.Cloud.afterSave("TodoItem", function(request) {
console.log('*** Cloud aftersave\'s request = ', request);
Parse.Cloud.run("fixTodoItem", {todoId: request.object.id}, {
success: function (result) {
console.log('Success: ', result);
},
error: function(error) {
console.log('Error: ', error);
}
});
}); Output:
|
I figure it out why Parse.Cloud.afterSave("TodoItem", function(request) {
Parse.Cloud.run("fixTodoItem", {todoId: request.object.id}, {
success: function (result) {
console.log('Success: ', result);
},
error: function(error) {
console.log('Error: ', error);
},
sessionToken: request.user.getSessionToken()
});
}); |
I'm also seeing req.user undefined in self-hosted Parse cloud code functions. It only occurs when attempting to make a cloud code call from a current user that is restored between launches of the iOS app. NOTE: I have not tried against Parse.com, as this is a new project without a Parse.com app. I recreated the bug in a fresh checkout of parse-server-example running on localhost. I only added the Steps to reproduce:
Client Environment: Server Environment: (cloned current parse-server-example and added Cloud Code function below) Here's the Cloud Code - just one function in my main.js Parse.Cloud.beforeSave(Parse.User, function(req, res) {
console.log("beforeSave req.user: " + req.user);
res.success();
}); Here's the offending iOS code. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject] ? ) - > Bool {
let configuration = ParseClientConfiguration {
$0.applicationId = "myAppId"
$0.server = "http://localhost:1337/parse"
}
Parse.initialize(with: configuration)
if let user = PFUser.current() {
print("Save will fail...")
saveRandomToCurrentUser()
} else {
let user = PFUser()
user.username = "[email protected]"
user.password = "password"
user.signUpInBackground { success, error in
if let error = error {
print("user sign up error: \(error)")
} else {
print("user sign up successful!")
print("Save will succeed...")
self.saveRandomToCurrentUser()
}
}
}
}
func saveRandomToCurrentUser() {
if let user = PFUser.current() {
user["random"] = "string"
user.saveEventually {
(success, error) in
if let error = error {
print("save error: \(error)")
} else {
print("save successful!")
}
}
}
} I haven't determined if this is a parse-server bug or Parse iOS SDK bug. |
In beforeSave(Parse.User) you should access the user with |
@flovilmart |
I'll add some tests checking if that's reproductible all the times. |
I suspect it to be an issue with the Parse iOS SDK. I haven't interrogated the headers yet (haven't had time today), but I suspect something isn't being persisted correctly for the logged-in user between launches. Resulting in parse-server not associating the request with the logged-in user. |
That is also possible, we infer the user on the session header |
I found a closed (yet unresolved) issue here parse-community/Parse-SDK-iOS-OSX#609 which describes the problem I'm having above. I see the error 'PFKeychainStore failed to set object for key 'currentUser', with error: -34018' which is describe here parse-community/Parse-SDK-iOS-OSX#437 as being fixed with 9.3, but I'm running iOS 10.0 and the problem persists. So I'll dive into those issues. |
I can confirm that the issue is resolved by following @takomborerwa 's suggestion in parse-community/Parse-SDK-iOS-OSX#437. Turning on Keychain Sharing for an app built against iOS 10 allows Facebook to store the user's credentials properly and send the proper headers to Parse Server. |
I've tried what @phuna suggested above, and it worked, but sometimes it returned undefined in the Cloud Function as well. |
Worked for me when enabling Keychain Sharing, as suggested by @mooshee in @takomborerwa's suggestion. |
Enabled Keychain Sharing and didn't help. |
If anyone runs into this, I turned on keychain sharing and still had the issue. I had to clean, delete the app, and reinstall to get things to work properly. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I'm trying to access a user in cloud code so I can modify user data. I don't have access to any session tokens and request.user is undefined
Steps to reproduce
Swift:
PFCloud.callFunctionInBackground("checkUser", withParameters: nil) {
(response: AnyObject?, error: NSError?) -> Void in
let responseString = response as? String
print(responseString)
}
Main.js
Parse.Cloud.define("checkUser", function(request, response) {
console.log(request);
var user = request.user;
response.success(user);
});
Expected Results
request.user or request.user.getSessionToken() give me correct data
Actual Outcome
request.user is undefined
Environment Setup
Logs/Trace
�[31merror�[39m: Uncaught internal server error. [TypeError: request.user is not a function] TypeError: request.user is not a function
at /var/app/current/cloud/main.js:54:24
at /var/app/current/node_modules/parse-server/lib/Routers/FunctionsRouter.js:95:11
at new Promise (/var/app/current/node_modules/parse-server/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:193:7)
at handleCloudFunction (/var/app/current/node_modules/parse-server/lib/Routers/FunctionsRouter.js:89:16)
at /var/app/current/node_modules/parse-server/lib/PromiseRouter.js:286:7
at Layer.handle as handle_request
at next (/var/app/current/node_modules/parse-server/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/var/app/current/node_modules/parse-server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle as handle_request
at /var/app/current/node_modules/parse-server/node_modules/express/lib/router/index.js:277:22
I passed the session token myself, but this doesn't seem secure
{ params: { sessionToken: 'XXXXXXXXXXXXXXXXXX' },
master: false,
user: undefined,
installationId: 'xxx-xxx-xxx-xxx',
log: FileLoggerAdapter {} }
The text was updated successfully, but these errors were encountered: