auth-jwt
Version:
Biblioteca de autenticação simples via JWT
90 lines (61 loc) • 1.33 kB
Markdown
> An authentication module using JWT
npm install auth-jwt
auth.js
```js
const auth = require('auth-jwt');
let auth = (req, res, next) => {
auth.verify(req, 'secretKey')
.then((user)=>{
req.user = user;
next()
})
.catch((e) => res.sendStatus(401) )
}
app.get('/user', auth, (req, res) => {
...
})
```
routes.js
```js
app.get('/user', auth, (req, res) => {
...
})
```
Include the token created by JWT in the request header
Authorization: JWT JSON_WEB_TOKEN_STRING.....
or
Authorization: Bearer JSON_WEB_TOKEN_STRING.....
```js
const auth = require('auth-jwt');
let auth = (req, res, next) => {
auth.getToken(req)
.then((user)=>{
console.log(token);
})
.catch((e) => console.log(e) )
}
```
```js
const auth = require('auth-jwt');
module.exports = async (req, res, next) => {
try {
req.user = await auth.verify(req, 'secretKey');
next();
} catch {
res.status(401).json({code: 401});
}
}
```
npm install
npm test
The [MIT License](http://opensource.org/licenses/MIT)
Copyright (c) 2016 Wallace Silva