1.0.13 • Published 4 months ago

delta-authentication v1.0.13

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

delta-authentication

A customizable authentication system for Node.js with optional encryption and a dynamic user model.

🚀 Installation

Install the package via NPM:

npm install delta-authentication

📌 Automatic Dependency Check

If required packages (express, mongoose, jsonwebtoken) are missing, the package will prompt you to install them automatically.


🔹 Usage

1️⃣ Import and Connect Database

import { connectDB } from "delta-authentication";
connectDB("mongodb://localhost:27017/mydb");

2️⃣ Define a Custom User Model

You can select which fields to include in your schema:

import { createUserModel } from "delta-authentication";

// ✅ Creating different models with custom names
const User = createUserModel("User", ["name", "emailid", "password"]);
const Client = createUserModel("Client", [
  "firstname",
  "emailid",
  "password",
  "phoneno",
]);
const Admin = createUserModel("Admin", [
  "name",
  "emailid",
  "password",
  "address",
]);

Available fields:

  • name
  • firstname
  • middlename
  • lastname
  • emailid (required)
  • password (required)
  • age
  • address
  • phoneno

3️⃣ Set Up Authentication Routes

import { createAuthRoutes } from "delta-authentication";
app.use("/auth", createAuthRoutes(User));

📌 API Endpoints

Signup User

POST /auth/signup

Body:

{
  "name": "John Doe",
  "emailid": "john@example.com",
  "password": "123456"
}

Login User

POST /auth/login

Body:

{
  "emailid": "john@example.com",
  "password": "123456"
}

Response:

{
  "token": "your-jwt-token"
}

🔑 Optional: Enable End-to-End Encryption

By default, only passwords are encrypted. You can enable AES-256 encryption for any field using delta-encrypt-decrypt:

1️⃣ Install the Encryption Package

npm install delta-encrypt-decrypt

2️⃣ Enable Encryption in .env

Set up encryption in your environment variables:

USE_ENCRYPTION=true
SECRET_KEY="Create a 32 character secret key"

3️⃣ Set Up Authentication Routes with Selective Encryption

You can choose which fields to encrypt by passing an array of field names:

import { createAuthRoutes } from "delta-authentication";

app.use(
  "/auth",
  createAuthRoutes(User, {
    encryption: process.env.USE_ENCRYPTION === "true", // Convert string to boolean
    secretKey: process.env.SECRET_KEY, // Provide a 32-character secret key
    encryptFields: ["name", "emailid", "password"], // Select which fields to encrypt
  })
);

🔹 Example: Encrypted Signup Data

If encryptFields = ["name", "emailid", "password"], the stored user data will look like this:

{
  "name": "ENCRYPTED_DATA",
  "emailid": "ENCRYPTED_DATA",
  "password": "ENCRYPTED_DATA",
  "age": 28
}

📜 License

This project is licensed under the MIT License.

💻 Contributing

Feel free to submit issues and pull requests on GitHub.


Happy coding! 🚀

1.0.13

4 months ago

1.0.12

4 months ago

1.0.11

4 months ago

1.0.10

5 months ago

1.0.9

5 months ago

1.0.8

5 months ago

1.0.7

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago