javascript - Sails.js Node server req.session is always empty, using Passport-local strategy. - Stack Overflow

Have ember app running on http:localhost:4200.Sails App is running on http:localhost:1337.I have a

Have ember app running on http://localhost:4200.
Sails App is running on 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:

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

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

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.

And if I sails.log.verbose(req.session) it's always empty at this point. No matter what I do.

I've enabled cors on my server, and my /config/cors.js has these options:

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:

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?

EDIT: These were asked for in ments:

Contents of /config/http.js:

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

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

And /config/session.js

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

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

Have ember app running on http://localhost:4200.
Sails App is running on 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:

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

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

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.

And if I sails.log.verbose(req.session) it's always empty at this point. No matter what I do.

I've enabled cors on my server, and my /config/cors.js has these options:

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:

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?

EDIT: These were asked for in ments:

Contents of /config/http.js:

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

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

And /config/session.js

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

  cookie: {
    maxAge: 48 * 60 * 60 * 1000
  }
}
Share Improve this question edited Jul 15, 2015 at 0:00 Ryan asked Jul 9, 2015 at 18:56 RyanRyan 5,6823 gold badges39 silver badges66 bronze badges 12
  • Do you have a session configured? That's inside config/http.js & config/session.js – Bwaxxlo Commented Jul 9, 2015 at 21:24
  • @Bwaxxlo added those files to question. – Ryan Commented Jul 9, 2015 at 21:34
  • 2 First of all, this will never evaluate to true if(req.body === {}). {} !== {}, object are pared by reference and not through properties. Secondly, ment out cookieParser & session in config/http.js because you're using your own special middleware (i.e: Passport) – Bwaxxlo Commented Jul 9, 2015 at 21:41
  • Also, if you're following this tutorial, please make sure you've created config/passport.js – Bwaxxlo Commented Jul 9, 2015 at 21:44
  • @Bwaxxlo the pare there was an edit for StackOverflow, not how it is in my code. I did go off that tutorial and I did create a config/passport.js. That is interesting about menting out session and cookieParser since that's not anything I've seen in sails documentation or in that tutorial. I'll give it a try, thanks. – Ryan Commented Jul 9, 2015 at 21:47
 |  Show 7 more ments

3 Answers 3

Reset to default 1

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./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. If there is an equivalent in passport's session support you might try to call it explicitly after updating values in the session.

You need to add a Get in the custom controller.

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. In my case the problem was that the session was disabled via .sailsrc file (which of course took me some time to spot). In case of this issue: just remove the "session": false line from that file and session-property gets added.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745630947a4637105.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信