Overview
The SvelteKit Basic Auth is a hook that adds password protection to an app. It checks for a valid authentication token and prompts the user to login using the browser’s built-in authentication mechanism. The hook retrieves a list of username-password pairs from an environment variable and allows the authorized username to be accessed from the page data.
Features
- Password protection for SvelteKit apps
- Check for valid authentication token
- Prompt user to login using browser’s authentication mechanism
- Retrieve username-password pairs from environment variable
- Access authorized username from page data
Installation
To install the SvelteKit Basic Auth hook, follow these steps:
- Copy the code from the file
src/hooks.server.ts - Create an environment variable file called
.env.exampleand add a list of username-password pairs in the formatUSERNAME1:PASSWORD1, USERNAME2:PASSWORD2, ... - Use the hook in your SvelteKit app to enable password protection and access the authorized username from the page data
Summary
The SvelteKit Basic Auth hook provides a simple and efficient way to add password protection to your SvelteKit app. By checking for a valid token and prompting the user to login, it ensures only authorized users can access the protected areas of your app. The hook retrieves username-password pairs from an environment variable, allowing you to easily manage and update the authorized users.