简体   繁体   English

Sails.js 节点服务器 req.session 始终为空,使用 Passport-local 策略。

[英]Sails.js Node server req.session is always empty, using Passport-local strategy.

Have ember app running on http://localhost:4200 .http://localhost:4200上运行 ember 应用程序。
Sails App is running on http://localhost:1337 . Sails App 在http://localhost:1337

I have a policy set on a pre-signup survey.我有一个关于注册前调查的政策。 So on the sails side in /api/controllers/ProcessSurveyController.js I have this:所以在/api/controllers/ProcessSurveyController.js的帆侧我有这个:

module.exports = {

    process_survey: function(req, res){
        if(req.body === {} || req.body === null){
            res.status(400);
            return res.send({err: "something bad happened"});
        }
        var params = req.body;
        req.session.user = {};
        if(params.p_1 === '1' && params.p_2 === '1' && params.p_3 === '0' && params.p_4 !== "Bad Param"){
            req.session.user.qualifies = true;
            res.status(200);
            return res.send({message: 'user qualifies', status: 'good'});   
        }else{
            req.session.user.qualifies= false;
            res.status(200);
            return res.send({message: "user fails to qualify", status: "bad"});
        }
    }
};

I then have this policy in api/policies/Qualifies.js然后我在api/policies/Qualifies.js有这个政策

module.exports= function(req, res, next){
    if(req.session.user.qualifies){
        return next();
    }else{
        res.status(400);
        return res.send({status: 400, message: 'User does not qualify.'});
    }
};

Which I apply to my api/UserController.js我应用于我的api/UserController.js

Only thing is that whenever I post from Ember to my UserController.create method I get an error from that policy saying cannot read property qualifies of undefined.唯一的问题是,每当我从 Ember 发布到我的UserController.create方法时,我都会从该策略中收到错误消息,说cannot read property qualifies of undefined.

And if I sails.log.verbose(req.session) it's always empty at this point.如果我sails.log.verbose(req.session)在这一点上它总是空的。 No matter what I do.不管我做什么。

I've enabled cors on my server, and my /config/cors.js has these options:我在我的服务器上启用了cors ,我的/config/cors.js有以下选项:

module.exports.cors = {
   allRoutes: true,

   origin: 'http://localhost:4200',

   credentials: true,

   methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD',

   headers: 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'
};

In my ember adapter I have this:在我的 ember 适配器中,我有这个:

export default DS.RESTAdapter.extend({
    host: 'http://localhost:1337',
    ajax: function(url, method, hash){
        hash.crossDomain = true;
        hash.xhrFields = {withCredentials: true};
        return this._super(url, method, hash);
    }
});

Clearly I'm missing something important but I just don't know what, and I've run out of ideas for google queries.显然我遗漏了一些重要的东西,但我不知道是什么,而且我已经没有谷歌查询的想法。 Why is my req.session always empty?为什么我的 req.session 总是空的?

EDIT: These were asked for in comments:编辑:这些是在评论中要求的:

Contents of /config/http.js : /config/http.js内容:

module.exports.http = {
    middleware: {
        passportInit: require('passport').initialize(),
        passportSession: require('passport').session(),

        order: [
            'startRequestTimer',
            'cookieParser',
            'session',
            'passportInit',
            'passportSession',
            'myRequestLogger',
            'bodyParser',
            'handleBodyParserError',
            'compress',
            'methodOverride',
            'poweredBy',
            '$custom',
            'router',
            'www',
            'favicon',
            '404',
            '500'
        ]
}

And /config/session.js/config/session.js

module.exports.session = {
  secret: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',

  cookie: {
    maxAge: 48 * 60 * 60 * 1000
  }
}

I doubt this is still an issue since it's so old, but there are times in my ExpressJS apps where I use express-session ( https://github.com/expressjs/session ) and have to explicitly call req.session.save(callback) in order for the session to save back to the store I'm using.我怀疑这仍然是一个问题,因为它太旧了,但有时在我的 ExpressJS 应用程序中我使用 express-session ( https://github.com/expressjs/session ) 并且必须显式调用 req.session.save(回调)以便会话保存回我正在使用的商店。 If there is an equivalent in passport's session support you might try to call it explicitly after updating values in the session.如果passport 的会话支持中存在等效项,您可以尝试在更新会话中的值后显式调用它。

You need to add a Get in the custom controller.您需要在自定义控制器中添加一个Get

So for example,例如,

instead of:代替:

process_survey: function(req, res){
         console.log(req.session)
}

write:写:

"Get process_survey": function(req, res){
         console.log(req.session)
}

I just added session back into an existing Sails-app.我刚刚将 session 添加回现有的 Sails-app。 In my case the problem was that the session was disabled via .sailsrc file (which of course took me some time to spot).在我的情况下,问题是会话通过.sailsrc文件被禁用(这当然花了我一些时间来发现)。 In case of this issue: just remove the "session": false line from that file and session -property gets added.如果出现此问题:只需从该文件中删除"session": false行并添加session -property。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM