@@ -6,7 +6,9 @@ Simple plug & play HTTP basic auth middleware for Express.
66
77Just run
88
9- npm install express-basic-auth
9+ ``` shell
10+ npm install express-basic-auth
11+ ```
1012
1113add the ` --save ` option to add it to the ` dependencies ` in your ` package.json ` as well
1214
@@ -15,12 +17,14 @@ add the `--save` option to add it to the `dependencies` in your `package.json` a
1517The module will export a function, that you can call with an options object to
1618get the middleware:
1719
18- var app = require('express')();
19- var basicAuth = require('express-basic-auth');
20+ ``` js
21+ var app = require (' express' )();
22+ var basicAuth = require (' express-basic-auth' );
2023
21- app.use(basicAuth({
22- users: { 'admin': 'supersecret' }
23- }));
24+ app .use (basicAuth ({
25+ users: { ' admin' : ' supersecret' }
26+ }));
27+ ```
2428
2529The middleware will now check incoming requests to match the credentials
2630` admin:supersecret ` .
@@ -38,13 +42,15 @@ containing an object with `user` and `password` properties, filled with the cred
3842If you simply want to check basic auth against one or multiple static credentials,
3943you can pass those credentials as in the example above:
4044
41- app.use(basicAuth({
42- users: {
43- 'admin': 'supersecret',
44- 'adam': 'password1234',
45- 'eve': 'asdfghjkl'
46- }
47- }));
45+ ``` js
46+ app .use (basicAuth ({
47+ users: {
48+ ' admin' : ' supersecret' ,
49+ ' adam' : ' password1234' ,
50+ ' eve' : ' asdfghjkl'
51+ }
52+ }));
53+ ```
4854
4955The middleware will check incoming requests to have a basic auth header matching
5056one of the three passed credentials.
@@ -55,11 +61,13 @@ Alternatively, you can pass your own `authorizer` function, to check the credent
5561however you want. It will be called with a username and password and is expected to
5662return ` true ` or ` false ` to indicate that the credentials were approved or not:
5763
58- app.use(basicAuth( { authorizer: myAuthorizer } ));
64+ ``` js
65+ app .use (basicAuth ( { authorizer: myAuthorizer } ));
5966
60- function myAuthorizer(username, password) {
61- return username.startsWith('A') && password.startsWith('secret');
62- }
67+ function myAuthorizer (username , password ) {
68+ return username .startsWith (' A' ) && password .startsWith (' secret' );
69+ }
70+ ```
6371
6472This will authorize all requests with credentials where the username begins with
6573` 'A' ` and the password begins with ` 'secret' ` . In an actual application you would
@@ -74,17 +82,19 @@ as the third parameter, which is expected to be called by standard node conventi
7482with an error and a boolean to indicate if the credentials have been approved or not.
7583Let's look at the same authorizer again, but this time asynchronous:
7684
77- app.use(basicAuth({
78- authorizer: myAsyncAuthorizer,
79- authorizeAsync: true
80- }));
85+ ``` js
86+ app .use (basicAuth ({
87+ authorizer: myAsyncAuthorizer,
88+ authorizeAsync: true
89+ }));
8190
82- function myAsyncAuthorizer(username, password, cb) {
83- if(username.startsWith('A') && password.startsWith('secret'))
84- return cb(null, true);
85- else
86- return cb(null, false)
87- }
91+ function myAsyncAuthorizer (username , password , cb ) {
92+ if (username .startsWith (' A' ) && password .startsWith (' secret' ))
93+ return cb (null , true );
94+ else
95+ return cb (null , false )
96+ }
97+ ```
8898
8999### Challenge
90100
@@ -93,18 +103,22 @@ responses of unauthorized requests. You can enable that by adding `challenge: tr
93103to the options object. This will cause most browsers to show a popup to enter credentials
94104on unauthorized responses:
95105
96- app.use(basicAuth({
97- users: { 'someuser': 'somepassword' },
98- challenge: true
99- }));
106+ ``` js
107+ app .use (basicAuth ({
108+ users: { ' someuser' : ' somepassword' },
109+ challenge: true
110+ }));
111+ ```
100112
101113## Try it
102114
103115The repository contains an ` example.js ` that you can run to play around and try
104116the middleware. To use it just put it somewhere (or leave it where it is), run
105117
106- npm install express express-basic-auth
107- node example.js
118+ ``` shell
119+ npm install express express-basic-auth
120+ node example.js
121+ ```
108122
109123This will start a small express server listening at port 8080. Just look at the file,
110124try out the requests and play around with the options.
0 commit comments