Skip to content

Commit 45220a1

Browse files
committed
Merge branch 'master' into format
* master: Update docs to use `parse` method (jshttp#97)
2 parents 5c2e430 + b7be5af commit 45220a1

1 file changed

Lines changed: 14 additions & 21 deletions

File tree

README.md

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,10 @@ $ npm install basic-auth
2323
<!-- eslint-disable no-unused-vars -->
2424

2525
```js
26-
var auth = require('basic-auth');
26+
const { parse } = require('basic-auth');
2727
```
2828

29-
### auth(req)
30-
31-
Get the basic auth credentials from the given request. The `Authorization`
32-
header is parsed and if the header is invalid, `undefined` is returned,
33-
otherwise an object with `name` and `pass` properties.
34-
35-
### auth.parse(string)
29+
### parse(string)
3630

3731
Parse a basic auth authorization header string. This will return an object
3832
with `name` and `pass` properties, or `undefined` if the string is invalid.
@@ -44,25 +38,24 @@ auth authorization header string.
4438

4539
## Example
4640

47-
Pass a Node.js request object to the module export. If parsing fails
41+
Pass a Basic auth header to the `parse()` method. If parsing fails
4842
`undefined` is returned, otherwise an object with `.name` and `.pass`.
4943

5044
<!-- eslint-disable no-unused-vars, no-undef -->
5145

5246
```js
53-
var auth = require('basic-auth');
54-
var user = auth(req);
47+
const { parse } = require('basic-auth');
48+
const user = parse(req.headers.authorization);
5549
// => { name: 'something', pass: 'whatever' }
5650
```
5751

58-
A header string from any other location can also be parsed with
59-
`auth.parse`, for example a `Proxy-Authorization` header:
52+
A header string from any other location can also be parsed for example a `Proxy-Authorization` header:
6053

6154
<!-- eslint-disable no-unused-vars, no-undef -->
6255

6356
```js
64-
var auth = require('basic-auth');
65-
var user = auth.parse(req.getHeader('Proxy-Authorization'));
57+
const { parse } = require('basic-auth');
58+
const user = parse(req.getHeader('Proxy-Authorization'));
6659
```
6760

6861
A credentials object can be formatted with `auth.format` as
@@ -80,13 +73,13 @@ var authHeader = auth.format(credentials);
8073
### With vanilla node.js http server
8174

8275
```js
83-
var http = require('http');
84-
var auth = require('basic-auth');
85-
var compare = require('tsscmp');
76+
const http = require('node:http');
77+
const { parse } = require('basic-auth');
78+
const compare = require('tsscmp');
8679

8780
// Create server
88-
var server = http.createServer(function (req, res) {
89-
var credentials = auth(req);
81+
const server = http.createServer(function (req, res) {
82+
const credentials = parse(req.headers.authorization);
9083

9184
// Check credentials
9285
// The "check" function will typically be against your user store
@@ -101,7 +94,7 @@ var server = http.createServer(function (req, res) {
10194

10295
// Basic function to validate credentials for example
10396
function check(name, pass) {
104-
var valid = true;
97+
let valid = true;
10598

10699
// Simple method to prevent short-circuit and use timing-safe compare
107100
valid = compare(name, 'john') && valid;

0 commit comments

Comments
 (0)