Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-basic-auth

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-basic-auth

Plug & play basic auth middleware for express

  • 1.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is express-basic-auth?

The express-basic-auth npm package is a simple and straightforward middleware for adding basic HTTP authentication to an Express.js application. It allows you to protect routes with a username and password, making it useful for securing endpoints in a lightweight manner.

What are express-basic-auth's main functionalities?

Basic Authentication

This feature allows you to set up basic authentication for your Express.js routes. In this example, the middleware is configured to require a username 'admin' and password 'supersecret' to access the root route.

const express = require('express');
const basicAuth = require('express-basic-auth');

const app = express();

app.use(basicAuth({
    users: { 'admin': 'supersecret' },
    challenge: true
}));

app.get('/', (req, res) => {
    res.send('Hello, authenticated user!');
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

Custom Unauthorized Response

This feature allows you to customize the response sent to clients when they fail to authenticate. In this example, the middleware is configured to return a simple 'Unauthorized' message.

const express = require('express');
const basicAuth = require('express-basic-auth');

const app = express();

app.use(basicAuth({
    users: { 'admin': 'supersecret' },
    unauthorizedResponse: (req) => 'Unauthorized'
}));

app.get('/', (req, res) => {
    res.send('Hello, authenticated user!');
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

Custom Authentication Function

This feature allows you to define a custom function to handle authentication logic. In this example, the custom authorizer function checks if the provided username and password match the expected values using a timing-safe comparison.

const express = require('express');
const basicAuth = require('express-basic-auth');

const app = express();

app.use(basicAuth({
    authorizer: (username, password) => {
        const userMatches = basicAuth.safeCompare(username, 'admin');
        const passwordMatches = basicAuth.safeCompare(password, 'supersecret');
        return userMatches & passwordMatches;
    },
    authorizeAsync: false,
    challenge: true
}));

app.get('/', (req, res) => {
    res.send('Hello, authenticated user!');
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

Other packages similar to express-basic-auth

Keywords

FAQs

Package last updated on 11 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc