Socket
Socket
Sign inDemoInstall

basic-auth

Package Overview
Dependencies
0
Maintainers
4
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    basic-auth

node.js basic auth parser


Version published
Maintainers
4
Install size
6.90 kB
Created

Package description

What is basic-auth?

The basic-auth npm package is a simple module to parse `Authorization` header for basic authentication in Node.js applications. It extracts the user credentials from the header and makes them available for authentication purposes.

What are basic-auth's main functionalities?

Parse Basic Auth Credentials

This code demonstrates how to use basic-auth to parse the `Authorization` header in an Express.js application. It checks if the credentials match a predefined username and password, and either grants access or denies it with a 401 status code.

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

app.use((req, res) => {
  const credentials = auth(req);
  if (!credentials || credentials.name !== 'user' || credentials.pass !== 'pass') {
    res.statusCode = 401;
    res.setHeader('WWW-Authenticate', 'Basic realm="example"');
    res.end('Access denied');
  } else {
    res.end('Access granted');
  }
});

app.listen(3000);

Other packages similar to basic-auth

Readme

Source

basic-auth

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Generic basic auth Authorization header field parser for whatever.

Installation

$ npm install basic-auth

API

var auth = require('basic-auth')

auth(req)

Get the basic auth credentials from the given request. The Authorization header is parsed and if the header is invalid, undefined is returned, otherwise an object with name and pass properties.

Example

Pass a node request or koa Context object to the module exported. If parsing fails undefined is returned, otherwise an object with .name and .pass.

var auth = require('basic-auth');
var user = auth(req);
// => { name: 'something', pass: 'whatever' }

With vanilla node.js http server

var http = require('http')
var auth = require('basic-auth')

// Create server
var server = http.createServer(function (req, res) {
  var credentials = auth(req)

  if (!credentials || credentials.name !== 'john' || credentials.pass !== 'secret') {
    res.statusCode = 401
    res.setHeader('WWW-Authenticate', 'Basic realm="example"')
    res.end('Access denied')
  } else {
    res.end('Access granted')
  }
})

// Listen
server.listen(3000)

License

MIT

Keywords

FAQs

Last updated on 02 Jul 2015

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc