Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8152997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:52:32+00:00 2026-06-06T15:52:32+00:00

I’ve got a rather strange problem with my code on production server. On my

  • 0

I’ve got a rather strange problem with my code on production server. On my MacOS it works perfectly, but when I deploy my app, I cannot login. After debug, I’ve found that I cant load session from req-object. Here’s the code of all main parts (settings,, login page and main page after login)

//SETTINGS
var express = require('express');
var app = express.createServer();
var mongo = require('mongodb'),
    Server = mongo.Server,
    Db = mongo.Db,
    ObjectID = require('mongodb').ObjectID;
var BSON = require('mongodb').BSONPure;

var RedisStore = require('connect-redis')(express);

//connecting to mongo
var server = new Server('localhost', 27017, {
    auto_reconnect: true
});
var db = new Db('metadocs-node_db', server);

//setting express app
app.set('view engine', 'ejs');
app.configure(function () {
    app.use("/static", express.static(__dirname + '/static'));
    app.use(express.bodyParser());
    app.use(express.cookieParser());
    app.use(express.session({
        secret: "meta-meta",
        store: new RedisStore
    }));
    app.use(express.methodOverride());
});

//login page - POST for /login page
app.post('/login', function (req, res) {
    db.collection("users", function (err, collection) {
        collection.findOne({
            username: req.body.username
        }, function (err, doc) {
            if (doc && doc.password == req.body.password) {
                req.session.user_id = doc._id;
                res.redirect('/');
            } else {
                res.render('login.ejs', {
                    success_login: 1
                });
            }
        });
    });
});

//GET INDEX PAGE - only after login
app.get('/', loadUser, function (req, res) {
    db.collection("companies", function (err, collection) {
        collection.count(function (err, count) {
            res.render('index.ejs', {
                total_companies: count,
                current_user: req.currentUser['username']
            });
        });
    });
});

//loadUser() is function that creates/loads user session if possible
function loadUser(req, res, next) {
    if (req.session.user_id) {
        db.collection("users", function (err, collection) {
            collection.findOne({
                _id: new ObjectID(req.session.user_id)
            }, function (err, user) {
                if (user) {
                    req.currentUser = user;
                    next();
                } else {
                    res.redirect('/login');
                }
            });
        });
    } else {
        res.redirect('/login');
    }
}

Here’s the problem code line:

if (req.session.user_id) {

in loadUser() function. The problem is req.session.user_id is empty – I’ve found it out while debugging every line step-by-step. What I’m doing wrong? It works on my Mac, but does’t work on Ubuntu.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:52:33+00:00Added an answer on June 6, 2026 at 3:52 pm

    I have tried the server you gave the address for at another post for this same problem. Cookies are set without any problems. I logged in successfully. However, one thing caught my attention: expiration time. It is 6/26/12 23:18 GMT now and your cookie expiration is set to 27 Jun 2012 03:18 GMT, which leaves only 4 hours before cookie is expired.

    It is perfectly okay to set cookies that will expire in 4 hours; unless your server or a client has wrong date/time or timezone set. Could you please make sure that the working/non-working clients and of course your server have the correct date/times and time zones set up? I believe this is probably the reason of your problem.

    On systems I develop, I give cookies expiration times for one year. I validate session on server side without depending on cookie expiration. I do not use connect for cookie and session management since I am always more comfortable doing these things myself. However you can do the same thing while you are still using connect. At session settings, you should set maxAge for cookie to a higher value:

    Below code is copied from connect documentation. I have just added maxAge parameter on third line, setting expiration to one day.

    connect().
      .use(connect.cookieParser('keyboard cat'))
      .use(connect.session({ key: 'sid', cookie: { secure: true, maxAge:86400000 }}))
    

    I hope your problem is solved with this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.