vardump
vardump

Reputation: 658

Passport.js express views

I'm using passport and some static .html files. I want to include them as express views or static files. Now I want SOME .html files like login.html to be available for everyone, but the other view files like secret.html should need passport. I know, that you can use app.get('somePath', ensureAuthenticated, function (req, res, next) {} for routes, but I want it for static files.

How to do it on a non-hacky way?

Upvotes: 1

Views: 237

Answers (1)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123473

If all "secure" files share a distinct URL prefix, such as /public/secure, you can use() ensureAuthenticated with this path:

app.use('/public/secure', ensureAuthenticated);
app.use('/public', express.static(__dirname + '/public'));

Otherwise, the express.static() middleware doesn't offer many options for controlling access. For the most part, it assumes all available files from the path given to it are meant to be "public."

Though, it does normally refuse to send hidden files, following the *nix standard of a . prefix:

~/path/to/.secret.html

Then, to actual serve it, you can send the file yourself in the route you suggested with the middlware attached:

app.get('/path/to/secret.html', ensureAuthenticated, function (req, res) {
    res.sendfile(__dirname + '/path/to/.secret.html');
});

And, you may want to consider moving it out of your static() directory,

Upvotes: 1

Related Questions