https://github.com/random-guys/jwt256
Encapsulated JWT tokens (AES encrypted payload)
https://github.com/random-guys/jwt256
library
Last synced: over 1 year ago
JSON representation
Encapsulated JWT tokens (AES encrypted payload)
- Host: GitHub
- URL: https://github.com/random-guys/jwt256
- Owner: random-guys
- Created: 2019-02-06T14:01:02.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-22T09:57:26.000Z (over 3 years ago)
- Last Synced: 2025-02-02T12:35:45.581Z (over 1 year ago)
- Topics: library
- Language: TypeScript
- Homepage:
- Size: 818 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# jwt256
Implementation of JWT using AES-256

## To Use
`yarn add @random-guys/jwt256` or `npm install @random-guys/jwt256`
---
Ensure `JWT_SECRET` and `REDIS_KEY` environment variables are set, and that `REDIS_KEY` is a 32 digit string.
## Usage
```ts
import { GetUserAuth, SetUserAuth } from "@random-guys/jwt256";
import redisService from "@app/common/services/redis";
//creates an encrpted JWT token and saves it in Redis using the user's id.
@httpPost("/signup")
async signup(@request() req: Request, @response() res: Response, @requestBody(), body: SignupDTO) {
try {
let user = create(body);
const token = await SetUserAuth(redisService, user.id);
this.handleSuccess(req, res, { token, user });
} catch (err) {
this.handleError(req, res, err);
}
}
/**
* reads the Authorization header and validates the content with that stored in Redis
*/
@httpPost("/buy", GetUserAuth(redisService))
async buyBook(
@request() req: Request,
@response() res: Response,
@requestBody() body: BuyBookDTO
) {
try {
const sale = await this.bookRepo.buyBook({ id: req.user } , body);
this.handleSuccess(req, res, user);
} catch (err) {
console.log(err);
this.handleError(req, res, err);
}
}
```
The user id is in the `req.user` field.