nodejs + express + appfog different session for img resource

168 Views Asked by At

I have the following code to validate if a user is logged-in to my app.

My problem is that once the user is logged in the img request has a different session than the one i'm saving for the user.

I have the following code to validate if a user is logged-in to my app.

    app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    globalApp.mongo.getUserBySessionId(req.sessionID,function(user){
        console.log("SessionID :"+req.sessionID);
        console.log("User : "+ JSON.stringify(user));
        console.log("Route : "+JSON.stringify(req.route.params));
        if(user){
            req.user = user;
            res.locals.user = req.user;
            res.locals.security = globalApp.security;
        }else{
            if(publicPaths.indexOf(req.route.params[0])==-1){
                res.send({error:true,reason:'You are not logged in, please re-start the app.'});
                return;
            }
        }
        next();
    });
});

And I'm requesting the image like this:

<img src="/images/loading.gif" width="49" height="49"/>

The funny thing is that all other type of files are being served properly.Server is creating a new session for each img request.

Also, this only happen when my app runs on appfog. In my computer (localhost) it works fine.

Any advice?

Thanks a lot.

1

There are 1 best solutions below

0
On

I know i am late, but i had a same problem. It seems AppFog cut cookies for image and may be some other resources, so there is no plain way to control image access with sessions. You can rename resource "/images/loading.gif" to "/images/loading" for exmaple. http://feedback.appfog.com/forums/171983-appfog/suggestions/4299861-do-not-cut-cookie-for-images-and-other-resources