Typescript Typings for basic-auth.
typings install --save basic-auth
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);
You can run the tests by running npm run build
and then npm run test
.