Basic Auth
Implements HTTP Basic Authentication
Options
- realm string
- Default:
"Basic Authentication"
- users record
- errorMessage string
- Default:
"401 Unauthorized"
Description
Middleware to protect the access to the site with basic access authentication.
Installation
This middeware must be used with the Lume's HTTP Server. To use it in production, you need a host running a Deno server, like Deno Deploy.
Create an entry point file (for example, serve.ts
) with the following code:
import Server from "lume/core/server.ts";
import basicAuth from "lume/middlewares/basic_auth.ts";
const server = new Server();
server.use(basicAuth({
users: {
user1: "password1",
user2: "password2",
},
}));
server.start();
Important
For security, it's not recommended to have the users and passwords hardcoded in your code. It's recommended to use environment variables, for example:
const user = Deno.env.get("AUTH_USERNAME");
const password = Deno.env.get("AUTH_PASSWORD");
server.use(basicAuth({
users: {
[user]: password,
},
}));
Local development
You can configure Lume's development server to use this middleware in the _config.ts
file:
import lume from "lume/mod.ts";
import basicAuth from "lume/middlewares/basic_auth.ts";
const site = lume({
server: {
middlewares: [
basicAuth({
users: { demo: "1234" },
}),
],
},
});
export default site;