Skip to content

Commit

Permalink
add docs and update code
Browse files Browse the repository at this point in the history
  • Loading branch information
johnOfGod33 committed Jul 14, 2024
1 parent 9f4dac9 commit b3fdee6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 41 deletions.
33 changes: 0 additions & 33 deletions .github/workflows/npm-publish.yml

This file was deleted.

75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# JWT AUTHENTICATION

This package provides functions for managing user authentication, including password hashing, password comparison for authentication, and JWT token verification

This package provides functions for managing user authentication, including:

- password hashing
- password comparison and token creation for authentication
- Jwt token verification

# Installing

coming soon

# Usage

## 1. hashPassowrd

This function hash password using bcrypt

**Example**

```javascript
const { hashPassword } = require("<package-name>");

hashPassword(password)
.then((hashPassword) => {
// save user info in your db with hashPassword
})
.catch((err) => {
console.error(err);
});
```

## 2. authenticateUser

This function compares a password with a hashed password and creates a JWT token if the passwords match.

**Example**

```javascript
const { authenticateUser } = require("<package-name>");

authenticateUser(hashedPassword, myPassword, userId, mySecretKey)
.then((token) => {
// return the token
})
.catch((err) => {
console.error(err);
});
```

## 3. VerifyToken

This function intercepts an HTTP request and verifies the JWT token in the request headers.

**Example**

```javascript
const express = require("express");
const { verifyJwtToken } = require("<package-name>");

const app = express();
const secretKey = "mySecretKey";

app.use(verifyJwtToken(secretKey));

app.get("/protected-route", (req, res) => {
res.send(`Hello, user ${req.ID}`);
});

app.listen(3000, () => {
console.log("Server started on port 3000");
});
```
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const bcrypt = require("bcrypt");
/**
*
* @param {String | Buffer} password the password to be encrypted
* @returns A promise to be either resolved by the encrypted password or or rejected with an error
* @returns A promise to be either resolved by the hash password or or rejected with an error
*/

const getHashPassword = async (password) => {
const hashPassword = async (password) => {
try {
let encryptedPassword = await bcrypt.hash(password, 10);

Expand All @@ -18,14 +18,14 @@ const getHashPassword = async (password) => {
};

/**
*Compare password and create token
*Compare password and create token if passwords matchs
* @param {String} encryptedPassword your encryptedPassword. it will be compare with input password
* @param {String} inputPassword password input
* @param {String | Number} userId user id will be use to create token if input password and encrypted password match
* @param {String} secretKey Secret key will be use to encrypted the token
* @returns
* @returns A promise to be either resolved by the token or or rejected with an error
*/
const authentification = async (
const authenticateUser = async (
encryptedPassword,
inputPassword,
userId,
Expand All @@ -47,7 +47,8 @@ const authentification = async (
};

/**
* Intercept request and verify the token
* A middleware function that verifies the JWT token. If the token is valid, the user ID is added to the request (req.ID), otherwise, a 401 error is returned.
* @param {String} secretKey secret key you use to encrypte the token
*/

const verifyToken = (secretKey) => {
Expand All @@ -64,7 +65,7 @@ const verifyToken = (secretKey) => {
};

module.exports = {
getHashPassword,
authentification,
hashPassword,
authenticateUser,
verifyToken,
};

0 comments on commit b3fdee6

Please sign in to comment.