simi kaur
simi kaur

Reputation: 1277

allow access to html page only to logged in users

I have app.html in a public folder which is serving static files to express.

I have used code from here.

But instead of profile.ejs from views folder(as per the code in link), I want to serve app.html from inside of public folder.

  app.get('/app.html', isLoggedIn, function(req, res) {
        res.render('app.html'/*, {
            user : req.user // get the user out of session and pass to template
        }*/);
        
        console.log("APP.HTML");
        
    });


function isLoggedIn(req, res, next) {

    // if user is authenticated in the session, carry on 
    if (req.isAuthenticated())
        return next();

    // if they aren't redirect them to the home page
    res.redirect('/');
}

But it does not restrict the access to loggedIn users and I am able to access it directly after logging out as well.

What could be wrong here?

Upvotes: 2

Views: 2805

Answers (1)

mscdex
mscdex

Reputation: 106736

The problem is most likely that you have express.static() before your app.get() line. The static middleware checks if the file exists and sends it if it does. So you need to put that app.get() before your express.static() middleware.

Upvotes: 3

Related Questions