# jwt-node-auth

> This is a NodeJS authentication package using JWT in the background. This package let you generate a JWT Token and Verify the token on a fly.

Latest version **1.0.1** (published 2023-05-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install jwt-node-auth
pnpm add jwt-node-auth
yarn add jwt-node-auth
bun add jwt-node-auth
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2023-05-05 |
| First published | 2023-05-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Geofrey Isiagi |
| Maintainers | isiagi |
| Keywords | authentication, authorization, jwt-authentication, node-authentication, node-auth, jwt-node |

## Links

- npm: https://www.npmjs.com/package/jwt-node-auth
- Repository: https://github.com/isiagi/jwt-node-auth
- Homepage: https://github.com/isiagi/jwt-node-auth#readme
- Issues: https://github.com/isiagi/jwt-node-auth/issues
- npm.io page: https://npm.io/package/jwt-node-auth

## Dependencies (4)

- [express](https://npm.io/package/express.md) ^4.18.2
- [ts-node](https://npm.io/package/ts-node.md) ^10.9.1
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^9.0.0
- [@types/express](https://npm.io/package/@types/express.md) ^4.17.17

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2023-05-05
- 1.0.0 — 2023-05-02

## README

# JWT-Node-Auth

**JWT-Node-Auth** is a NodeJS authentication package using JWT in the background. This package let you generate a JWT Token and Verify the token on a fly.

## Installation

```bash
npm install jwt-node-auth
```

## Usage

To use jwt-node-auth in your NodeJS application, you'll need to create a new instance of the NodeAuth class:

```js
const { NodeAuth } = require("jwt-node-auth");

const auth = new NodeAuth(process.env.jwt__secret, process.env.jwt_expiry);

```

 ### Generate Token 
 To generate a JWT token, call the getSignedJwtToken method on the auth object:

 ### async way

```
async function getToken(){
    const token = await auth.getSignedJwtToken({ userId: '12345' }); 

    return token
}
 //userId is token payload to be signed with token in generation.It can be any key value pair's like auth.getSignedJwtToken({ user: '12345', role: Admin, ... }).

```

### then way
```
function getToken() {
    auth.getSignedJwtToken({ userId: '12345' }).then((token) => {
            console.log(token);
            //return token
        }).catch((error) => {
            console.log(error);
        });
}

 //userId is token payload to be signed with token in generation.It can be any key value pair's like auth.getSignedJwtToken({ user: '12345', role: Admin, ... }).
```


 ### Verify Token And Protect Routes 
 To verify a JWT token, you can use the requireAuth middleware provided by the package. 
 This middleware can be used in the following ways.

 ```
 app.use(auth.requireAuth);
 // OR with Router
 router.get("/all", auth.requireAuth, testController.getAll);
 ```

 This will automatically verify the JWT token in the Authorization header of incoming requests. 

 //headers must have

```
`Authorization: Bearer ${token}`

Example
// "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNjgzMDIxMjU4LCJleHAiOjE2ODMwMjE3NTh9.jDVTDEoZsEG2m70qrxKzRcv1qo8er02PzFv3V-05ou0"
```
 If the token is valid, the middleware will set the req.user property to the decoded token payload.

```
 In protected a route
 
 console.log(req.user); //{ userId: '12345' }
```

 ## Contributing 

 Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.  

 Please make sure to update tests as appropriate.  

 ## License  

 [MIT](https://choosealicense.com/licenses/mit/)

---
_Source: https://npm.io/package/jwt-node-auth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
