Skip to content

typed-typings/npm-basic-auth

Repository files navigation

Typed basic-auth

Greenkeeper badge Build Status

Typescript Typings for basic-auth.

Installation

typings install --save basic-auth

Usage

import express = require('express');
import auth = require('basic-auth');

const app = express();

app.get('/', (req: express.Request, res: express.Response) => {
    const credentials = auth(req);
    // => { name: 'something', pass: 'whatever' }
});
import {createServer, IncomingMessage, ServerResponse} from 'http';
import auth = require('basic-auth');

const server = createServer((req: IncomingMessage, res: ServerResponse) => {
  const 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');
  }
});

server.listen(3000);

More examples

Contributing

You can run the tests by running npm run build and then npm run test.