https://github.com/samayun/auth-helper
collection of authentication related helper function
https://github.com/samayun/auth-helper
authentication authentication-backend bcryptjs json-web-token jwt-authentication
Last synced: 8 months ago
JSON representation
collection of authentication related helper function
- Host: GitHub
- URL: https://github.com/samayun/auth-helper
- Owner: samayun
- Created: 2021-07-26T13:29:54.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-26T15:11:57.000Z (almost 5 years ago)
- Last Synced: 2025-02-28T10:09:06.692Z (over 1 year ago)
- Topics: authentication, authentication-backend, bcryptjs, json-web-token, jwt-authentication
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/jwt-auth-helper
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# jwt-auth-helper
collection of authentication related helper function
## Installation
Installation is as simple as any other `npm` package:
```
$ npm install jwt-auth-helper
```
## Usage
* hash & compare password
* generate & verify token
### Hash data
```js
const { BCrypt } = require('jwt-auth-helper');
async function signup() {
const hashedPassword = await BCrypt.makeHash("password");
console.log(hashedPassword);
}
signup();
```
### compare hashed data
```js
const { BCrypt } = require('jwt-auth-helper');
async function login() {
const isMatched = await BCrypt.compareHash("password", "$2b$10$P6fFTv5nUlIS57E8Yb8qiOk72FdoVgYmfewEcEGUddrgGwXIg5QfO");
console.log(isMatched);
}
login();
```
### Generate encoded data
```js
const { JWT } = require('jwt-auth-helper');
const jwt = new JWT(process.env.SECRET || "JWT_SECRET_KEY");
async function ResponseAuthTokenFromUser() {
const authenticateUser = {
id: 1,
name: "Samayun Chowdhury",
role: "ADMIN",
password: null
}
const access_token = jwt.generateJWTToken(authenticateUser, '5m');
console.log(access_token);
}
ResponseAuthTokenFromUser();
```
### decode encoded data
```js
const { JWT } = require('jwt-auth-helper');
const jwt = new JWT(process.env.SECRET || "JWT_SECRET_KEY");
async function verify() {
const decodedToken = jwt.verifyToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwibmFtZSI6IlNhbWF5dW4gQ2hvd2RodXJ5Iiwicm9sZSI6IkFETUlOIiwicGFzc3dvcmQiOm51bGwsImlhdCI6MTYyNzMxMDM4MywiZXhwIjoxNjI3MzEwNjgzfQ.cSrzBWDNzQMWFLdXct-7io_YWKfzz98xmiH76hxQZHY");
console.log(`decodedToken`, decodedToken);
}
verify();
```