Vite HTTP Basic Auth Plugin
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.
Common errors
-
TypeError: httpAuth is not a function
cause Incorrect import statement for the plugin.fixEnsure you are using the correct ESM import syntax: `import httpAuth from 'vite-plugin-http-basic-auth';`. -
401 Unauthorized
cause Incorrect username/password credentials provided, or the environment variables supplying them are not loaded correctly.fixVerify 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. -
HTTP Basic Auth is not prompting in browser
cause The plugin might not be active for the current server type (dev or preview) or the plugin array in Vite config is misconfigured.fixCheck `useInServer` and `useInPreview` options in the plugin configuration. Confirm the plugin is correctly added to the `plugins` array in `vite.config.ts`.
Warnings
- gotcha Environment variables starting with `VITE_` are exposed to the client-side bundle in Vite by default. Ensure that sensitive credentials used for server-side basic auth are not inadvertently exposed or handle them with care.
- gotcha Basic Authentication sends credentials in plain text (Base64 encoded, not encrypted) over the network. Always use HTTPS for any environment where this plugin is deployed to prevent eavesdropping.
- gotcha The plugin's `useInServer` and `useInPreview` options are `true` by default. If you only want basic auth for specific environments (e.g., preview but not dev), you must explicitly set these options to `false` based on your `mode`.
Install
-
npm install vite-plugin-http-basic-auth -
yarn add vite-plugin-http-basic-auth -
pnpm add vite-plugin-http-basic-auth
Imports
- httpAuth
const httpAuth = require('vite-plugin-http-basic-auth');import httpAuth from 'vite-plugin-http-basic-auth';
- Configuring the plugin
plugins: [httpAuth([{ username: 'user', password: 'pass' }])] - Types
import type { UserCredentials, AuthOptions } from 'vite-plugin-http-basic-auth';
Quickstart
import { defineConfig, loadEnv } from 'vite';
import httpAuth from 'vite-plugin-http-basic-auth';
// To run this example, create a .env file:
// VITE_AUTH_USERNAME_DEV=devuser
// VITE_AUTH_PASSWORD_DEV=devpass
// VITE_AUTH_REALM=SecureDevSite
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd(), '');
return {
plugins: [
httpAuth([{
username: env.VITE_AUTH_USERNAME_DEV ?? '', // Use environment variables for credentials
password: env.VITE_AUTH_PASSWORD_DEV ?? ''
}], {
realm: env.VITE_AUTH_REALM ?? 'Protected Area', // Optional: customize the realm
useInServer: true, // Default: true, enables auth for 'npm run dev'
useInPreview: true // Default: true, enables auth for 'npm run preview'
})
]
};
});