vite-plugin-http-basic-auth is a Vite plugin designed to add HTTP Basic Authentication support to the Vite development server and preview server. It is currently at version 1.0.2. As a plugin for Vite, its release cadence is generally tied to the Vite ecosystem, with updates provided as needed for compatibility or new features. This plugin is particularly useful for protecting development or staging environments that are exposed publicly, requiring credentials before allowing access. Key differentiators include its simple configuration within the Vite `defineConfig`, direct integration with environment variables for credentials, and optional enablement for both development and preview modes. It leverages standard HTTP Basic Auth, making it widely compatible with browsers and other clients.
npm install vite-plugin-http-basic-authVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure HTTP Basic Authentication using environment variables for a Vite project, protecting both the development and preview servers.
Access environment variables that should only be server-side without the `VITE_` prefix, or ensure sensitive credentials are only used in the server configuration and not within the client-side code paths.
Ensure your development and preview servers are served over HTTPS. For Vite, you can configure HTTPS via `server.https: true` in `vite.config.ts` and provide certificate details.
Conditionally set `useInServer` or `useInPreview` based on `mode`: `useInServer: mode === 'development'`, etc., or omit them to use the default `true`.
Ensure you are using the correct ESM import syntax: `import httpAuth from 'vite-plugin-http-basic-auth';`.
Verify the username and password in your `.env` file match what is configured in `vite.config.ts`. Ensure `loadEnv` is correctly configured to load your `.env` file and prefixes.
Check `useInServer` and `useInPreview` options in the plugin configuration. Confirm the plugin is correctly added to the `plugins` array in `vite.config.ts`.