1.0.6 • Published 4 years ago

auth-jwt v1.0.6

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

auth-jwt

An authentication module using JWT

Install

npm install auth-jwt

Usage

Example

auth.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

app.get('/user', auth, (req, res) => {
    ...
})

Include the JWT or Bearer in requests

Include the token created by JWT in the request header

Authorization: JWT JSON_WEB_TOKEN_STRING.....

or

Authorization: Bearer JSON_WEB_TOKEN_STRING.....

Get token in requests

const auth = require('auth-jwt');

let auth = (req, res, next) => {
    auth.getToken(req)
        .then((user)=>{
            console.log(token);
        })
        .catch((e) => console.log(e) )
}

Async / Await

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});
    }
}

Tests

npm install
npm test

License

The MIT License

Copyright (c) 2016 Wallace Silva

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

7 years ago

0.0.10

7 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago