License Key Library
A simple, secure, and flexible licensing library built for Node.js, React, Next.js, and general JavaScript applications.
🚀 Quick Start
Installation
npm install license-lib
Environment Setup Create a .env file at the root with your database, backend url and JWT configuration:
DB_HOST=localhost
DB_NAME=license_db
DB_USER_ADMIN=license_admin
DB_PASS_ADMIN=secure_admin_password
DB_USER_READER=license_reader
DB_PASS_READER=secure_reader_password
JWT_SECRET=your_jwt_secret
VITE_LICENSE_API_URL=https://your.backend.url.com/api #or http://localhost:3000
⚙️ Database Setup
Use the provided SQL script to set up your database:
CREATE DATABASE license_db;
USE license_db;
CREATE TABLE licenses (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id VARCHAR(255) NOT NULL,
hashed_key VARCHAR(255) NOT NULL,
license_type ENUM('one-time', 'subscription', 'trial') DEFAULT 'one-time',
tier VARCHAR(50) DEFAULT 'basic',
valid_until DATETIME DEFAULT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE USER 'license_reader'@'%' IDENTIFIED BY 'secure_reader_password';
GRANT SELECT ON license_db.* TO 'license_reader'@'%';
CREATE USER 'license_admin'@'%' IDENTIFIED BY 'secure_admin_password';
GRANT SELECT, INSERT, UPDATE ON license_db.* TO 'license_admin'@'%';
FLUSH PRIVILEGES;
📦 API Endpoints
Generate License
POST /generate-license
Body:
{
"userId": "user123",
"licenseType": "subscription",
"tier": "pro",
"validUntil": "2024-12-31"
}
Response:
{
"licenseKey": "generated.jwt.token"
}
Validate License
POST /validate-license
Body:
{
"userId": "user123",
"licenseKey": "generated.jwt.token"
}
Response:
{
"valid": true
}
🎯 Client Integration Import functions directly from the client module:
import { activateLicense, generateFingerprint } from './client';
// Validate license
const valid = await activateLicense(userId, licenseKey);
// Device fingerprinting
const fingerprint = generateFingerprint();
⚛️ React Integration Use the provided React hook and provider:
import { LicenseProvider } from './react/LicenseProvider';
import useLicense from './react/useLicense';
const App = () => (
<LicenseProvider userId="user123" licenseKey="jwt.token">
<YourApp />
</LicenseProvider>
);
const YourApp = () => {
const { isLicensed } = useLicense();
return <div>{isLicensed ? 'Licensed!' : 'Not Licensed!'}</div>;
};
▲ Next.js Integration Wrap your component easily:
import withLicense from './nextjs/withLicense';
const Page = ({ isLicensed }) => (
<div>{isLicensed ? 'Licensed!' : 'Not Licensed!'}</div>
);
export default withLicense(Page, { userId: 'user123', licenseKey: 'jwt.token' });
Description
Languages
JavaScript
100%