N
NuxtM 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!
M
manniL13d 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
MW
M Whitaker12d 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')));
M
manniL12d ago
then it should be trivial with nitro too 🙂
MW
M Whitaker12d ago
It should, but sadly not for me so far. 🤷‍♂️ But will check out the Nitro docs- thanks for the pointer!
Want results from more Discord servers?
Add your server
More Posts
nuxt v2 upgrade to v3 errorHello guys, we have project which is build with nuxt v2, but now i updated it to v3, and im getting [RESOLVED] Nuxt3 and netlify hosting env variablesHey! First of all "Nuxt3 is epic!". But I need some help with Nuxt 3 Netlify environment variables. Nuxt 3 dayjs moduleI try to use dayjs module in my pnpm monorepo i follow all the documentation but when i use dayjs iHow to set meta theme-color in nuxt.config``` app: { head: { ... meta: [{ theme_color: '#ffffff', }], }, },``` Testing Nuxt 3 API EndpointsHi, I would like to test my Nuxt 3 server api endpoints. I know I could test it for example in posNitro: How do I pass data in a middleware to route handlers?In Express, you can do this attaching data to the `response.locals` object. How do I do this in NitrMake `useRoute()` update in layoutHi! Im trying to use useRoute() in a layout, to update my navigations links based on the ´route.paraHow do you set a PostCSS parser in the `nuxt.config.ts`?I'm trying to do that but it seems the "parser" option isn't allowed on the object.Auto-import only Nuxt and Vue features but no custom or third party codeHey! I wonder: is it possible to disable auto imports for components, composables, and utils while kuseAsyncData weird behaviorHi guys, I'm experiencing a strange behavior. I'm using async data to call an API. But there is a coProblem with sending POST/GET Request at the same timeNow I'm looking to load the last projects of a user from the database by sending his name and receivNuxt Content behind CloudflareWhen I use Nuxt Content behind Cloudflare, I have the issue that some pages (but not all) cannot be Odd situation with Nuxt hosting and site accessibilityHey everyone. First post on this Discord. We have a Nuxt app (an intranet, small company app) that wUButton showing over sticky headerI have a table and one of the columns is filled with Nuxt UI buttons. I have a sticky header for thi