Skip to content

Commit b7be5af

Browse files
authored
Update docs to use parse method (jshttp#97)
1 parent c0d7950 commit b7be5af

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,53 +23,46 @@ $ 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.
3933

4034
## Example
4135

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

4539
<!-- eslint-disable no-unused-vars, no-undef -->
4640

4741
```js
48-
var auth = require('basic-auth');
49-
var user = auth(req);
42+
const { parse } = require('basic-auth');
43+
const user = parse(req.headers.authorization);
5044
// => { name: 'something', pass: 'whatever' }
5145
```
5246

53-
A header string from any other location can also be parsed with
54-
`auth.parse`, for example a `Proxy-Authorization` header:
47+
A header string from any other location can also be parsed for example a `Proxy-Authorization` header:
5548

5649
<!-- eslint-disable no-unused-vars, no-undef -->
5750

5851
```js
59-
var auth = require('basic-auth');
60-
var user = auth.parse(req.getHeader('Proxy-Authorization'));
52+
const { parse } = require('basic-auth');
53+
const user = parse(req.getHeader('Proxy-Authorization'));
6154
```
6255

6356
### With vanilla node.js http server
6457

6558
```js
66-
var http = require('http');
67-
var auth = require('basic-auth');
68-
var compare = require('tsscmp');
59+
const http = require('node:http');
60+
const { parse } = require('basic-auth');
61+
const compare = require('tsscmp');
6962

7063
// Create server
71-
var server = http.createServer(function (req, res) {
72-
var credentials = auth(req);
64+
const server = http.createServer(function (req, res) {
65+
const credentials = parse(req.headers.authorization);
7366

7467
// Check credentials
7568
// The "check" function will typically be against your user store
@@ -84,7 +77,7 @@ var server = http.createServer(function (req, res) {
8477

8578
// Basic function to validate credentials for example
8679
function check(name, pass) {
87-
var valid = true;
80+
let valid = true;
8881

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

0 commit comments

Comments
 (0)