1
votes

I am getting the Invalid session token(code: 209) issue while my session token exist in the _Session table for which i am getting this issue. Usually This error comes when session token doest not exist in db for which i am just making request but in my case i am getting this issue while session token exist in db. I am just sharing the cloud code where i am getting this error.

main.js :-

Parse.Cloud.define("ping", function (req, res) {
    try {
        if (req.user !== undefined) {
            var userId = req.params.hasOwnProperty(GameConstants.USER_ID) && req.params.user_id !== "" ? req.params.user_id : req.user.get("username");
            LoginHelper.updateUserSessionEndDateTime(userId, function (error, status) {
                if (error) {
                    res.success({result: 0, custom_error_code: error.code, custom_error_message: error.message});  //Getting the error in this block :- {"result":0,"custom_error_code":209,"custom_error_message":"invalid session token"}

                    //custom log
                    logger.info(JSON.stringify({result: 0, custom_error_code: error.code, custom_error_message: error.message, req: req}));
                } else if (status === 0) {
                    res.success({result: 0, custom_error_code: CustomErrorCode.INVALID_USER_ERROR, custom_error_message: "Inavalid user"});

                    //custom log
                    logger.info(JSON.stringify({result: 0, custom_error_code: error.code, custom_error_message: error.message, req: req}));
                } else {                    
                    res.success({result: 1});

                    //CommonHelper.recordBuyerSessionData(userId);
                }
            });
        } else {
            res.success({result: 0, custom_error_code: CustomErrorCode.INVALID_USER_ERROR, custom_error_message: "Inavalid user"});

            //custom log
            logger.info(JSON.stringify({result: 0, custom_error_code: CustomErrorCode.INVALID_USER_ERROR, custom_error_message: "Inavalid user", req: req}));
        }
    } catch (e) {
        res.success({result: 0, custom_error_code: e.code, custom_error_message: e.message, stacktrace: e.stack});

        //custom log
        logger.info(JSON.stringify({result: 0, custom_error_code: e.code, custom_error_message: e.message, stacktrace: e.stack, req: req}));
    }

});

LoginHelper.js :-

updateUserSessionEndDateTime: function (userId, callback) {
        var query = new Parse.Query(Parse.Object.extend(GameConstants.GAME_USERS));
        query.select("session_end_date_time");
        query.equalTo(GameConstants.OBJECT_ID, userId);
        query.first({
            success: function (gameUser) {
                if (typeof gameUser !== "undefined") {
                    //console.log("updateUserSessionEndDateTime data===========> " + JSON.stringify(gameUser));
                    gameUser.set(GameConstants.SESSION_END_DATE_TIME, new Date());
                    gameUser.save(null, {
                        success: function (gameUser) {
                            //console.log("updateUserSessionEndDateTime.save.success===========> " + JSON.stringify(gameUser));
                            callback(null, 1);
                        },
                        error: function (error) {
                            //console.log("updateUserSessionEndDateTime.save.error===========> " + error.message);
                            callback(error);
                        }
                    });
                } else {
                    //console.log("updateUserSessionEndDateTime.sessionData-------> undefined ");
                    callback(null, 0);
                }
            },
            error: function (error) {
                //console.log("updateUserSessionEndDateTime.error===========> " + error.message);
                callback(error);
            }
        });
    }

Error Logs -

{"result":0,"custom_error_code":209,"custom_error_message":"invalid session token"}{"result":0,"custom_error_code":209,"custom_error_message":"invalid session token"}{"result":0,"custom_error_code":209,"custom_error_message":"invalid session token"}{"result":0,"custom_error_code":209,"custom_error_message":"invalid session token"}{"result":0,"custom_error_code":209,"custom_error_message":"invalid session token"}{"result":0,"custom_error_code":209,"custom_error_message":"invalid session token"}{"result":0,"custom_error_code":209,"custom_error_message":"invalid session token"}

After user login successfully then calling ping cloud function. I don't want to remove ACL to solve this issue. I kindly request you to please help me why i am getting this issue.

Thanks

1

1 Answers

0
votes

Cloud code does not automatically pass your session to subsequent server calls. You need to explicitly pass your session token when saving, fetching, etc, if you have put ACL permissions in place that restrict public read/write.

i.e. your save call should be gameUser.save(null, { success:..., error:..., sessionToken:userSessionToken});

Where userSessionToken is found by calling req.user.getSessionToken(); in the original cloud function.