N
Nuxt4mo ago
M Whitaker

Protecting static folder on server side

I have a folder with statically built HTML, CSS and JS assets (think static dashboard) that I would like to protect using sessions. I don't know the HTML files ahead of time, so I am hoping that there is a way to serve and protect a whole folder. I tried using serveStatic from h3, but got confused on how to actually implement that and how it fits into the Nuxt 3 file-based routing. Any pointers would be appreciated!
4 Replies
manniL
manniL4mo ago
why do you want to protect it? protecting static files will be tricky depending on the use case but you could e.g. serve them via nitro + useStorage and check auth before
M Whitaker
M Whitaker4mo ago
it's an internal dashboard built with Observable that I only want to show to logged in users. Observable framework has their own cli to build those static HTML files and assets. I'll check into Nitro, but I suspect it will be tricky to serve that folder. only to logged in users. Many thanks for responding! Just for future reference, the use case I described is trivial with express:
import express from 'express';
const app = express();
/ Protect the protected folder
app.use('/protected', (req, res, next) => {
if (req.session.authenticated) {
// Serve files from the protected folder
next();
} else {
// Deny access or redirect to login page
res.status(403).send('Access denied');
}
});

// Serve files from the protected folder
app.use('/protected', express.static(path.join(path.resolve(), 'dist')));
import express from 'express';
const app = express();
/ Protect the protected folder
app.use('/protected', (req, res, next) => {
if (req.session.authenticated) {
// Serve files from the protected folder
next();
} else {
// Deny access or redirect to login page
res.status(403).send('Access denied');
}
});

// Serve files from the protected folder
app.use('/protected', express.static(path.join(path.resolve(), 'dist')));
manniL
manniL4mo ago
then it should be trivial with nitro too 🙂
M Whitaker
M Whitaker4mo ago
It should, but sadly not for me so far. 🤷‍♂️ But will check out the Nitro docs- thanks for the pointer!