【问题标题】:Getting Invalid session token(code: 209) while session token exist in _Session table - Parse Server JS SDK_Session 表中存在会话令牌时获取无效的会话令牌(代码:209) - Parse Server JS SDK
【发布时间】:2017-04-28 10:56:55
【问题描述】:

当我的会话令牌存在于我遇到此问题的 _Session 表中时,我遇到了无效会话令牌(代码:209)问题。 通常当会话令牌在数据库中不存在时会出现此错误,我只是提出请求,但在我的情况下,当会话令牌存在于数据库中时,我遇到了这个问题。 我只是在共享出现此错误的云代码。

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);
            }
        });
    }

错误日志 -

{"result":0,"custom_error_code":209,"custom_error_message":"无效会话令牌"}{"result":0,"custom_error_code":209,"custom_error_message":"无效会话令牌"}{ "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":"无效的会话令牌"}

用户登录成功后调用ping云函数。我不想删除 ACL 来解决这个问题。我恳请您帮助我为什么会遇到这个问题。

谢谢

【问题讨论】:

    标签: parse-server


    【解决方案1】:

    云代码不会自动将您的会话传递给后续服务器调用。如果您设置了限制公共读/写的 ACL 权限,则需要在保存、获取等时显式传递您的会话令牌。

    即你的保存电话应该是gameUser.save(null, { success:..., error:..., sessionToken:userSessionToken});

    userSessionToken在原云函数中调用req.user.getSessionToken();找到。

    【讨论】:

    • 嗨,杰克,谢谢您的回复。我上面提供的服务器云代码在 95% 的情况下为我工作,在 5% 的情况下失败,然后我收到无效的会话令牌问题。在我执行数据库操作以应用 ACL 的每种方法中传递会话令牌是很重要的。
    • 听起来 5% 的 ACL 不允许公共读/写。如果您不想重新编写 ACL,请传递会话或传递主密钥。
    • 如果我通过了 useMasterKey : true 那么没有 ACLs 检查将通过 ACLs 检查,我只想出于安全原因进行 ACLs 检查。
    • 您没有将会话令牌传递到您的查询或保存中。
    猜你喜欢
    • 2015-06-08
    • 1970-01-01
    • 2018-11-01
    • 2015-10-24
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多