netlify-plugin-redirect-with-auth
v1.0.1
Published
Add redirects with authorization header set in env variable
Maintainers
Readme
Netlify Plugin: Redirect with Auth
This Netlify build plugin allows you to add redirects with authentication headers to your Netlify site.
Features
- Adds redirects with custom authentication headers - header is set in an environment variable, separate from the code in your repo
- Works for both build and development environments
- Configurable through the
netlify.tomlfile
Installation
- First, install the plugin as an npm dependency:
npm install netlify-plugin-redirect-with-auth- Then, add the plugin to your
netlify.tomlfile:
[[plugins]]
package = "netlify-plugin-redirect-with-auth"
[plugins.inputs]
redirects = [
{ from = "/submit", to = "https://api.example.com/submit" },
{ from = "/api/*", to = "https://api.example.com/:splat" }
]Set the
NETLIFY_REDIRECT_AUTH_HEADERenvironment variable:For production:
- Go to your Netlify site dashboard
- Navigate to Site settings > Build & deploy > Environment
- Add a new environment variable:
- Key:
NETLIFY_REDIRECT_AUTH_HEADER - Value: Your desired authorization header (e.g.,
Bearer your-token-here)
- Key:
For development:
- Create a
.envfile in your project root (if it doesn't exist already) - Add the following line to the
.envfile:NETLIFY_REDIRECT_AUTH_HEADER=Bearer your-token-here - This environment variable will be used when you run
netlify dev
Note: Make sure to add .env to your .gitignore file to avoid committing sensitive information to your repository.
Configuration
Environment Variables
Set the following environment variable in your Netlify site settings:
NETLIFY_REDIRECT_AUTH_HEADER: The authentication header to be added to the redirects.
Plugin Inputs
Configure the plugin in your netlify.toml file:
[[plugins]]
package = "netlify-plugin-redirect-with-auth"
[plugins.inputs]
redirects = [
{ from = "/submit", to = "https://api.example.com/submit" },
{ from = "/api/*", to = "https://api.example.com/:splat" }
]Each redirect object in the redirects array must have both from and to properties.
How It Works
- The plugin runs during the
onPreBuildandonPreDevevents. - It checks for the presence of the
NETLIFY_REDIRECT_AUTH_HEADERenvironment variable. - It validates the
redirectsinput to ensure it's a non-empty array of valid redirect objects. - For each redirect, it adds a new redirect rule to the Netlify configuration with:
- The specified
fromandtoURLs - A status code of 200
- The
forceflag set to true - The authentication header from the environment variable
- The specified
Notes
- This plugin adds the authentication header to all specified redirects.
- The redirects are set to force (always redirect, even if the requested file exists).
- The status code is set to 200 for all redirects.
Troubleshooting
If you encounter issues:
- Ensure the
NETLIFY_REDIRECT_AUTH_HEADERenvironment variable is set correctly. - Check that your
redirectsinput innetlify.tomlis formatted correctly. - Verify that each redirect object has both
fromandtoproperties.
For more help, please open an issue in the plugin's GitHub repository.
