Skip to content

Commit 888f0e9

Browse files
aarongodinjfromaniello
authored andcommitted
Update Readme and use a consistent JS style for code examples
1 parent 6591014 commit 888f0e9

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,23 @@
22

33
[![Build](https://travis-ci.org/auth0/express-jwt.png)](http://travis-ci.org/auth0/express-jwt)
44

5-
Middleware that validates JsonWebTokens and sets `req.user`.
6-
7-
This module lets you authenticate HTTP requests using JWT tokens in your Node.js
8-
applications. JWTs are typically used to protect API endpoints, and are
9-
often issued using OpenID Connect.
5+
This module provides Express middleware for validating and decoding JWTs ([JSON Web Tokens](https://jwt.io)) through the [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken/) module. The decoded information is provided on the Express request object.
106

117
## Install
128

13-
$ npm install express-jwt
9+
```
10+
$ npm install express-jwt
11+
```
1412

1513
## Usage
1614

17-
The JWT authentication middleware authenticates callers using a JWT.
18-
If the token is valid, `req.user` will be set with the JSON object decoded
19-
to be used by later middleware for authorization and access control.
20-
21-
For example,
15+
Basic usage using an HS256 secret:
2216

2317
```javascript
2418
var jwt = require('express-jwt');
2519

2620
app.get('/protected',
27-
jwt({secret: 'shhhhhhared-secret'}),
21+
jwt({ secret: 'shhhhhhared-secret' }),
2822
function(req, res) {
2923
if (!req.user.admin) return res.sendStatus(401);
3024
res.sendStatus(200);
@@ -34,9 +28,11 @@ app.get('/protected',
3428
You can specify audience and/or issuer as well:
3529

3630
```javascript
37-
jwt({ secret: 'shhhhhhared-secret',
31+
jwt({
32+
secret: 'shhhhhhared-secret',
3833
audience: 'http://myapi/protected',
39-
issuer: 'http://issuer' })
34+
issuer: 'http://issuer'
35+
})
4036
```
4137

4238
> If the JWT has an expiration (`exp`), it will be checked.
@@ -100,6 +96,7 @@ app.use(jwt({
10096
```
10197

10298
### Multi-tenancy
99+
103100
If you are developing an application in which the secret used to sign tokens is not static, you can provide a callback function as the `secret` parameter. The function has the signature: `function(req, payload, done)`:
104101
* `req` (`Object`) - The express `request` object.
105102
* `payload` (`Object`) - An object with the JWT claims.
@@ -108,6 +105,7 @@ If you are developing an application in which the secret used to sign tokens is
108105
* `secret` (`String`) - The secret to use to verify the JWT.
109106

110107
For example, if the secret varies based on the [JWT issuer](http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#issDef):
108+
111109
```javascript
112110
var jwt = require('express-jwt');
113111
var data = require('./data');
@@ -126,7 +124,7 @@ var secretCallback = function(req, payload, done){
126124
};
127125

128126
app.get('/protected',
129-
jwt({secret: secretCallback}),
127+
jwt({ secret: secretCallback }),
130128
function(req, res) {
131129
if (!req.user.admin) return res.sendStatus(401);
132130
res.sendStatus(200);
@@ -158,19 +156,21 @@ var isRevokedCallback = function(req, payload, done){
158156
};
159157

160158
app.get('/protected',
161-
jwt({secret: 'shhhhhhared-secret',
162-
isRevoked: isRevokedCallback}),
159+
jwt({
160+
secret: 'shhhhhhared-secret',
161+
isRevoked: isRevokedCallback
162+
}),
163163
function(req, res) {
164164
if (!req.user.admin) return res.sendStatus(401);
165165
res.sendStatus(200);
166-
});
166+
}
167+
);
167168
```
168169

169170
### Error handling
170171

171172
The default behavior is to throw an error when the token is invalid, so you can add your custom logic to manage unauthorized access as follows:
172173

173-
174174
```javascript
175175
app.use(function (err, req, res, next) {
176176
if (err.name === 'UnauthorizedError') {
@@ -179,8 +179,7 @@ app.use(function (err, req, res, next) {
179179
});
180180
```
181181

182-
You might want to use this module to identify registered users while still providing access to unregistered users. You
183-
can do this by using the option _credentialsRequired_:
182+
You might want to use this module to identify registered users while still providing access to unregistered users. You can do this by using the option `credentialsRequired`:
184183

185184
```javascript
186185
app.use(jwt({
@@ -196,8 +195,10 @@ app.use(jwt({
196195

197196
## Tests
198197

199-
$ npm install
200-
$ npm test
198+
```
199+
$ npm install
200+
$ npm test
201+
```
201202

202203
## Contributors
203204
Check them out [here](https://github.com/auth0/express-jwt/graphs/contributors)

0 commit comments

Comments
 (0)