Search code examples
node.jsexpressmiddleware

Middleware in NodeJs always redirecting to the same page (I guess)


I'm developing a page in order to learn in NodeJs with express and ejs. In a routes file if match to / or /login checks if logged in. If not, loads the login page, otherwise loads the admin page. This work fine, I added another routes to redirect to some other pages. To check if logged in I use a middleware requireLoggedIn, here is my code:

var path = require("path");

module.exports = function (app) {
    app.get(/^\/$|^\/login$/,function(req,res){
        if(req.session.loggedIn == undefined)
        {
            res.set('Access-Control-Allow-Origin', '*');
            res.render('pages/login');
        }
        else
        {
            console.log("here")
            res.writeHead(301, {
                Location: '/admin'
            });
            res.end();
        }
    }).listen(3000);

    function requireLoggedIn(req, res, next) {
        if (!req.session || req.session.loggedIn === undefined) {
            res.writeHead(301, { 'Location': '/login' });
            res.end();
            return;
        }
        next();
    }

    app.get('/admin', requireLoggedIn, function(req,res){
        res.send('HERE WILL BE ADMIN PAGE');
    });

    app.get('/new-product', requireLoggedIn, function(req,res){
        res.render('pages/new-product');
    });
}

After logging in, if I want to go to /new-product it's always redirecting to /admin. I thought it was a problem on my regex on the first route (Because is the only place that redirects to /admin), but I added a console.log, and is not logging when trying to load /new-product and loads /admin. Removing the middleware the pages loads just fine, so I guess the problem is there, but I don't see which is the problem

What am I doing wrong?

EDIT

After clearing browser cache, I can load correctly, to any page, but after the first load of any page, all other pages will redirect to that.

EDIT

I add more code with more debugs, to check:

var path = require("path");

module.exports = function (app) {
    app.get(/^\/$|^\/login$/,function(req,res){
        console.log(req.session.loggedIn);
        if(req.session.loggedIn == undefined)
        {
            res.set('Access-Control-Allow-Origin', '*');
            res.render('pages/login');
            console.log("NOT LOGGED IN");
        }
        else
        {
            req.session.loggedIn = true;
            console.log("REDIRECT TO DASHBOARD")
            res.writeHead(301, {
                Location: '/dashboard'
            });
            res.end();
        }
    }).listen(3000);

    function requireLoggedIn(req, res, next) {
        if (!req.session || req.session.loggedIn === undefined) {
            res.writeHead(301, { 'Location': '/login' });
            res.end();
            return;
        }
        next();
    }

    app.get("/dashboard", requireLoggedIn, function(req,res){
        console.log('LOAD DASHBOARD');
        res.render('pages/dashboard');
    });

    app.get("/new-product", requireLoggedIn, function(req,res){
        console.log('LOAD NEW PRODUCT');
        res.render('pages/new-product');
    });
}

(/admin now is called dashboard, but is the same). Step by Step

  1. Clear browser cache and start node
  2. localhost:3000/ --> get logged NOT LOGGED IN
  3. Login and automatically redirects to /dashboard --> get logged REDIRECT TO DASHBOARD| Then get logged -->LOAD DASHBOARD`
  4. Try to enter in /new-product --> get logged LOAD DASHBOARD

Solution

  • Try switching to 302 redirect... 301 redirects are often cached.