how to use sessions in express, couchDB, and node.js

后端 未结 4 396
你的背包
你的背包 2020-12-24 07:38

so I basically want to use sessions to store the users name and check to see if the user logged in or not. If not, the page will redirect to the login page.

I am usi

相关标签:
4条回答
  • 2020-12-24 07:47

    If you're doing app.use(app.router) before the lines that set up the cookie and session handling, try moving it to after them. That fixed the same problem for me.

    0 讨论(0)
  • 2020-12-24 07:48

    the sessionSecret in the configuration (config.json?) should be non-empty:

    sessionSecret: "something other than an empty string",

    0 讨论(0)
  • 2020-12-24 07:55

    I was trying to solve the exact same problem for he last few hour.

    Try initializing your server like that:

    var app = express.createServer(
    express.cookieParser(),
    express.session({ secret: "crazysecretstuff"})
      );
    

    That should work.

    0 讨论(0)
  • 2020-12-24 08:02

    I had same issue of getting undefined for session variable which I knew was set ok.

    In my case it turned out to be caused by cross origin request, I had forgotten the withCredentials header on the request.

    So for example in my client side Angular app I needed to do this:

    var config = { withCredentials: true };
    return $http.get(appConfig.apiUrl + '/orders', config);
    

    and in Express this:

    res.header('Access-Control-Allow-Credentials', true);
    
    0 讨论(0)
提交回复
热议问题